Termux is a terminal emulator that brings some sort of "linux environment" to your android phone. It can allow you to install packages you wouldn't normally be able to install in an android phone such as python, git and nodejs. These are development tools that can aid in your development process if you do not have a laptop or a desktop. You can also use it to continue your development when you are away from your PC. Most of the essential command line tools used in linux are available in termux. You can get the app from Fdroid or from Google Playstore. I would recommend you opt for the version on Fdroid since the one available in the Playstore hasn't been updated in a long time. Using Fdroid, you will probably need to install the Fdroid app first and then search for termux terminal emulator, then install it. Run the following commands to update all packages to the latest version.
apt update && apt upgrade
or
apt update
apt upgrade
You can also run the following command just to be sure
apt install coreutils
You can then grant termux access to your storage by running the following command.
termux-setup-storage
Common termux commands
These commands are the same ones you would use in a linux bash shell with very few deviations.
ls
Lists the current directory's content.
Suppose you have a directory called app with the following files and folder;
- index.js
- index.css
- manifest.json
- modules
While at the directory app typing ls
displays;
- index.js
- index.css
- manifest.json
- modules
The command ls
is very important when you need to know what is in a certain directory.
cd
Used to change the current working directory. Typing the following code moves you to the directory app.
cd app
Please note that you must already have a directory called app in your current working directory.
mkdir
The command mkdir
is used to make a new directory.
mkdir app
The code above creates a new directory called app.
rm
Removes a file. It could be empty or with content.
rm index.html
The code above removes the file index.html from the current working directory.
rmdir
Removes a directory without content. (An empty directory).
The below code removes the directory app only if it is empty, otherwise it throws an error.
rmdir app
rm -r
Removes a directory with or without content. The following code removes the directory app.
rm -r app
cat
Displays the contents of a file. The following code displays the contents of the file index.html.
cat index.html
pwd
Prints the working directory. Just type pwd
to know the directory you are currently in.
whatis
Used to show an offline manual of a given command. The following command tells you what the command ls is.
whatis ls
man
Displays an detailed manual for a command.
man cat
The above code gives online information about the command cat
exit or logout
Used to exit the shell. Just type any of the commands exit
or logout
. You can also use ctrl + d
.
clear
Clears the screen content. Using this command gives you a clean screen.
touch
Creates a new file. The following code creates a new file called manifest.json.
touch manifest.json
echo
Displays/prints arguments to the screen. All the following codes prints Hello World.
echo Hello World
echo "Hello World"
echo "Hello World" ;
top
Displays the current running processes.
apt install <package> / pkg install <package>
Installs a package. The following commands both install vim package. Vim is a programmers text editor.
apt install vim
pkg install vim
To show the power of termux, we are going to setup a nodejs server. Installing Nodejs automatically installs npm
. To install nodejs, run the following command.
apt install nodejs
When prompted about the file taking some space, accept by typing the letter y
and pressing enter. The installation will be completed within a short period of time. When done, confirm that nodejs and npm are installed by checking the versions as below.
node -v
or
node --version
and
npm -v
or
npm --version
By the time I was installing, nodejs version available in the apt repository was 20.2.0
while npm was version 9.6.6
A simple server in Nodejs
Create a file called server.js
I recommend installing vim which is a programmers text editor. Install it by running the following command.
apt install vim
To create or open a file in vim use;
vim filename
In our case using vim server.js
creates a new file called server.js. To edit (insert content to) the file type the letter i
.
After creating the file, copy the following simple code for a server.
const http= require ('http');
http.createServer((request, response) => {
response.writeHead(200, {
});
response.write('I just created my first server in Nodejs \n');
response.end();
}).listen(1337);
With vim, to save a file you press Esc
then type :wq
.
You can also use the following;
:wq - To save and Quit
:q -To quit without saving
You can now run the following code to activate your server.
node server.js
If everything worked fine, you can now open your browser at localhost:1337
. To stop the server in Termux use Ctrl + C
.