Make Android Great (Again) II - Termux advanced compilation manual

1. Before we start

The first article of the series: Make Android Great (Again) - Dive into Termux

This article assumes that you have learned the following:

  1. Know how to use an Android phone
  2. Install Termux on the mobile phone
  3. Know how to run commands on Termux
  4. Be familiar with using Linux

This article also supposes you've got the following equipment:

  1. An Android phone for installing and running Termux
  2. A computer, install Ubuntu LTS version (docker is recommended).

2. Some best practices that can improve efficiency

Before starting to explain how to compile Termux package compilation, it is necessary to share some of the best practices that can significantly improve efficiency.
Best practices can greatly improve efficiency, make the mood more pleasant, greatly reduce the time from getting started to giving up and reduce the probability.

Best practices for improving the efficiency of running commands in Termux

  1. Connect the Android phone to a Bluetooth keyboard
  2. Or, run the sshd server on your mobile phone (see below for details), and use computer to connect to the mobile phone's ssh through the LAN wifi, to control the mobile phone more easily

How to make Termux run sshd as openssh server?

Run the following commands on mobile phone's Termux APP:

pkg install openssh
passwd # set current user's password
sshd # start openssh-server

Run the following commands on the computer, or use other ssh connection software to fill in the corresponding parameters to connect:

ssh -p 8022 xxx@[your phone IP]

Best practices for improving compilation efficiency

Compile directly with Docker.
This way you will have fewer weird operating system exclusive problems. Regardless of whether the host machine is Windows, Mac or Linux itself, it is recommended to use docker to run the official Ubuntu image provided by Termux packages to compile.
In this way, the compilation environment can be started with one click, without installing too many toolchain program on your Ubuntu system, and there will be no environmental configuration problems.

3. Preparations before compilation

Common preparations before entering Docker

Clone the latest Termux packages repository:

git clone
cd termux-packages

Run the docker script inside the Termux packages to create a compilation environment:

bash scripts/

After entering the docker environment, you can freely run various compilation commands.

Advanced tips for the Termux packages docker environment

Custom parameters before entering Docker:

# Customize container name, e.g.:
export CONTAINER_NAME=hhj_termux_armv8

# Customize the mirror address, e.g.:

# Finally, run the docker environment and automatically apply the above parameters
bash scripts/

How to access host machine's inside docker?
Edit scripts/, add --network host \ in the appropriate position below $SUDO docker run \

How to mount more local directories?
Add --volume [host directory]:[docker directory] \ below --volume $VOLUME \

Some useful custom settings after entering Docker

After entering docker, you can set the following environment variables and set them as needed.

# Network proxy settings
export https_proxy=
export http_proxy=
export no_proxy="localhost,,"

# Set compilation parameters, adjust as needed
export TERMUX_ARCH=aarch64 # arm, aarch64, i686 or x86_64.
export TERMUX_TOPDIR=/home/builder/termux-packages/termux-build # compile output directory

# Path setting, enhance compatibility
export ANDROID_HOME=/home/builder/lib/android-sdk-9123335
export NDK=/home/builder/lib/android-ndk-r25c
export PATH=/home/builder/termux-packages/termux-build/_cache/cmake-3.24.2/bin:$PATH

Manually run compilation commands without scripts (optional)

Usually suitable for debugging, when you need to manually run compilation commands in docker, without using scripts.
The following environment variables can be used to configure the environment for cmake, make and other commands.
For more detailed information, please refer to:

Note: Do not use host build or host build
Note 2: If you still don't understand what this section is talking about, just skip this section.

export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
export TARGET=aarch64-linux-android
export API=24
export AR=$TOOLCHAIN/bin/llvm-ar
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export AS=$CC
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/ld
export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
export STRIP=$TOOLCHAIN/bin/llvm-strip


4. Start compiling the Termux packages

First make sure that you have configured and entered docker according to the previous section.

Take compiling Python as an example. By default, the dependencies are compiled first, and then the ontology is compiled:

./ python

If you want to download dependencies from the official apt repo, only compile python itself locally:

./ -I python

How to compile legacy software?

For example, inside Termux's official apt repo, python has reached python 3.11, and the official apt repo only keeps the latest version, meaning the old version of Python can no longer be installed through apt install or pkg install.

If we want to use Python3.10 again, what should we do? We can compile her by ourselves. The following are the detailed steps.

  1. Switch the git repository back to the python 3.10 script
cd [git repository directory of your termux-packages]
git checkout bootstrap-2022.10.23-r1+apt-android-7 # The latest version that supports python 3.10

Confirm the Python version number in packages/python/, namely _MAJOR_VERSION=3.10, which proves that the switching was successful.

  1. Clean up the compilation cache
# Clear the flags of compiled packages
rm -rf /data/data/.built-packages

# Clear all compiled caches
  1. Start compiling

Familiar formula:

./ python

Note: It is not recommended to use the -I parameter, because many apt software inside the official apt repo have been updated, and old versions of Python dependencies may have compatibility issues. It is better to recompile them all through source code.

  1. Installation

After the compilation is successful, copy all the files in the output directory to the phone
Then execute the following command on your mobile phone's Termux APP, to install them manually (note that the dependencies should be installed first and then the main ones):

dpkg -i xxx.deb

How to self-host the apt service after compiling?

After compiling, we might find that dpkg installation is too troublesome to deal with various dependencies, so how to create an apt service that can be hosted by yourself?

Suppose you already have a web service.

Inside docker or Linux host machine, use the following command to build apt related files and directories:

pip3 install termux-apt-repo #

# Generate the apt directory files in the directory named "backup"
cd xxx/termux-packages
termux-apt-repo --use-hard-links output backup stable main

# Synchronize to the directory of the web service (if you use ftp or other methods, please work around it yourself)
rsync --delete -rvP backup/dists xxx/www/html/termux-packages # Assume that this directory corresponds to

Then execute the following command on the Termux of the mobile phone

echo "deb [trusted=yes] stable main" > $PREFIX/etc/apt/sources.list
cat <<EOF > $PREFIX /etc/termux/chosen_mirrors

apt update
pkg search python # Confirm that python is the version compiled by yourself
pkg install python # Happy to install the python compiled by myself

5. More reference documents

Leave a comment