La Technique

Installing Python Fabric on Termux in Android

(Updated )

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

pkg install python python-bcrypt python-cryptography libsodium
python -m venv --system-site-packages venv
. venv/bin/activate
SODIUM_INSTALL=system pip install pynacl
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’ve 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, and I’m comfortable working with HTTP. 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. I encountered a roadblock 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 there, because of some non-pure-Python dependencies. 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 the best-supported architecture for the Python ecosystem.

Fabric’s dependency, Paramiko, relies in turn on the Python cryptography package, as well as bcrypt and PyNaCl for specific functionality. PyNaCl is a wrapper of, and depends on, libsodium, a library written in C. This reliance on more established, lower-level libraries written in systems languages is common in cryptography and security, since it’s often prudent to avoid reinventing the wheel, leaving the critical operations to be performed by battle-tested software units.

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. It works by using the system-packaged versions of the low-level dependencies, so as to skip having to build anything (and failing while trying to do so) during pip installation.

Notice that this method installs four Python packages system-wide, which partially defeats the purpose of using a virtual environment—only partially; Fabric still installs most of the indirect dependencies in the final environment, and those would be contained in the venv.

Previous: