Sphere
Log in Join

`error: externally-managed-environment` on Ubuntu 24.04 with pip 24.0 (PEP 668)

The first time a teammate ran pip install requests on our new Ubuntu 24.04 dev box, it printed a wall of text and installed nothing. I'd seen similar on Debian 12 but hadn't read it properly. Here's the whole thing, because the important part is at the bottom:

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.

If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip.

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.

That's pip 24.0 with Python 3.12.3, and it's PEP 668. The distro marks the system Python with an EXTERNALLY-MANAGED file so pip stops writing over packages apt owns. Annoying, but it prevents a genuinely awful class of breakage.

## First attempt: venv failed differently

I reached for the obvious fix:

bash
python3 -m venv ~/.venvs/scraper

which got:

The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

apt-get install python3.12-venv

Ubuntu splits ensurepip out of the base package. So:

bash
sudo apt update
sudo apt install -y python3.12-venv python3-pip
python3 -m venv ~/.venvs/scraper
source ~/.venvs/scraper/bin/activate
python -m pip install --upgrade pip

Note python -m pip rather than bare pip. If the venv activation gets lost in a subshell, plain pip can still point at the system one and you're back to the first error. I've done that twice.

## CLI tools go in pipx

For things like black, ruff, and httpie, making a venv per tool is tedious. pipx handles it:

bash
sudo apt install -y pipx
pipx install ruff
pipx ensurepath

It puts each tool in its own venv and symlinks the binary into ~/.local/bin. ensurepath edits your shell profile, so you need a new shell or source ~/.bashrc. Worth knowing if ruff: command not found shows up immediately after a successful install.

The IDE was the other annoyance. VS Code's Python extension kept using /usr/bin/python3 for a week because I never selected the interpreter from the status bar, so packages installed in the venv worked in the terminal and failed in the editor with missing imports. Python: Select Interpreter sorted it, but I wasted a morning assuming the venv was broken.

## About --break-system-packages

It works, and I'd avoid it on any machine that matters. In a throwaway container built from python:3.12-slim it's harmless, since nothing else depends on that interpreter. On a workstation, apt and pip will eventually fight over the same site-packages directory and you'll get a weird ImportError three months later with no obvious cause.

The only place I use it now is inside a Docker layer where I control the whole image. Anywhere else, venv first. Took me a couple of days of muttering about it to accept that.