Fix 'externally-managed-environment' pip error on Linux
This pip error on newer Linux distros stops you installing Python packages. The fix is a virtual environment or a flag. Here's how to get past it.
I know this error is infuriating. You type pip install requests and BAM—externally-managed-environment. Your pip is broken? No, actually your Linux distro is trying to protect you. Let's fix it fast.
The quick fix
You've got two solid options. Pick one based on what you're doing.
Option 1: Use a virtual environment (the right way)
This is what I'd recommend 9 times out of 10. Python 3.3+ comes with venv built in.
python3 -m venv myprojectenv
source myprojectenv/bin/activate
pip install requests
Once you're inside the virtual environment, the error vanishes because you're not touching the system Python. Your pip installs land in myprojectenv/lib/python3.x/site-packages. Clean, isolated, no conflicts.
Option 2: Override the error (quick and dirty)
If you're in a controlled environment like a Docker container or a one-off VM, you can bypass the safeguard:
pip install requests --break-system-packages
Or set an environment variable to make this the default:
export PIP_REQUIRE_VIRTUALENV=false
pip install requests
I use this rarely—only when I'm building a throwaway container. On your main machine, stick with venv.
Why this error happens
Starting around 2023, major Linux distros adopted PEP 668. Ubuntu 23.04, Debian 12, Fedora 38, and newer versions now mark the system Python environment as "externally managed." The idea is simple: your package manager (apt, dnf) handles Python packages like pip itself. If you install something with pip globally, you risk overwriting a system package that the OS depends on. That's why the error blocks you.
It's the same reason your distro says "don't use sudo pip". This is just a friendlier way of enforcing that rule.
Less common variations
Variation A: You see the error inside a virtual environment
Rare but happens if you accidentally activated the wrong environment, or your python3 symlink points to a broken venv. Run which python3 and which pip—they should both point inside your venv/bin/ folder. If not, deactivate and re-create the environment.
Variation B: The error appears with pipx
pipx installs tools in isolated environments by default, so it usually works. But if you see the error, run pipx install --force package-name. This forces pipx to use its own venv, bypassing the system Python restriction entirely.
Variation C: You get the error in a CI/CD pipeline (GitHub Actions, GitLab CI)
Your runner image (like ubuntu:24.04) is fresh. The fix is to create a venv inside the pipeline step:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Then run your tests inside the same shell session. No global pip pollution.
Variation D: The error shows even after setting PIP_REQUIRE_VIRTUALENV
Some distros (like NixOS or Gentoo) go further and patch pip to ignore that variable. If --break-system-packages also fails, check if you're inside a distro that uses pip>=23.1 with the new EXTERNALLY-MANAGED marker file in /usr/lib/python3.x/EXTERNALLY-MANAGED. Delete that file? You could, but I'd never recommend it—it'll break your package manager. Just use venv.
Prevention
Stop installing Python packages globally on modern Linux. That's the old way. Here's my rule of thumb:
- For project dependencies, create a
venvin each project directory. Usepip freeze > requirements.txtto lock versions. - For CLI tools like black, httpie, or pytest, install them with
pipx. It handles the venv mess for you. - For system-level scripts that need Python, either run them inside a venv or use Docker to fully isolate the environment.
One tip that saved me hours: add this to your .bashrc or .zshrc:
export PIP_REQUIRE_VIRTUALENV=true
This makes pip refuse to work outside a venv globally—even on older distros. Keeps your system clean and your future self grateful.
That's it. You're not stuck. Use venv for projects, pipx for tools, and you'll never see this error again.
Was this solution helpful?