Install Azure CLI on your Android Phone

I installed the Azure CLI in the Termux app on my Android phone. This post describes all the steps required to successfully run Azure CLI on most Android phones.

Installing Azure CLI on Termux on your Android phone is an alternative to using Azure Cloud Shell on Chrome or Firefox, or to using the Cloud Shell feature on the Azure mobile app. It’s also a cool thing to try.

This post is based on the excellent work done by Matthew Emes, who wrote a blog post about installing Azure CLI on a Chromebook. Matthew’s procedure got me started, but I had to modify it to make Azure CLI work in Termux on my Android phone. Also, Azure CLI has changed since Matthew wrote about it and some of his steps, while they still work, are no longer necessary.

Termux

Install Termux on your Android phone. Termux is a terminal emulator and Linux environment that runs on most Android devices with no rooting or setup required. You can use Termux as a terminal emulator to manage remote systems and it will run a large number of Linux utilities and programming languages directly on your phone. Install it from the Google Play store.

Install required packages

The Azure CLI requires the Python language, a C compiler, the make utility, and some encryption libraries to be installed. In addition, I recommend installing OpenSSH and the nano text editor.

Open Termux on your phone. Install the following packages in Termux using the built-in package manager, pkg. You may also use apt, if you prefer.

$ pkg update
$ pkg install openssl libffi python clang make
$ pkg install openssh nano

Use the Python virtual environments functionality to create a separate Python instance and directory in which Azure CLI will run, avoiding locked-down directories in Android. Install Python’s virtual environment package.

$ pip install --user virtualenv

You will see a warning stating that the virtualenv package is not in the path. Add it to the path with the following commands:

$ PATH=$PATH:~/.local/bin
$ export PATH

Make the path change permanent by creating a .bashrc file in your home directory:

$ nano ~/.bashrc

Add the path commands to the .bashrc file:

PATH=$PATH:~/.local/bin
export PATH

Save the file and exit. Read the .bashrc file:

$ source ~/.bashrc

Create a Python virtual environment for the Azure CLI scripts. This prevents Python from trying to install Azure CLI in protected directories, and failing to install.

$ virtualenv ~/.local/lib/azure-cli
$ cd ~/.local/lib/azure-cli
$ source ./bin/activate
(azure-cli) $

Install Azure CLI in the virtual environment:

(azure-cli) $ pip install cffi
(azure-cli) $ pip install azure-cli

Be patient. It takes a very long time to install the azure-cli package because it is compiling some of the dependencies. It took about twenty minutes to install on my phone.

Optionally, you may create a requirements.txt file to support upgrading Azure CLI in the future:

(azure-cli) $ pip freeze > requirements.txt

Test Azure CLI in Termux

Test the Azure CLI. Log in to your Azure subscription:

(azure-cli) $ az login

Follow the instructions displayed on the terminal. You will probably have to open the Chrome web browser on your phone and go to the URL: https://microsoft.com/devicelogin and enter in a code provided on the terminal screen. Enter the code in the browser, then return to Termux.

If the login page hangs up after you select your account, check which browser you are using. At the time I wrote this post, the device login page did not work in Firefox, so you may need to use Chrome.

After the login process is complete, run a test command. For example, list your resources in Azure. In the example below, I list resources in a resource group named test-group.

(azure-cli) $ az resource list -g test-group -o table

Bash wrapper for Azure CLI

You need to run Azure CLI from the terminal and in shell scripts. Create a bash script that runs azure CLI using the Python instance you installed in the virtual environment.

Get the path of the Python that runs in the virtual environment, so you can use it later:

(azure-cli) $ which python
/data/data/com.termux/files/home/.local/lib/azure-cli/bin/python

Copy the full path to the clipboard.

Quit the virtual environment:

(azure-cli) $ deactivate

Edit a shell script named az, which will serve as an alias for the process of running the azure-cli command in the Python virtual environment we previously created.

$ nano ~/.local/bin/az

Enter the following into the file. The last line includes the Python path you previously copied, which you can paste in.

#!/usr/bin/env bash
/data/data/com.termux/files/home/.local/lib/azure-cli/bin/python -m azure.cli "$@"

Make the command executable

$ chmod +x ~/.local/bin/az

Test the command:

$ cd ~
$ az account list -o table

You should see a list of your available Azure subscriptions.

Now, logout:

$ az logout

Enable Bash command completion for Azure CLI

It is convenient to enable bash command completion for Azure CLI commands. This is an optional configuration and you may skip it, if you wish.

The Python argcomplete package is already installed as part of Azure CLI. However, in its default configuration, it will not work for Azure CLI on Android because you do not have write access to the /etc/bash_completion.d directory. Activate Azure CLI bash command completion for your user by running the following command:

$ eval "$(~/.local/lib/azure-cli/bin/register-python-argcomplete az)"

To make it work all the time, add the command to your .bashrc file:

$ nano ~/.bashrc

In the file, add the eval command. See the last line I added in the script, below:

PATH=$PATH:~/.local/bin
export PATH
eval "$(~/.local/lib/azure-cli/bin/register-python-argcomplete az)"

Save the file and read the .bashrc file again:

$ source ~/.bashrc

Upgrading Azure CLI

You may occasionally want to upgrade Azure CLI. To upgrade, activate the virtual environment and use pip:

$ cd ~/.local/lib/azure-cli
$ source ./bin/activate
(azure-cli) $ pip install --upgrade azure-cli
(azure-cli) $ deactivate
$

Conclusion

You now have the ability to run Azure CLI commands from your Android phone. This can be useful if you build some shell scripts based on Azure CLI to accomplish repetitive tasks in Azure, or to accomplish a task that is not supported by the Azure web app.

Scroll to Top