Skip to content
FR
  1. LuzineDebug
  2. Python
  3. error: externally-managed-environment
Python Beginner ≈ 5 min to fix

Fix "error: externally-managed-environment" with pip

Error 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.
  • ╰─> 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.
In short

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.

Be the first to confirm this fix

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

  1. pip install on the system Python of Debian 12+, Ubuntu 23.04+ (including 24.04 LTS), Raspberry Pi OS Bookworm, Arch or Kali.
  2. Homebrew Python on macOS: pip3 install targets the Homebrew-managed Python, which is also marked “externally managed”.
  3. Virtual environment not activated: the venv exists, but the terminal still calls the system pip.
  4. Docker image based on debian or ubuntu with apt install python3-pip, instead of the official python image or a venv.

Step-by-step fix

  1. Create a virtual environment for the project

    This is the recommended fix. On Debian and Ubuntu, the venv module is sometimes packaged separately: install python3-venv if creation fails.

    bash
    # Debian / Ubuntu, only if needed
    sudo apt install python3-venv
    
    python3 -m venv .venv
    source .venv/bin/activate
    python -m pip install requests
  2. Install command-line tools with pipx

    For a global tool such as black, httpie or yt-dlp, pipx creates an isolated venv per tool and puts the executable on your PATH.

    bash
    # Debian / Ubuntu
    sudo apt install pipx
    # macOS
    brew install pipx
    
    pipx ensurepath
    pipx install black
  3. 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.

    bash
    apt search python3-requests
    sudo apt install python3-requests
  4. Fast alternative: uv

    uv creates environments and installs packages very quickly while respecting PEP 668. uv tool install plays the same role as pipx.

    bash
    uv venv
    source .venv/bin/activate
    uv pip install requests
    
    uv tool install ruff
  5. Fix a Dockerfile

    Use the official python image (its Python is not marked “externally managed”) or create a venv inside the image and put it first on PATH.

    dockerfile
    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
  6. 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 use sudo pip. Do not delete the EXTERNALLY-MANAGED file.

    bash
    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 .venv in every project before the first pip install.
  • Install global tools with pipx or uv tool, never with sudo pip.
  • In Docker, start from the official python:3.x-slim image 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

All Python errors →