How to install Azure CLI on Linux
How to Install Azure CLI on Linux
The Azure Command-Line Interface (CLI) is a powerful cross-platform tool that enables developers, system administrators, and DevOps professionals to manage Azure resources directly from the command line. This comprehensive guide will walk you through multiple methods to install Azure CLI on various Linux distributions, from basic installation to advanced configuration and troubleshooting.
Table of Contents
- [What is Azure CLI](#what-is-azure-cli)
- [Prerequisites and System Requirements](#prerequisites-and-system-requirements)
- [Installation Methods Overview](#installation-methods-overview)
- [Method 1: Using the Official Installation Script](#method-1-using-the-official-installation-script)
- [Method 2: Package Manager Installation](#method-2-package-manager-installation)
- [Method 3: Manual Installation](#method-3-manual-installation)
- [Method 4: Docker Container Installation](#method-4-docker-container-installation)
- [Post-Installation Configuration](#post-installation-configuration)
- [Verifying Your Installation](#verifying-your-installation)
- [Common Issues and Troubleshooting](#common-issues-and-troubleshooting)
- [Best Practices and Tips](#best-practices-and-tips)
- [Updating and Maintaining Azure CLI](#updating-and-maintaining-azure-cli)
- [Conclusion](#conclusion)
What is Azure CLI
Azure CLI is Microsoft's official command-line tool for managing Azure resources. It provides a consistent command-line experience across Windows, macOS, and Linux platforms. With Azure CLI, you can:
- Create, configure, and manage Azure resources
- Automate deployment and management tasks
- Integrate Azure operations into scripts and CI/CD pipelines
- Access Azure services without using the web portal
- Perform bulk operations efficiently
The tool supports all Azure services and provides both interactive and scripted usage patterns, making it an essential tool for cloud professionals working with Microsoft Azure.
Prerequisites and System Requirements
Before installing Azure CLI on your Linux system, ensure you meet the following requirements:
System Requirements
- Operating System: Any modern Linux distribution (64-bit)
- Python: Python 3.6 or later (for some installation methods)
- Memory: Minimum 512 MB RAM
- Disk Space: At least 1 GB of free disk space
- Network: Internet connection for downloading packages and accessing Azure services
Supported Linux Distributions
Azure CLI officially supports the following Linux distributions:
- Ubuntu: 18.04 LTS, 20.04 LTS, 22.04 LTS
- Debian: 9, 10, 11
- CentOS: 7, 8
- Red Hat Enterprise Linux (RHEL): 7, 8, 9
- SUSE Linux Enterprise Server (SLES): 12, 15
- openSUSE: 15
- Fedora: 35, 36, 37
- Amazon Linux: 2
- Oracle Linux: 7, 8
Required Permissions
- Root or sudo access: Required for system-wide installation
- User permissions: Sufficient for user-specific installations using some methods
Pre-Installation Checks
Before proceeding, run these commands to verify your system:
```bash
Check Linux distribution and version
cat /etc/os-release
Check Python version (if applicable)
python3 --version
Check available disk space
df -h
Verify internet connectivity
ping -c 3 google.com
```
Installation Methods Overview
Azure CLI can be installed on Linux using several methods, each with its own advantages:
1. Official Installation Script: Simplest method, automatically detects your distribution
2. Package Manager: Uses your distribution's native package manager
3. Manual Installation: Direct download and installation of packages
4. Docker Container: Runs Azure CLI in an isolated container environment
Choose the method that best fits your environment and security requirements.
Method 1: Using the Official Installation Script
The official installation script is the recommended method for most users as it automatically detects your Linux distribution and installs the appropriate packages.
Step 1: Download and Execute the Installation Script
```bash
Download and run the installation script
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
```
Step 2: Alternative Manual Script Execution
If you prefer to review the script before execution:
```bash
Download the script first
curl -sL https://aka.ms/InstallAzureCLIDeb -o install_azure_cli.sh
Review the script content
cat install_azure_cli.sh
Make it executable and run
chmod +x install_azure_cli.sh
sudo ./install_azure_cli.sh
```
What the Script Does
The installation script performs the following actions:
1. Detects your Linux distribution and version
2. Updates package repositories
3. Installs required dependencies
4. Adds Microsoft's package repository
5. Installs Azure CLI package
6. Configures the installation
Advantages of This Method
- Simplicity: One-command installation
- Automatic detection: Works across different distributions
- Always current: Downloads the latest version
- Microsoft supported: Official installation method
Potential Issues
- Requires internet access: Cannot work in offline environments
- Sudo privileges needed: Must run with administrative rights
- Limited customization: Less control over the installation process
Method 2: Package Manager Installation
This method uses your Linux distribution's native package manager, providing better integration with your system's package management.
Ubuntu/Debian Installation
Step 1: Update Package Index
```bash
sudo apt-get update
```
Step 2: Install Prerequisites
```bash
sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg
```
Step 3: Add Microsoft Signing Key
```bash
Download and install the Microsoft GPG key
curl -sL https://packages.microsoft.com/keys/microsoft.asc |
gpg --dearmor |
sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null
```
Step 4: Add Azure CLI Repository
```bash
Get the distribution codename
AZ_REPO=$(lsb_release -cs)
Add the Azure CLI repository
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" |
sudo tee /etc/apt/sources.list.d/azure-cli.list
```
Step 5: Install Azure CLI
```bash
Update package index with new repository
sudo apt-get update
Install Azure CLI
sudo apt-get install azure-cli
```
CentOS/RHEL/Fedora Installation
Step 1: Import Microsoft Repository Key
```bash
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
```
Step 2: Add Azure CLI Repository
For CentOS/RHEL:
```bash
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
```
For Fedora:
```bash
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
```
Step 3: Install Azure CLI
For CentOS/RHEL:
```bash
sudo yum install azure-cli
```
For Fedora:
```bash
sudo dnf install azure-cli
```
SUSE/openSUSE Installation
Step 1: Import Microsoft Repository Key
```bash
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
```
Step 2: Add Repository
```bash
sudo zypper addrepo --name 'Azure CLI' --check https://packages.microsoft.com/yumrepos/azure-cli azure-cli
```
Step 3: Install Azure CLI
```bash
sudo zypper install --from azure-cli azure-cli
```
Method 3: Manual Installation
Manual installation gives you complete control over the process and is useful in restricted environments.
Step 1: Download the Package
Visit the [Azure CLI releases page](https://github.com/Azure/azure-cli/releases) and download the appropriate package for your distribution.
For Ubuntu/Debian:
```bash
Download the latest .deb package
wget https://packages.microsoft.com/repos/azure-cli/pool/main/a/azure-cli/azure-cli_2.53.0-1~jammy_all.deb
```
For CentOS/RHEL/Fedora:
```bash
Download the latest .rpm package
wget https://packages.microsoft.com/yumrepos/azure-cli/azure-cli-2.53.0-1.el7.x86_64.rpm
```
Step 2: Install Dependencies
Ensure all required dependencies are installed:
```bash
For Ubuntu/Debian
sudo apt-get install python3-pip python3-venv
For CentOS/RHEL/Fedora
sudo yum install python3-pip python3-virtualenv
```
Step 3: Install the Package
For Ubuntu/Debian:
```bash
sudo dpkg -i azure-cli_2.53.0-1~jammy_all.deb
sudo apt-get install -f # Fix any dependency issues
```
For CentOS/RHEL/Fedora:
```bash
sudo rpm -ivh azure-cli-2.53.0-1.el7.x86_64.rpm
```
Method 4: Docker Container Installation
Running Azure CLI in a Docker container provides isolation and consistency across different environments.
Step 1: Install Docker
Ensure Docker is installed on your system:
```bash
Check if Docker is installed
docker --version
If not installed, install Docker (Ubuntu example)
sudo apt-get update
sudo apt-get install docker.io
sudo systemctl start docker
sudo systemctl enable docker
```
Step 2: Pull Azure CLI Docker Image
```bash
Pull the official Azure CLI image
docker pull mcr.microsoft.com/azure-cli:latest
```
Step 3: Run Azure CLI Container
```bash
Run Azure CLI interactively
docker run -it mcr.microsoft.com/azure-cli:latest
Run a specific Azure CLI command
docker run --rm mcr.microsoft.com/azure-cli:latest az --version
Mount local directory for persistent storage
docker run -it -v ~/.azure:/root/.azure mcr.microsoft.com/azure-cli:latest
```
Creating an Alias for Convenience
```bash
Add this to your ~/.bashrc or ~/.zshrc
alias az='docker run --rm -it -v ~/.azure:/root/.azure -v $(pwd):/workspace -w /workspace mcr.microsoft.com/azure-cli:latest az'
Reload your shell configuration
source ~/.bashrc
```
Post-Installation Configuration
After installing Azure CLI, you'll need to configure it for your Azure environment.
Initial Authentication
```bash
Interactive login (opens web browser)
az login
Login with service principal
az login --service-principal -u -p --tenant
Login with managed identity (on Azure VMs)
az login --identity
```
Setting Default Subscription
```bash
List available subscriptions
az account list --output table
Set default subscription
az account set --subscription "Your Subscription Name"
Verify current subscription
az account show
```
Configuring Default Settings
```bash
Set default output format
az configure --defaults output=table
Set default location
az configure --defaults location=eastus
Set default resource group
az configure --defaults group=myResourceGroup
```
Configuration File Location
Azure CLI stores configuration in:
- Linux: `~/.azure/`
- Configuration file: `~/.azure/config`
- Credentials: `~/.azure/azureProfile.json`
Verifying Your Installation
After installation, verify that Azure CLI is working correctly:
Basic Verification
```bash
Check Azure CLI version
az --version
Verify installation components
az --help
Test connectivity to Azure
az account list-locations --output table
```
Extended Verification
```bash
Check available extensions
az extension list-available --output table
Verify authentication status
az account show
Test a simple Azure operation
az group list --output table
```
Expected Output
A successful installation should display version information similar to:
```
azure-cli 2.53.0
core 2.53.0
telemetry 1.1.0
Dependencies:
msal 1.20.0
azure-mgmt-resource 22.0.0
Python location '/opt/az/bin/python3'
Extensions directory '/home/user/.azure/cliextensions'
```
Common Issues and Troubleshooting
Issue 1: Permission Denied Errors
Problem: Installation fails with permission denied errors.
Solution:
```bash
Ensure you have sudo privileges
sudo -v
Check if user is in sudo group
groups $USER
Add user to sudo group if needed
sudo usermod -aG sudo $USER
```
Issue 2: Package Repository Errors
Problem: Cannot access Microsoft package repositories.
Solution:
```bash
Update package repositories
sudo apt-get update
Clear package cache
sudo apt-get clean
Verify internet connectivity
curl -I https://packages.microsoft.com
Check DNS resolution
nslookup packages.microsoft.com
```
Issue 3: Python Version Conflicts
Problem: Azure CLI installation fails due to Python version issues.
Solution:
```bash
Check Python version
python3 --version
Install specific Python version (Ubuntu example)
sudo apt-get install python3.8 python3.8-venv python3.8-pip
Update alternatives
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
```
Issue 4: SSL Certificate Errors
Problem: SSL certificate verification failures during installation.
Solution:
```bash
Update CA certificates
sudo apt-get update && sudo apt-get install ca-certificates
For CentOS/RHEL
sudo yum update ca-certificates
Verify system time (SSL certificates are time-sensitive)
timedatectl status
Sync system time if needed
sudo ntpdate -s time.nist.gov
```
Issue 5: Command Not Found After Installation
Problem: `az` command not found after successful installation.
Solution:
```bash
Check if Azure CLI is in PATH
which az
Add to PATH if needed
echo 'export PATH=$PATH:/usr/local/bin' >> ~/.bashrc
source ~/.bashrc
Verify installation location
find /usr -name "az" 2>/dev/null
```
Issue 6: Authentication Failures
Problem: Cannot authenticate with Azure after installation.
Solution:
```bash
Clear existing authentication
az account clear
Login interactively
az login --use-device-code
Check firewall/proxy settings
export HTTP_PROXY=http://proxy.company.com:8080
export HTTPS_PROXY=http://proxy.company.com:8080
```
Best Practices and Tips
Security Best Practices
1. Use Service Principals: For automated scripts and CI/CD pipelines
```bash
Create service principal
az ad sp create-for-rbac --name myServicePrincipal --role contributor
```
2. Enable Multi-Factor Authentication: When possible
```bash
Login with device code for MFA
az login --use-device-code
```
3. Regularly Update: Keep Azure CLI updated
```bash
Check for updates
az upgrade
```
Performance Optimization
1. Use Output Formats Efficiently:
```bash
Use JSON for scripting
az vm list --output json | jq '.[] | .name'
Use table for human reading
az vm list --output table
```
2. Leverage Parallel Execution:
```bash
Use --no-wait for long-running operations
az vm start --ids $(az vm list --query "[].id" -o tsv) --no-wait
```
3. Configure Default Values:
```bash
Set commonly used defaults
az configure --defaults location=eastus group=myRG
```
Scripting Best Practices
1. Error Handling:
```bash
#!/bin/bash
set -e # Exit on any error
if ! az account show &>/dev/null; then
echo "Please login to Azure CLI first"
az login
fi
```
2. Use Variables:
```bash
RESOURCE_GROUP="myResourceGroup"
LOCATION="eastus"
az group create --name $RESOURCE_GROUP --location $LOCATION
```
3. Implement Logging:
```bash
Enable debug logging
az vm list --debug > debug.log 2>&1
```
Maintenance Tips
1. Regular Updates:
```bash
Update Azure CLI
az upgrade
Update extensions
az extension update --name
```
2. Clean Up:
```bash
Clear CLI cache
az cache purge
Remove unused extensions
az extension remove --name
```
3. Backup Configuration:
```bash
Backup Azure CLI configuration
cp -r ~/.azure ~/.azure.backup
```
Updating and Maintaining Azure CLI
Automatic Updates
Enable automatic updates for the best experience:
```bash
Check current version
az --version
Upgrade to latest version
az upgrade
Enable auto-upgrade (if available)
az config set auto-upgrade.enable=yes
```
Manual Updates
For package manager installations:
```bash
Ubuntu/Debian
sudo apt-get update && sudo apt-get upgrade azure-cli
CentOS/RHEL
sudo yum update azure-cli
Fedora
sudo dnf update azure-cli
```
Extension Management
```bash
List installed extensions
az extension list
Update all extensions
az extension update --name
Remove unused extensions
az extension remove --name
```
Monitoring and Logging
```bash
Enable logging
export AZURE_CLI_LOGGING_ENABLE=true
Set log level
az configure --defaults logging.enable_log_file=true
```
Conclusion
Installing Azure CLI on Linux is a straightforward process with multiple methods available to suit different environments and requirements. The official installation script provides the simplest approach for most users, while package manager installations offer better system integration. Docker containers provide isolation and consistency, making them ideal for development and CI/CD environments.
Key takeaways from this guide:
1. Choose the right method: Select the installation method that best fits your environment and security requirements
2. Verify your installation: Always test your installation thoroughly before using it in production
3. Follow security best practices: Use service principals for automation and keep your installation updated
4. Leverage troubleshooting resources: Common issues have well-documented solutions
5. Maintain your installation: Regular updates and proper configuration ensure optimal performance
With Azure CLI properly installed and configured, you'll have a powerful tool for managing your Azure resources efficiently from the command line. Whether you're automating deployments, managing resources, or integrating with CI/CD pipelines, Azure CLI provides the flexibility and power needed for modern cloud operations.
Remember to consult the official [Azure CLI documentation](https://docs.microsoft.com/en-us/cli/azure/) for the most up-to-date information and advanced usage scenarios. The Azure CLI community and Microsoft support channels are also valuable resources for getting help with specific issues or advanced configurations.