Azure / azure-cli

Azure Command-Line Interface
MIT License
3.92k stars 2.88k forks source link

[Doc] Add doc for installing Azure CLI with pip #20476

Open jiasli opened 2 years ago

jiasli commented 2 years ago

There are platforms or architectures where we don't officially distribute native packages:

The solution is to install azure-cli from PyPI: https://pypi.org/project/azure-cli/

pip install azure-cli

We do have the doc https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=script which is internally using pip to install azure-cli. It calls https://github.com/Azure/azure-cli/blob/dev/scripts/curl_install_pypi/install which then calls https://github.com/Azure/azure-cli/blob/dev/scripts/curl_install_pypi/install.py.

However, this method is

  1. Too complex/automated and leaves users with very few customization options
  2. Interactive, which is not suitable for automated installation: image
  3. Not actively maintained. For example, it support Ubuntu up to 18.04: https://github.com/Azure/azure-cli/blob/50ccb24528eb36529d1432808f3ff385cc4c04aa/scripts/curl_install_pypi/install.py#L366-L367
jiasli commented 2 years ago

[draft version]

Install Azure CLI with pip

Azure CLI is built on Python. The supported Python versions are 3.7 ~ 3.10.

Directly running pip install azure-cli installs Azure CLI system-wide and may cause conflicts with other Python libraries. Instead, we recommend installing Azure CLI in a virtual environment.

Windows

Install

First install Python from https://www.python.org/downloads/windows/.

Then launch a PowerShell terminal and run:

# Create a virtual environment
python -m venv azure-cli-env

# Update pip
azure-cli-env\Scripts\python.exe -m pip install --upgrade pip

# Install azure-cli
azure-cli-env\Scripts\python.exe -m pip install azure-cli

# Run any Azure CLI commands
azure-cli-env\Scripts\az --version

Uninstall

Delete the virtual environment:

Remove-Item -Force -Recurse azure-cli-env

Linux/MacOS

Install

First make sure python3 and its related packages are installed:

# Ubuntu/Debian
sudo apt install python3 python3-venv --yes

# Fedora/CentOS/RHEL
sudo dnf install python3 -y

# Homebrew
brew install python

Then continue the installation:

# Create a virtual environment
python3 -m venv azure-cli-env

# Update pip
azure-cli-env/bin/python -m pip install --upgrade pip

# Install azure-cli
azure-cli-env/bin/python -m pip install azure-cli

# Run any Azure CLI commands
azure-cli-env/bin/az --version

Uninstall

Delete the virtual environment:

rm -rf azure-cli-env

Reference

dbradish-microsoft commented 2 years ago

@jiasli , where is this information ultimately going to reside?

jiasli commented 2 years ago

@dbradish-microsoft, I am not sure. Actually some customers and I are a little bit concerned about the compliance/security of PyPI. Let's discuss about this internally first.

ngf-davef-devops commented 1 year ago

@jiasli If you think some customers are concerned about PyPI ... wait till they start discussing HomeBrew !

HomeBrew is not considered an acceptable solution by InfoSec in any of the last four environments I've worked in, all four have been Microsoft customers using, or looking to use Azure in some way. You need to figure out a way to package the CLI tools for macOS using accepted platform standards.

rakotomandimby commented 1 year ago

Anyway, I noted it on my blog for posterity and ease me when I want to remember how to do: https://mihamina.rktmb.org/2023/06/azurecli-custom-python.html

tkaepp commented 8 months ago

@rakotomandimby Thanks for your blog post. It helped me to fix my setup!

I also edited the az bash script manually because I still ran into the error when some application could not use the activated virtualenv.

So my workaround is to always activate the virtualenv and then I also switched to my system python binary instead of the bundled one from the cli.

#!/usr/bin/env bash
. /home/tkae/azure-cli-env/bin/activate
python -m azure.cli "$@"

I run into this issue because I use a gentoo based system where your packages are not available. I'll try to create an ebuild (gentoo package) to install it the right way if I find the time.

@jiasli Just as an FYI. If the intended solution is a pip install azure-cli to as the "Install from script" alternative it can be that some distributions won't allow that.

For example gentoo greets you with the following error if you try to install a package via pip outside of the package manager.

error: externally-managed-environment

× This environment is externally managed
╰─> 
    The system-wide Python installation in Gentoo should be maintained
    using the system package manager (e.g. emerge).

    If the package in question is not packaged for Gentoo, please
    consider installing it inside a virtual environment, e.g.:

    python -m venv /path/to/venv
    . /path/to/venv/bin/activate
    pip install mypackage

    To exit the virtual environment, run:

    deactivate

    The virtual environment is not deleted, and can be re-entered by
    re-sourcing the activate file.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
jiasli commented 7 months ago

@tkaepp, thanks for the information, but Gentoo Linux is not an officially supported system. The pip installation method provided above has been verified to work on Azure CLI's supported operating systems: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli

I also edited the az bash script manually because I still ran into the error when some application could not use the activated virtualenv.

Could you provide more information on this?

So my workaround is to always activate the virtualenv and then I also switched to my system python binary instead of the bundled one from the cli.

Azure CLI didn't bundle python binary. That azure-cli-env/bin/python in the virtual environment is merely a wrapper of system python.

If you install Azure CLI in a virtual environment, the az entry script won't be available on PATH. To invoke it, you may use any of these methods:

1. Call azure-cli-env/bin/az. It automatically uses the virtual environment's python interpreter:

$ cat azure-cli-env/bin/az
#!/home/user2/azure-cli-env/bin/python
...

$ azure-cli-env/bin/az --version
...
Python location '/home/user2/azure-cli-env/bin/python'

2. Activate the virtual environment

3. Add azure-cli-env/bin/python to PATH

Jiihaa commented 6 days ago

Please consider supporting Windows ARM as official package, this workaround using Python is tedious to be frank. What I had to do is, open powershell, install Python 3.12, run this modified script (not the same as above) `

Create a virtual environment

python3 -m venv azure-cli-env

Update pip

azure-cli-env/bin/python -m pip install --upgrade pip

Install azure-cli

azure-cli-env/bin/python -m pip install azure-cli

azure-cli-env/bin/python -m pip install setuptools `

and after that notepad $profile, write: Set-Alias -Name az -Value "C:\azure-cli-env\Scripts\az.bat"

and restart the Powershell, now it seems to work at least for az --version, haven't tested more deeply yet.