Install Azure CLI on Ubuntu

This documentation is based on the official Azure documentation.

Install a few pre-requisites:

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

Download the key for the Microsoft archive:

mkdir -p /etc/apt/keyrings
curl -sL |
   gpg --dearmor |
      sudo tee /etc/apt/keyrings/microsoft.gpg > /dev/null

Add the repository to the sources list:

SUITE=$(lsb_release -cs)
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/microsoft.gpg] $SUITE main" |
    sudo tee /etc/apt/sources.list.d/microsoft.list

Pin a few rules to ensure that the Azure CLI is fetched only from Microsoft’s archive:

cat << EOF | sudo tee /etc/apt/preferences.d/99-microsoft

Never prefer packages from the Microsoft repository:

Package: *
Pin: origin
Pin-Priority: 1

...except if it is the Azure CLI:

Package: azure-cli
Pin: origin
Pin-Priority: 500

Finally, install the CLI:

sudo apt-get update && \
    sudo apt-get install -y azure-cli