Part 2: Working with Termux, Virtualenv and Git on Android

In the first part we detailed how to install Heroku’s CLI on Android using Termux. In this second part we will see how to use Termux to work efficiently with Git and Virtualenv.

Virtualenv is a Linux package to help separate system-wide Python packages from our app’s specific packages used. However, creating a virtual environment is not possible outside Termux’s default folder because symlinks creations are denied. Moreover, Termux’ folder is not accessible through Android’s file manager and therefore any program we would use to access and modify files in our app. So we’ll have to develop in an accessible folder on Android using a text editor and git client to commit changes and then use git pull everything to our current project within Termux and vice versa.

Let’s allow Termux to access the phone’s storage, it will ease our workflow explained at the end of this part. In Termux type:

termux-setup-storage

It will popup a dialog box to allow the app to browse the phone’s files outside Termux.
Or go into Settings -> Apps, select Termux, then App permissions and allow Storage.
To have Termux startup at your desired location, edit /data/data/com.termux/files/usr/etc/bash.bashrc:

nano /data/data/com.termux/files/usr/etc/bash.bashrc

And append at the end of your file:

cd /name/of/your/projects/folder

Let’s start building up an app with Heroku. Install python (Python 3 is the default version) and git:

apt-get install python
apt-get install git

Create a folder for you project:

mkdir project_folder
cd project_folder

Install a virtual environment:

pip install virtualenv
virtualenv venv -p python
source venv/bin/activate

Here, the app can be created either using Heroku CLI (Heroku’s Command Line Interface) or in the Heroku’s dashboard and then cloning the app using git.

Using the command line interface
In Termux, create a new folder for the application.

mkdir name_of_app
cd name_of_app

Create the app:

heroku create name_of_app

The app creation should be confirmed and returned with the git remote url.
Initialise the Git repository inside Termux and add the git remote to your Heroku Git address you should:

git init
heroku git:remote -a name_of_app

If you need to rename the app:

heroku apps:rename another_name

You’ll also need to rename the git remote name:

heroku git:remote -a another_name

Change to the app’s folder:

cd name_of_app
nano runtime.txt

Create a requirements.txt file and add python-3.7.4

nano requirements.txt

Add and commit the files and push to Heroku:


git add .
git commit -am “it’s a start”
git push heroku master

Using Heroku’s dashboard

Got to dashboard.heroku.com and create a new app. Add Python to your project, under settings tap Add buildpack and choose Python or enter: heroku/python.

Under Termux, you will initialise the repository, clone and add the remote git for the app created on Heroku:

git init
heroku git:remote -a name_of_app

From then on, add new files, commit and push to Heroku like shown for the CLI:

git add .
git commit -m “new commit”
git push heroku master

If the git push fails and the following error appears:

Your local git repository has more than 1 app referenced in git remotes.

Run the command below to remove one of the git remotes listed in the error:

git remote rm one_of_the_remote_git

Once your app git is setup within Termux, copy the folder to the desired location on your phone, that is accessible through a regular file browser. I created a folder named projects within the phone’s internal storage:

mkdir /storage/emulated/0/projects
cp -r name_of_app /storage/emulated/0/projects/

Add the remote git folder accessible on your phone through a file browser, I chose local as a name:

git remote add local /storage/emulated/0/projects/name_of_app

Now you can pull and push as such:

git pull local
git push local

Get PocketGit on the PlayStore.
In Clone URL put the path to your local project accessible through your Android’s file manager. If you followed our folder path then put:

/storage/emulated/0/projects/name_of_app

Otherwise, use pwd to copy the path to the project folder.
In Local Path choose the same path.
Save, and tap on the project, you should be browsing your project folder.
Editing is now simple, explore the files using PocketGit and tap on any file to make changes in your preferred editor (I use the pro version for DroidEdit), save the file and commit with git. You can then pull the changes from Termux using:

git pull local

Using programming for marketing and work efficiency

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store