Unleash the potential of your unrooted Android phone

Ren Jie Chong
Level Up Coding
Published in
8 min readOct 11, 2020

--

Photo by Geeky Shots on Unsplash

An estimated 85% of smartphone users worldwide use Android. Regardless of whether you were drawn to the ecosystem for its affordable price point, highly customizable nature, or your outright contempt of fruit, there is a very good chance that you have not explored the amazing things your (unrooted) Android device can do.

At its core, the Android OS is built on top of a Linux kernel. One of the main differences between Android and other distros is that it does not come packaged with many of the GNU libraries commonly found in Linux. As a result, most applications cannot be run in Android. Instead, the Android Runtime (ART) executes dex files and bytecode that has been compiled from Java — apps from the play store that we’re all too familiar with.

Figuring out a convenient way to access the Linux shell is key to unleashing your phone’s potential. Thankfully, the doors to the terminal have been kept pretty open, and today, we can build upon the fantastic contributions that have been made to various open-sourced projects… enter Termux.

Installing Termux

Termux is an Android terminal emulator and Linux environment application that works directly with no rooting or setup required. A minimal base system is installed automatically, additional packages are available using the package manager.

That is how the official documentation defines this nifty tool. The experience Termux gives is similar to what you get when creating docker images from an Alpine base image —a minimal, lightweight environment. After installing Termux from the play store, follow these simple steps to make it work harder for you:

Installing Termux and basic dependencies

Do take note that there are limitations as to what you can install in Termux. For example, my attempts at getting nvm working never succeeded (thread here), which is why i rely on the nodejs-lts package instead. Installing dependencies can be a bit of a hit and miss, but at least in my experience, you can get most things up and running with a little tweaking.

In this day and age, you would be forgiven for thinking Termux bland. After all, here is a terminal that doesn’t shimmer with fancy colors, and it does not appear to augment the individual with 1337-developer abilities. Let’s fix that next!

Installing Oh-My-Termux

If you develop from a Linux or OSX machine, you’re likely to have used or seen someone use Oh-My-Zsh at some point. At the heart of it all lies Zsh, which is pretty much like Bash, but with added capabilities such as built-in customization via plugins and themes (look here for the list of official themes). To get things working with the least amount of effort, I would recommend setting up Oh-My-Termux. It sets you up with the most popular theme, Powerlevel9k, and installs the necessary Powerline font for you:

To toggle your shell between Bash or Zsh, you can type chsh followed by bash or zsh respectively, then restart your shell. Here’s a comparison of the uninspiring default Bash shell against Zsh after installing Oh-My-Termux. In Bash, I have to type the full git command that I want to execute. With Oh-My Termux, however, I just need to type git a and hit the TAB key, which provides me with a list of matching git commands that I can use. Here’s what they look like before and after the facelift:

(Left) Image of plain Bash terminal versus (Right) a Zsh terminal with lots of useful information
(Left) Image of plain Bash terminal versus (Right) a Zsh terminal with lots of useful information

With Bash, anything that we want to initialize (such as scripts or environment variables) whenever we open a shell is typically added to ~/.bashrc. For Zsh, the same concept applies, but we’re now looking at ~/.zshrc. This is also where Zsh can be customized to your desires:

Available customization in ~/.zshrc

Note that Powerlevel9k is deprecated at the time of this writing. You may wish to update your configurations to use its successor, Powerlevel10k. It offers some material and aesthetic enhancements, such as having plugins load in a non-blocking manner, as well as a new CLI wizard for customizing the interface further.

So what can we do now?

Having Termux and Oh-My-Termux setup with NodeJS theoretically allows you to take on web development tasks… but that’s a little hardcore if you ask me. I will not last more than 15 minutes staring at a 6" screen; and mashing away at the soft keyboard with my fat fingers will simply accelerate the degradation of my screen around the BACKSPACE key. But hey, perhaps tunnel vision is your special ability, or you’re one of those Vim gurus, in which case, go for it! If not for full-fledged development, what then do I use Termux for?

  • Using it to control my Heroku services: upgrading projects to the Hobby tier when I need them to be up more reliably, then switching them back to the Free tier & scaling the web dynos to 0 when I want them hidden. The easiest way to get Heroku on Termux is via npm i -g heroku. With the following script placed in ~/toggleHeroku.sh, we can start or stop our services using ~/toggleHeroku.sh start or ~/toggleHeroku.sh stop.
Script to toggle tier and visibility of Heroku services
  • Running a mock server for mobile apps: When developing mobile apps, it’s a good idea to have a backend that can serve mocks responses to the app. This allows frontend developers to focus on the client UI and logic, instead of having to worry about server readiness or network instability. What better way to do this than by having the mock server running on the very same phone?
  • Getting opinions about a WIP project: If you have a project that is still very much a work-in-progress or contains sensitive information, controlling its distribution is going to be one of your top priorities. But what if you need the opinion of another, but do not wish to lug your laptop around? One way that works for me is by cloning the project into Termux, running it, then viewing it from my phone’s browser. To convince yourself that this works, try it out with a create-react-app project:

If you’re on a tablet the bigger screen size offers more options for you to create a sensible developer experience. You could use Android’s split screen feature for Termux and your browser, and you could even install Tmux to create multiple sessions in the same Termux screen. But just to prove that features such as Hot Module Reloading would still work…

Hot module reloading for a create-react-app project using Termux and Firefox
Hot module reloading for a create-react-app project using Termux and Firefox

Going one step further with AnLinux

Whatever we have achieved so far allows you to see how your web projects behave on a mobile phone. To some extent, you could also test how things would render on a desktop if you use a browser that offers toggling to Desktop site (e.g. Firefox, Chrome). This doesn’t always work though, and mouse-related events such as :hover will simply not be testable.

If having this capability is important to you, you will be glad to hear what AnLinux has to offer. AnLinux can be downloaded from the play store here. As the app’s full name suggests, it basically allows you to Run Linux On Android Without Root Access. Under the hood, it makes use of PRoot, which is:

… a user-space implementation of chroot, mount --bind, and binfmt_misc. This means that users don’t need any privileges or setup to do things like using an arbitrary directory as the new root filesystem, making files accessible somewhere else in the filesystem hierarchy, or executing programs built for another CPU architecture transparently through QEMU user-mode.

(Left) Features in AnLinux menu that we will use, (Middle) Installation of a Linux distro and (Right) setup of a desktop GUI.

For this guide, we will be going through the setup of a Linux distro (unintuitively termed Dashboard in the image above), followed by installation of a Desktop Environment. Why 2 steps? Installing a distro server grants you a shell environment, which allows you to do sysadmin related operations. Optionally, and in our case, we wish to have a GUI to analyze how our apps respond on a Desktop. Hence we need to install a desktop environment too.

1Install Linux distro (Dashboard): I’ve only tried using Ubuntu so far, and would strongly recommend you use it as your primary distro. Copy the command from AnLinux and execute it in Termux — this should only take a minute or two. Once installation is complete, you will see a file created in ~/start-ubuntu.sh.

2Install Desktop Environment: I strongly recommend Xfce4, because when i attempted Mate, LXQt, or LXDE, the Desktop environments did not render properly. First, ensure that you execute ~/start-ubuntu.sh, which activates the Ubuntu instance in your shell. You can identify that you’re in the Ubuntu shell by observing how your beautiful Oh-My-Termux interface is now gone (you can always set it up for the Ubuntu instance later). Execute the command copied over from AnLinux. Go for coffee — the installation took approximately 23 minutes before I was prompted to select my keyboard layout. It continued running for another 15 minutes before prompting me to set a VNC password, then was finally completed.

At this point, the Ubuntu instance would have been installed with a TigerVNC server. Several VNC commands are now symbolically linked to your bin folder:

  • vncpasswd: execute this to change your VNC password.
  • vncserver-stop: shuts down the VNC server. By default, it will run vncserver -kill :1, preventing further connections to display 1.
  • vncserver-start: starts the VNC server. By default, it will run vncserver -geometry 1028x768 -depth 24 -name remote-desktop :1, permitting client connections to display 1 via port 5901. Note how the server gets run with a resolution of 1028x768. To modify this to suit your client’s screen resolution, we need to modify the vncserver-start script:
Modify vncserver-start script to match client screen resolution

Restart the VNC server by running vncserver-stop, followed by vncserver-start.

Connect with a VNC Viewer

All that remains is to connect a client to view the Ubuntu Xfce4 GUI. You can make the connection from a desktop or laptop using a client such as RealVNC over http://[phone-lan-ip]:5901. However, given that we went through these additional steps to see the GUI from our phone, download RealVNC from the play store, and make a connection to 127.0.01:5901. Input your VNC password when prompted — You should now be able to interact with your desktop GUI!

For some reason, the internet browser did not work for me until I installed Firefox by opening a terminal and executing apt install firefox. Feel free to install chrome or any other browser based on your preferences/needs.

Image of VNC viewer from Android phone, connected to TigerVNC server of Termux’s Ubuntu instance in the same phone.
Image of VNC viewer from Android phone, connected to TigerVNC server of Termux’s Ubuntu instance in the same phone.

Conclusion

We have explored how Termux, Oh-My-Termux, and AnLinux complement each other to turn your Android phone into a development machine. As you attempt to install the dependencies you need, you’re likely to encounter the occasional bump in the road due to how Termux was designed; pseudo-root access from PRoot; or lack of support for ARM architectures. Nevertheless, It allows us to do so much more with our phones, where the limitations are less because of technical challenges, but more from our lack of imagination.

--

--