La Technique

Installing Python Fabric on Termux in Android

TL;DR: To install the Fabric package in a Python virtual environment in Termux, do:

pkg install python python-bcrypt python-cryptography clang libffi libsodium openssl
SODIUM_INSTALL=system pip install pynacl
python -m venv --system-site-packages venv
. venv/bin/activate
pip install fabric

To perform the necessary occasional reboot of my home server (which I prefer to trigger manually, so that I’m in control of its downtimes), I just have to log in via SSH and run a quick sudo reboot. I can do this anywhere with my phone, thanks to Tailscale and the wonderful Termux app, which unlocks the power of the Linux command-line environment on Android. (Android is still a Linux OS, after all, a super fancy distribution of it.)

When doing the reboots though, I also want to first pause the Healthchecks.io uptime monitoring I have set up for the server, so as not to trigger false alarms. I decided to write a script to automate this pause-monitoring-and-reboot-server sequence, which seemed like a fun and quick exercise. Healthchecks.io has an HTTP API, which I’m comfortable with. The server has SSH, and I’ve never automated doing things over SSH, so there was something to learn for me.

I found Fabric, which is perfect for this remote execution task. To work with SSH, Fabric builds on top of Paramiko, a Python implementation of the SSH protocol. Fabric is easy to learn and use for this basic reboot task I want to do, and I figured it out quickly.

That was on my laptop, however. The roadblock came when I tried to run the Fabric script in my target deployment environment, Termux on my phone. It turns out that Fabric has quite a number of dependencies that do not work with a quick pip install command. This is the friction that comes with using Termux, on an ARM CPU architecture system, while developing from an x86/AMD64 PC, which is still by far the best-supported architecture for the Python ecosystem. Fabric builds on top of Paramiko, which in turn relies on the Python cryptography package, and there are further low-level library dependencies (like libsodium), often written in C or some other systems programming language. Which makes sense, as this is security we’re dealing with, and here tried-and-tested is often the way to go.

I’m clearly not the first person to try and install Fabric on Termux, but I still had to piece together the complete solution from places on the interwebs to deal with my particular setup of using a Python virtual environment. I came up with the sequence in the TL;DR section above. Note that I’m not sure if all of those pkg packages are actually needed from a fresh install of Termux, though there’s likely no harm in installing all these reputable packages. Notice also that this installs some packages system-wide, which partially defeats the purpose of using a virtual environment. Only partially: Fabric still installs a lot more dependencies, which would then be contained in the venv.

I’m sharing this in the hope of saving precious time for a few kindred souls, though I guess I’m also feeding a bit of useful data to the generative AI slurpers.

Previous: