Fix "error: externally-managed-environment" with pip
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.
- ╰─> To install Python packages system-wide, try brew install xyz, where xyz is the package you are trying to install.
- hint: See PEP 668 for the detailed specification.
Your system (Debian 12+, Ubuntu 23.04+, Homebrew, Arch…) forbids pip from installing into the system Python, as defined by PEP 668. The right fix is to install into a virtual environment (python3 -m venv .venv), or with pipx for command-line tools.
What this error means
The Python shipped by your distribution or by Homebrew is also used by system tools. If pip installs or upgrades packages there, it can break apt, system utilities or Homebrew formulae. PEP 668 therefore lets the distributor place an EXTERNALLY-MANAGED file in the standard library folder. Since pip 23.0, when that file is present and you are not in a virtual environment, pip refuses to install and prints this message. It is not a bug: it is a safeguard that points you to a venv, pipx or the system package manager.
Common causes
pip installon the system Python of Debian 12+, Ubuntu 23.04+ (including 24.04 LTS), Raspberry Pi OS Bookworm, Arch or Kali.- Homebrew Python on macOS:
pip3 installtargets the Homebrew-managed Python, which is also marked “externally managed”. - Virtual environment not activated: the venv exists, but the terminal still calls the system
pip. - Docker image based on
debianorubuntuwithapt install python3-pip, instead of the officialpythonimage or a venv.
Step-by-step fix
-
Create a virtual environment for the project
This is the recommended fix. On Debian and Ubuntu, the
venvmodule is sometimes packaged separately: installpython3-venvif creation fails.# Debian / Ubuntu, only if needed sudo apt install python3-venv python3 -m venv .venv source .venv/bin/activate python -m pip install requests -
Install command-line tools with pipx
For a global tool such as
black,httpieoryt-dlp,pipxcreates an isolated venv per tool and puts the executable on yourPATH.# Debian / Ubuntu sudo apt install pipx # macOS brew install pipx pipx ensurepath pipx install black -
Use the system package when it exists
If a library must be available to the system Python (for an admin script, for example), install the version packaged by your distribution.
apt search python3-requests sudo apt install python3-requests -
Fast alternative: uv
uvcreates environments and installs packages very quickly while respecting PEP 668.uv tool installplays the same role as pipx.uv venv source .venv/bin/activate uv pip install requests uv tool install ruff -
Fix a Dockerfile
Use the official
pythonimage (its Python is not marked “externally managed”) or create a venv inside the image and put it first onPATH.FROM debian:bookworm-slim RUN apt-get update \ && apt-get install -y --no-install-recommends python3 python3-venv \ && rm -rf /var/lib/apt/lists/* RUN python3 -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -
Last resort only: --break-system-packages
This flag bypasses the safeguard and can break system tools during an upgrade. If you truly have no other option, limit the damage with
--user(install into your home folder) and never usesudo pip. Do not delete theEXTERNALLY-MANAGEDfile.python3 -m pip install --user --break-system-packages requests
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Make a habit of creating a
.venvin every project before the firstpip install. - Install global tools with
pipxoruv tool, never withsudo pip. - In Docker, start from the official
python:3.x-slimimage or a dedicated venv.
Frequently asked questions
Is it dangerous to use --break-system-packages?
Potentially, yes: you can replace a library a system tool depends on and break it at the next upgrade. A venv gives the same result without that risk.
Why did this error appear after upgrading to Ubuntu 24.04?
Ubuntu enforces PEP 668 since version 23.04. pip install commands that worked on 22.04 are now blocked outside a virtual environment.
What is the difference between pip and pipx?
pip installs libraries into the current environment. pipx installs command-line applications, each in its own isolated environment.
Useful tools for this error
More Python errors
- Fix "ModuleNotFoundError: No module named" in Python
- Fix "IndentationError: unexpected indent" in Python
- Fix "'NoneType' object is not subscriptable" in Python
- Fix "UnicodeDecodeError: 'utf-8' codec can't decode byte" in Python
- Fix "KeyError" in Python (dict, pandas, os.environ)
- Fix "RecursionError: maximum recursion depth exceeded" in Python
- Fix "attempted relative import with no known parent package" in Python