Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jiasli opened this issue Nov 23, 2021 · 8 comments
Open

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

jiasli opened this issue Nov 23, 2021 · 8 comments

Comments

@jiasli
Copy link
Member

jiasli commented Nov 23, 2021

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:
    elif distname == 'ubuntu' and version in ['15.10', '16.04', '18.04']or distname == 'debian' and version.startswith('8'):
    dep_list = ['libssl-dev', 'libffi-dev', python_dep, 'build-essential']
@ghost ghost added the needs-triage This is a new issue that needs to be triaged to the appropriate team. label Nov 23, 2021
@ghost ghost removed the needs-triage This is a new issue that needs to be triaged to the appropriate team. label Nov 23, 2021
@yonzhan yonzhan added this to the Dec 2021 (2022-01-04) milestone Nov 23, 2021
@jiasli
Copy link
Member Author

jiasli commented Jan 7, 2022

[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

@jiasli jiasli changed the title [Doc] Add doc for pip install azure-cli [Doc] Add doc for installing Azure CLI with pip Jan 7, 2022
@dbradish-microsoft
Copy link
Contributor

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

@jiasli
Copy link
Member Author

jiasli commented Jan 10, 2022

@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
Copy link

@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
Copy link

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
Copy link

tkaepp commented Nov 10, 2023

@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
Copy link
Member Author

jiasli commented Nov 13, 2023

@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
Copy link

Jiihaa commented Jul 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants