How to Install Azure CLI: A Complete Guide

Learn All the Ways to Install Azure CLI on Ubuntu, Windows, macOS, and RedHat

How to Install Azure CLI: A Complete Guide

Azure CLI provided by Microsoft is a cross-platform command-line tool for managing Azure resources and automating cloud resource deployment. The tool enables you to query, create and manage the cloud resources using the terminal.

Azure CLI is supported across multiple operating systems, including Windows, macOS, Linux, and most recently, on a web browser. In this article, you'll see how you can install the CLI tool and get started, and automate the management of your cloud environment.

Table of Content

  1. Prerequisites
  2. Using Azure Cloud Shell in Azure Portal
  3. Installing Azure CLI on Windows
  4. Installing Azure CLI on Linux
  5. Installing Azure CLI on macOS
  6. Running Azure CLI as a Docker Container
  7. Conclusion
  8. Reference

Prerequisites

There are no prerequisites for this article; however, it's good to have an active Azure Subscription to deploy resources to use Cloud Shell.

Using Azure Cloud Shell in Azure Portal

If you need to use the Azure CLI commands for temporary or interim access, you can use Azure Cloud Shell, which comes pre-installed with fully functional Azure CLI commands and other relevant tools necessary to run the queries.

To access the Cloud Shell for your account, follow the steps below:

  • Log in to your Azure Account with a valid subscription. (https://portal.azure.com)
  • Navigate to shell.azure.com that opens the browser session for the Azure Cloud Shell in a separate tab. This gives you an entire webpage dedicated to Cloud Shell instead of opening a partial window with Azure Portal GUI view.
  • If it's your first time logging on to Cloud Shell, select Bash as your default shell:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c6e44655-e13a-412f-a20b-c8cec8bf447e/02-shellchoice.png
Cloud Shell Terminal Selection
  • Choose the subscription you want to use to store the cloud shell session profile. If you already have a storage account that you would like to use instead, choose Show advanced settings to point to the current storage account resource.
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/19c5d77d-35b7-4ac4-ac9e-e44b28e18040/04-selectsub.png
Cloud Shell Storage
  • Fill out the dropdown by selecting the location of your current storage account and the resource group. Enter cloudshell as the new File Share name to create a file share container at your designated location.
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e3fd2f96-07da-4d5c-aca0-d2a62e83c7a8/05-advanced-shell.png
Cloud Shell File Share
  • Click Create storage to finish the storage mount process.
  • Once the storage is ready, cloud shell will spawn and prompt a bash terminal for usage as shown below:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/1ee9587f-8690-4b50-a85b-41a197ca7a14/06-cli-login.png
Cloud Shell Azure CLI Version
  • Run az --version to validate the module's installation from the terminal.

Installing Azure CLI on Windows

The Azure CLI for Windows offers multiple different ways of installation. Regardless of which option you prefer to choose, you get the ability to query and run CLI commands using PowerShell and Command Prompt(CMD) for resource management.

Installing Python for Windows

Since Azure CLI is written in Python, it needs a python interpreter to execute the commands.

Use the link below to download the latest Python executable and install it on your windows machine. Be sure to Add Python to PATH while installing the application:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0e4ab909-7e23-4e55-9323-e262630475c9/01-PythonPath.png
Python Installation Path
Download Python
The official home of the Python Programming Language

Installing Azure CLI using MSI GUI Installer

If you are comfortable with the traditional way of installing the software using an executable package, you can use the MSI installer for Azure CLI to install the modules.

  • Use the following URL to download the latest CLI installer: Download Azure CLI
  • Run the installer, accept the terms in the License Agreement using the checkbox, and click Install:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/7dda00c7-e86f-4098-b51b-763213edf597/07-azmsi-install.png
Azure CLI MSI Installation
  • Once the installation is complete, go to the Start Menu and search for powershell and click Open:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/303231e1-9b08-4053-b2ad-ab683fe208ec/08-powershell-launch.png
PowerShell Start Menu
  • Type az --version and press Enter. The version number and the relevant information confirms that the Azure CLI is installed:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/bc78e0ae-70c9-4e4b-9c52-ae8e373321ac/09-cli-powershell.png
PowerShell Azure CLI Results

Installing Azure CLI using MSI Installer with PowerShell

If you chose to use PowerShell command instead, follow the steps below to install the Azure CLI using PowerShell command:

  • Go to the Windows Search bar and enter powershell.
  • This time, click on Run as administrator when opening the PowerShell:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5bd18419-69a7-42eb-98c1-5000223aa4dc/10-ps-runadmin.png
PowerShell Admin Mode Launch
  • Paste the following command in the PowerShell terminal:
Invoke-WebRequest -Uri <https://aka.ms/installazurecliwindows> -OutFile .\\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\\AzureCLI.msi
  • The command above will download and install the latest version of the Azure CLI. If you already have a version installed, the installer will update the existing version.
  • After the installation is complete, reopen the PowerShell and run the az --version command to validate the Azure CLI installation.
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/bc78e0ae-70c9-4e4b-9c52-ae8e373321ac/09-cli-powershell.png
PowerShell Azure CLI Output

Installing Azure CLI using Chocolatey

The popular Windows Package Manager, Chocolatey, also supports the installation of the Azure CLI module. So whether you are trying to incorporate the installation into your image baseline code or distributing the package to multiple endpoints, you can do so using the choco commands.

Follow the steps below to install Azure CLI using Chocolatey:

  • If you don't have chocolatey installed already, run the following script in an elevated (Administrator) PowerShell session to install the tool on your Windows machine:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('<https://chocolatey.org/install.ps1>'))
Chocolatey Install
  • Once installed, reopen the PowerShell in Admin mode and run the following command to install the Azure CLI package:
choco install azure-cli
Choco Install Azure CLI
  • Confirm the installation by typing Y:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b01ce815-bcae-40f7-a5d6-60ca6e77ec9f/11-choco-azcli.png
Chocolatey CLI Package Install 
  • Once the installation is complete, the output confirms the installation of the CLI module. At this point, you need to reopen the PowerShell instance to start using the Azure CLI commands.
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5afcbc31-3130-4243-adf0-9ead270479e9/12-choco-result.png
Choco Result Output

Installing Azure CLI on Linux

The versatility of Azure CLI enables the usage across multiple operating systems. In addition, it supports the majority of the Linux flavors for install and use. In the following section, you'll discover how you can install the CLI tool on Ubuntu and other major Linux OS.

Installing Azure CLI on Debian and Ubuntu Server

Ubuntu and Debian Linux has their package distribution management system that assists in delivering the application to the operating system. The process to install the tools involves adding Microsoft's package source as a trusted repo and finally using the apt package manager to pull the package on the system.

  • If you prefer to use one-liner for the install, run the following command in the terminal:
curl -sL <https://aka.ms/InstallAzureCLIDeb> | sudo bash
CLI Install using Single Script

Ensure that you have sudo permission before running the command.

Follow the steps below to install Azure CLI on Ubuntu and Debian OS using the apt package manager:

  • Launch the Terminal on your ubuntu system and run the following command to perform the package updates:
sudo apt-get update
apt Package Update
sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg
Dependencies Install
  • The next step involves adding the Microsoft package manager signing keys to the system trusted root:
curl -sL <https://packages.microsoft.com/keys/microsoft.asc> |
    gpg --dearmor |
    sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null
Adding Microsoft Singing Keys
  • You need to add the CLI repo as a source in the /etc/apt/source.list.d folder using the following commands:
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
Adding Microsoft Package Repo
  • Finally, run the following command to update the apt package sources and install the CLI tool:
sudo apt-get update
sudo apt-get install azure-cli
Installing CLI on Ubuntu
  • Validate the installation by running the az --version in the terminal:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/fb3bf297-6695-4808-9f16-f02f9a8585a6/13-ubuntu-cli.png
CLI Version on Ubuntu
  • Python package is installed on most of the Linux distribution. However, if you get any python package error, run the following command to install Python:
sudo apt-get update
sudo apt-get install python
Installing Python on Ubuntu

Installing Azure CLI on RedHat, CentOS, or Fedora

Fortunately, if you are used to the RedHat or CentOS version of Linux, Microsoft provides the CLI package on both the dnf and yum package manager library.

Regardless of which operating system you have, follow the steps below to add the Microsoft package repository:

  • Run the following command in the terminal to import Microsoft repository keys:
sudo rpm --import <https://packages.microsoft.com/keys/microsoft.asc>
Adding Microsoft Package Repo
  • Run the following command to add Microsoft package repo as source:
echo -e "[azure-cli]
name=Azure CLI
baseurl=https://packages.microsoft.com/yumrepos/azure-cli
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc" | sudo tee /etc/yum.repos.d/azure-cli.repo
Adding Package Manager Keys
  • On CentOS, since you have yum package manager, run the following command to install Azure CLI tools:
sudo yum install azure-cli
Installing CLI using YUM Package Manager

The installation will pull other necessary packages for CLI to work as well.

  • Alternatively, use the following command if you have dnf package manager on your system:
sudo dnf install azure-cli
Installing CLI using DNF

Installing Azure CLI using Install Script

Although using the package manager is always the recommended method, there might be a case when you don't have access to the package manager directly for any reason and would like to use a URL directly to install the CLI tools. It is worth mentioning that you need to install the following packages before running the script since these modules are required for the script to execute successfully and complete the installation.

Once the modules mentioned above are installed, run the following script in your terminal to complete the installation:

curl -L <https://aka.ms/InstallAzureCli> | bash
Installing CLI using Bash Script

Installing Azure CLI on macOS

MacOS comes with Homebrew preinstalled, which is the best method to install the Azure CLI tool on Mac devices. Homebrew is a package managed for macOS, which is similar to apt in Linux OS. If you don't have Homebrew installed, follow the instructions below to set up the Homebrew and install the CLI tool:

  • Open your Apple Terminal by pressing Command + spacebar on your device and type Terminal:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6c1eeebc-8098-4387-9d87-bb22dbb48598/14-macos-terminal.png
macOS Search Terminal
  • Paste the following command in the terminal and press Enter:
/bin/bash -c "$(curl -fsSL <https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh>)"
Installing Homebrew on macOS
  • Once the installation is complete, run the following command to perform a package update on brew:
brew update
Updating Homebrew
  • Finally, run the last command to install the Azure CLI tool:
brew install azure-cli
Installing CLI using Homebrew

Python and other necessary packages will be installed automatically while installing the CLI tool.

Running Azure CLI as a Docker Container

If building custom images using Docker is your only option, luckily, there's a managed Azure CLI Docker image provided by Microsoft on their open container registry that you can pull down. You can use the image directly with Docker to run an isolated container or use it as the base for your custom image.

Follow the instructions below to run Azure CLI as a Docker Container.

  • If you don't already have Docker installed, follow the instruction to install Docker on Windows or Linux operating system.
  • Run the following command in your terminal to verify the Docker installation:
docker --version
Checking Docker Version
  • Run the following command to install the CLI using docker run:
docker run -it mcr.microsoft.com/azure-cli
Running CLI in Docker Container

This will place you directly inside the container to start using right away to connect to Azure and manage your cloud environment.

Conclusion

Now that you have installed the Azure CLI, you will start interacting with your cloud resources using the command line.

Keep an eye on the upcoming article in the series on using Azure CLI!

Reference