Termux turns Android into a Linux development environment

Termux turns Android into a Linux development environment

Old Android devices become development powerhouses.

tux and android stuffed animals on shelf
Image credits : 

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

So you finally figured out how to exit Vim and you can write the most highly optimized version of "Hello World" this side of the Mississippi. Now it's time to up your game! Check out Termux for Android.

What is Termux?

Termux is an Android terminal emulator and Linux environment. What that means in practice is that you can install Termux on most Android devices and do almost anything you would do in a full Linux development environment on that device. That all sounds cool, but you're probably asking yourself, "why would I want to code on my phone on a touch screen? That just sounds awful." Start thinking more along the lines of tablets paired with a keyboards or Chromebooks that can now run Android applications. These are very cheap devices that can now be used to introduce people to Linux hacking and development. I know many of us in the Linux community started out by installing Linux on an old PC.

Tablets and Chromebooks are this generation's old, junky computers. And there are plenty to go around. Why not use them to introduce the next generation to Linux? And since Termux can be installed with a click in the Google Play Store, I would argue Termux is the easiest way to introduce anyone to Linux. But don't leave all the fun for the noobs. Termux can accommodate many of your needs for a Linux development environment.

Termux is Linux, but it is based on Android and runs in a container. That means you can install it with no root access required—but it also means it may take some getting used to. In this article, I'll outline some tips and tricks I found to get Termux working as a full-time development environment.

Where's all my stuff?

The base of the Termux filesystem that you can see starts around /data/data/com.termux/files/. Under that directory, you'll find your home directory and the usr directory, where all the Linux packages are installed. This is kind of weird, but no big deal right? You would be wrong, because almost every script on the planet is hard coded for /bin/bash. Other libraries, executables, and configuration files are in places inconsistent with other Linux distributions.

Termux provides lots of packages that have been modified to run correctly. Try looking there first instead of doing a custom build. However, you will still probably need to custom-build many things. You can try modifying your package's source code, and even though changing paths is easy, it gets old quick. Thankfully Termux also comes bundled with termux-exec. Termux-exec will redirect script paths on the fly to get them to work correctly.

You may still run into some hard-coded paths that termux-exec doesn't handle. Since you don't have root access in Termux, you can't just create a symlink to fix path issues. However, you can create a chroot jail. Using the PRoot package, you can create a chroot that you have full control over and allows you to modify anything you want. You can also make chroots of different Linux distributions. If you are a Fedora fan, you can use Termux and run it in a chroot jail. Check out the PRoot page for more distros and installation details, or you can use this script to make a Termux chroot jail. I've only tried the Termux chroot and the Ubuntu chroot. The Ubuntu chroot had some issues that needed to be worked around, so your mileage may vary depending on the version of Linux you choose.

One user to rule them all

In Termux, everything is installed and run under one user. This isn't so much a problem, rather something you need to get used to. This also means the typical services and user groups you might be familiar with are nowhere to be found. And nothing auto-starts on boot, so it's up to you to manage the start and stop of services you might use, like databases, SSH, etc. Also remember, your one user can't modify the base system, so you will need to use a chroot if you need to do that. Since you don't have nice, preset start scripts, you will probably have to come up with some of your own.

For everyday development, I needed Postgres, Nginx, and Redis. I'd never started these services manually before; normally they start and stop for me automatically, and I had to do a little digging to find out how to start my favorite services. Here is a sample of the three services I just mentioned. Hopefully, these examples will point you in the right direction to use your favorite service. You can also look at a package's documentation to find information on how to start and stop it.

Postgres

Start: pg_ctl -D $PREFIX/var/lib/postgresql start
Stop: pg_ctl -D $PREFIX/var/lib/postgresql stop

Nginx

Start: nginx
Stop: nginx -s stop

Redis

Start: redis-server $PREFIX/etc/redis.conf
Stop: kill "$("$PREFIX/bin/applets/cat" "$PREFIX/var/run/redis_6379.pid"

Broken dependencies

Android is built differently than other versions of Linux, and its kernel and libraries don't always match those in typical Linux software. You can see the common porting problems when trying to build software in Termux. You can work around most of them, but it may be too much effort to fix every dependency in your software.

For example, the biggest problem I ran into as a Python developer is by the Android kernel not supporting semaphores. The multi-processing library in Python depends on this functionality, and fixing this on my own was too difficult. Instead, I hacked around it by using a different deployment mechanism. Before I was using uWSGI to run my Python web services, so I switched to Gunicorn. This allowed me to route around using the standard Python multi-processing library. You may have to get a little creative to find alternative software dependencies when switching to Termux, but your list will probably be very small.

Everyday Termux

When using Termux on a daily basis, you'll want to learn its touch screen or hardware keyboard shortcuts. You'll also need a text editor or IDE for coding. All the likely console-based editors are available through a quick package install: Vim, Emacs, and Nano. Termux is only console-based, so you won't be able to install any editors based on a graphical interface. I wanted to make sure Termux had a great IDE to go along with it, so I built the web-based Neutron64 editor to interface seamlessly with Termux. Just go to Neutron64.com and install Neutron Beam on Termux to start coding.

Check out Termux and turn your old Android devices into development powerhouses. Happy coding!

Topics

About the author

Paul Bailey - Paul is a web developer with a background in aerospace engineering. He helps organize Python community events in Texas with PyWeb Houston and PyTexas. He’s obsessed with Web technology and created a web based development environment called Neutron64. In addition to being a Web and aerospace geek, he is a father of three and can cook a pretty mean pizza from scratch.