Install Azure CLI 2.0 on Ubuntu

Azure CLI or Azure command line interface is a cross platform command line tool offered by Microsoft to work with Microsoft Azure and manage azure resources. One can use it in the browser (in the azure cloud shell) or it can also be installed or major Operating Systems of one’s choice. Azure CLI 2.0 is optimized for managing and administering Azure resources from the command line, and for building automation scripts that work against the Azure Resource Manager.

Do note that azure is the prefix for old CLI – Azure CLI (i.e. version 1.0) , and that az is the prefix for the new CLI – Azure CLI 2.0.

In this blog post, we’ll learn how to install Azure CLI 2.0 on Ubuntu machine.

First we need to get the packages required for the installation:

sudo apt-get update
sudo apt-get install curl apt-transport-https lsb-release gnupg

After this, we need to download and install the signing key from Microsoft:

curl -sL https://packages.microsoft.com/keys/microsoft.asc | \
    gpg --dearmor | \
    sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null

Get microsoft signing key

Now we need to add Microsoft repo to the source repo list using below command:

AZ_REPO=$(lsb_release -cs)
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list
Add Microsoft repo to the source repo list
Add Microsoft repo to the source repo list

Finally, install the Azure CLI package:

sudo apt-get install apt-transport-https
sudo apt-get update && sudo apt-get install azure-cli

Install Azure CLI by using apt command

To check if Azure CLI is successfully installed, we can run:

az --version

which should return output like below:

Check version of Azure CLI installed
Check version of Azure CLI installed

We can then login into Azure by using below command:

az login
Login into Azure by using Azure CLI
Login into Azure by using Azure CLI

We then need to follow instructions specified. On successful completion, it will return the list of subscriptions one has access to in a nice JSON format.

We can also specify the subscription that we want to work with by using below command:

az account set --subscription="SUBSCRIPTION_ID"

Remember to not add any spaces before or after = sign in this command.

A full copy of source code used in this blog post can be found in this GitHub Repository under branches blog/7562 and master.

5 thoughts on “Install Azure CLI 2.0 on Ubuntu

  1. root@****# apt-get install apt-transport-https
    E: Type ‘“deb’ is not known on line 1 in source list /etc/apt/sources.list.d/azure-cli.list
    E: The list of sources could not be read.
    E: Type ‘“deb’ is not known on line 1 in source list /etc/apt/sources.list.d/azure-cli.list
    E: The list of sources could not be read.

    Like

  2. You should certainly verify and double check what you are posting before publishing rubbish like this. After running what you posted here I started getting the below all the time when trying to install anything in Ubuntu:
    E: Type ‘“deb’ is not known on line 1 in source list /etc/apt/sources.list.d/azure-cli.list
    E: The list of sources could not be read.
    E: Type ‘“deb’ is not known on line 1 in source list /etc/apt/sources.list.d/azure-cli.list
    E: The list of sources could not be read.
    I had to to waste my time troubleshooting the problem and deleting the file.

    Like

    • Carefully looking at the error also looks like it is a formatting issue. Copy the command from post into a notepad and then re-write double quotes. It should work then. All better if you feel like typing the command on terminal. That would eliminate all formatting issues. Would look forward to how it goes.

      Like

Leave a comment