Linux distributions use Python for their own tools, and they install Python packages with their package manager (apt). When pip also installs into the same place, the two can overwrite each other’s files. PEP 668 lets a distribution mark its Python as “externally managed”, and recent pip versions then refuse system-wide installs with this message:
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.
The error isn’t a bug. It’s the system pointing you to a virtual environment.
Where you’ll see it
- Debian 12 and later, and Raspberry Pi OS based on it.
- Ubuntu 23.04 and later, including Ubuntu 24.04.
- Homebrew’s Python on macOS, and several other distributions.
Causes
- Running
pip installorpip3 installoutside any virtual environment. - A script or tutorial written for older systems that installs packages globally.
- A Dockerfile based on a Debian or Ubuntu image that calls the system pip.
- Believing a virtual environment is active when it isn’t (the prompt doesn’t show its name).
How to fix it
-
Create and use a virtual environment (recommended for any project):
python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txtInside the environment, pip installs normally. Run your app with
.venv/bin/python, or after activating. -
If
venvitself fails with “ensurepip is not available”, install the missing module:sudo apt install python3-venv. -
For command-line tools you want everywhere (formatters, downloaders), use pipx, which gives each tool its own environment:
sudo apt install pipx pipx install black -
For a library a system script needs, install the distribution’s package:
sudo apt install python3-requests. It may be an older version than the one on PyPI. -
--break-system-packagesis the last resort. It bypasses the protection, and pip may then overwrite files the OS relies on. Use it only in disposable containers, never on a server you care about. -
In Docker, either start from an official
pythonimage, where pip works normally, or create a venv in the image and put itsbinfolder first inPATH.
Don’t delete the EXTERNALLY-MANAGED marker file to silence the error: it hides the problem and is restored on the next system update.
If packages are installed but your code still can’t import them, see ModuleNotFoundError.
Prevention
- Start every project with a virtual environment and a
requirements.txt. - Keep system Python for the OS, and your apps in their own environments.
- Update old install scripts to create a venv first.
On ArabyHost
On ArabyHost AI/Developer plans there’s no root access, so system-wide installs aren’t possible in any case; your packages always belong in a virtual environment. cPanel’s Setup Python App creates one for each app (Python 3.9 to 3.12). Enter it over SSH with the command shown at the top of the app’s page, then pip install -r requirements.txt works without this error. See Python hosting for what the plans support.
FAQ
Is it safe to use --break-system-packages?
It's safe only in a throwaway environment such as a disposable container. On a real system, pip can replace packages that the OS tools depend on, and a later update or system command may break.
Does pip install --user avoid the error?
No. On systems that adopted PEP 668, user installs into the system Python are blocked too. Use a virtual environment or pipx instead.
python3 -m venv fails with ensurepip is not available. What now?
On Debian and Ubuntu the venv module ships separately. Install it with `sudo apt install python3-venv` (or the versioned package such as `python3.12-venv`), then create the environment again.