If youโre a Python developer, youโve likely run into the frustrating “error: externally-managed-environment” when trying to install packages with pip3. This error pops up on Linux distributions like Ubuntu, Debian, or Fedora, and even on macOS, stopping you from installing Python packages system-wide. Donโt worryโthis guide will explain why this happens and walk you through 5 practical solutions to fix it, using simple language and examples. By the end, youโll be installing packages like a pro, whether youโre a beginner or a seasoned coder.
Table of Contents
What Is the “error: externally-managed-environment” Error?
When you run a command like pip3 install requests, you might see something like this:
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.
Make sure you have python3-full installed.
If you wish to install a non-Debian packaged Python application, it may be easiest to use pipx install xyz, which will manage a virtual environment for you.
Make sure you have pipx installed.
See /usr/share/doc/python3.11/README.venv for more information.
note: 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.
This error happens because modern Linux distributions (like Ubuntu 23.04+, Debian 12+, and Fedora 38+) and some macOS setups follow PEP 668, a Python Enhancement Proposal that marks system Python environments as “externally managed.” This means the operating systemโs package manager (e.g., apt for Debian/Ubuntu, dnf for Fedora, or brew for macOS) controls the Python environment to prevent conflicts between system tools and pip-installed packages.
Why Does This Matter?
Your operating system uses Python for critical toolsโlike Ubuntuโs update-manager or Fedoraโs dnf. If you install or update packages with pip3, you might accidentally overwrite or conflict with these system dependencies, potentially breaking your OS. PEP 668 protects your system by blocking pip3 from installing packages globally unless youโre in a virtual environment or use specific workarounds.
When Does the Error Occur?
Youโll see this error when:
- You run
pip3 install <package>in the system Python environment (e.g.,/usr/bin/python3). - Youโre on a Linux distribution or macOS that adopts PEP 668.
- You try to install packages globally, even with the
--userflag, which can still conflict with system packages.
Now, letโs explore the 5 best ways to fix this error, starting with the safest and most recommended solutions.
Solution 1: Use a Virtual Environment (The Best Way)
The most reliable and recommended way to avoid the “externally-managed-environment” error is to use a Python virtual environment. A virtual environment creates an isolated Python setup for your project, letting you install packages with pip3 without touching the system Python.
Why Itโs Great
- Safe: No risk of breaking system Python.
- Isolated: Each project gets its own packages, avoiding conflicts.
- Standard Practice: Widely used by Python developers for clean dependency management.
Step-by-Step Guide
- Check if
venvIs Installed
On Debian/Ubuntu, ensure thepython3-venvpackage is installed:
sudo apt update
sudo apt install python3-venv
For Fedora:
sudo dnf install python3-virtualenv
- Create a Virtual Environment
Navigate to your project directory (or create one):
mkdir my_project
cd my_project
Create a virtual environment named venv (or any name you prefer):
python3 -m venv venv
- Activate the Virtual Environment
Activate it to switch to the isolated environment:
- On Linux/macOS:
bash source venv/bin/activate - On Windows:
bash venv\Scripts\activate
Your terminal prompt should change, showing(venv)or similar, indicating the environment is active.
- Install Packages with pip3
Now, pip3 will install packages only in the virtual environment:
pip3 install requests
Verify the installation:
pip3 list
- Deactivate When Done
To exit the virtual environment, simply type:
deactivate
Example in Action
Letโs say you want to install the numpy package:
cd my_project
python3 -m venv venv
source venv/bin/activate
pip3 install numpy
python3 -c "import numpy; print(numpy.__version__)"
This installs numpy in the venv folder, leaving your system Python untouched.
Pro Tip
If youโre still getting the error in a virtual environment, double-check that youโve activated it correctly. Run which pip3 to confirm it points to venv/bin/pip3, not /usr/bin/pip3. If itโs wrong, you might need to recreate the environment or ensure python3-venv is installed.
Solution 2: Use pipx for Python Applications
If youโre installing a Python application (like ansible, awscli, or pibooth) rather than a library, pipx is a fantastic tool. Pipx automatically creates a virtual environment for each app, installs the app, and adds it to your $PATH so you can run it from anywhere.
Why Itโs Great
- Hassle-Free: Manages virtual environments for you.
- Global Access: Run apps from the command line without activating environments.
- Safe: Avoids system Python conflicts.
Step-by-Step Guide
- Install pipx
On Debian/Ubuntu:
sudo apt update
sudo apt install pipx
On Fedora:
sudo dnf install pipx
On macOS (with Homebrew):
brew install pipx
- Set Up pipx
Ensure pipx is in your$PATH:
pipx ensurepath
Restart your terminal or source your shell configuration (e.g., source ~/.bashrc).
- Install an Application
Install your desired app with pipx:
pipx install ansible
This creates an isolated environment for ansible and makes it runnable globally.
- Run the Application
Use the app as if it were installed system-wide:
ansible --version
Example in Action
To install the pibooth photo booth app on a Raspberry Pi:
sudo apt install pipx
pipx ensurepath
pipx install pibooth
pibooth --version
This avoids the error and keeps your system clean.
Pro Tip
Pipx is best for command-line tools, not libraries like numpy or requests. If you need a library, stick with a virtual environment or the system package manager.
Solution 3: Install Packages with the System Package Manager
If the package you need is available in your operating systemโs package repository, you can install it using the system package manager (apt, dnf, brew, etc.) instead of pip3. This is the safest way to install Python libraries for system-wide use.
Why Itโs Great
- System-Approved: Packages are tested to work with your OS.
- No Conflicts: Avoids pip-related issues entirely.
- Easy Updates: Managed by the OSโs update system.
Step-by-Step Guide
- Search for the Package
Check if the package is available:
- On Debian/Ubuntu:
bash apt search python3-<package>
Example:apt search python3-requests - On Fedora:
bash dnf search python3-<package>
Example:dnf search python3-numpy - On macOS (Homebrew):
bash brew search <package>
Example:brew search requests
- Install the Package
Install it with the appropriate command:
- On Debian/Ubuntu:
bash sudo apt install python3-requests - On Fedora:
bash sudo dnf install python3-numpy - On macOS:
bash brew install requests
- Verify Installation
Check that the package is available:
python3 -c "import requests; print(requests.__version__-clad)"
Example in Action
To install flask on Ubuntu:
apt search python3-flask
sudo apt install python3-flask
python3 -c "import flask; print(flask.__version__)"
This installs flask in /usr/lib/python3/dist-packages/, managed by apt.
Pro Tip
System packages might be older than the latest version on PyPI (e.g., apt might offer requests 2.25.1 while PyPI has 2.31.0). If you need the latest version, use a virtual environment or pipx instead.
Solution 4: Use the –break-system-packages Flag (Use with Caution)
If you absolutely need to install a package globally with pip3 and canโt use a virtual environment or pipx, you can bypass the error with the --break-system-packages flag. This tells pip3 to ignore the externally managed environment warning.
Why Itโs Risky
- Potential Conflicts: You might overwrite system dependencies, breaking OS tools.
- Future Issues: Updates to system packages could cause mysterious errors.
- Not Recommended: Python developers and OS maintainers discourage this unless youโre sure of the consequences.
Step-by-Step Guide
- Run pip3 with the Flag
Add--break-system-packagesto your pip3 command:
pip3 install requests --break-system-packages
- Combine with –user (Safer)
To reduce risk, install packages for your user only:
pip3 install --user requests --break-system-packages
This installs packages in ~/.local/lib/python3.x/site-packages/, avoiding system-wide changes.
- Verify Installation
Check that the package is installed:
python3 -c "import requests; print(requests.__version__)"
Example in Action
To install pyautogui globally:
pip3 install --user pyautogui --break-system-packages
python3 -c "import pyautogui; print(pyautogui.__version__)"
This installs pyautogui for your user, but be cautious of conflicts.
Pro Tip
Use this only in controlled environments, like a Docker container or a personal VM, where breaking the system wonโt cause major issues. For example, in a Docker container, you might use:
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y python3-pip
RUN pip3 install flask --break-system-packages
But even here, a virtual environment is safer.
Solution 5: Remove the EXTERNALLY-MANAGED File (Last Resort)
Some tutorials suggest deleting the EXTERNALLY-MANAGED file that triggers the error. This file, typically located at /usr/lib/python3.x/EXTERNALLY-MANAGED, tells pip3 that the environment is managed by the OS. Removing it disables the PEP 668 protection.
Why Itโs Risky
- Breaks System Integrity: Youโre bypassing a deliberate safety mechanism.
- Hard to Debug: Future package conflicts might be tricky to trace.
- Not Permanent: OS updates might restore the file.
Step-by-Step Guide
- Locate the File
Find theEXTERNALLY-MANAGEDfile:
find /usr/lib -name EXTERNALLY-MANAGED
Example output: /usr/lib/python3.11/EXTERNALLY-MANAGED
- Back Up the File
Before deleting, make a backup:
sudo cp /usr/lib/python3.11/EXTERNALLY-MANAGED ~/EXTERNALLY-MANAGED-BACKUP
- Remove the File
Delete it with:
sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED
- Install Packages
Now, pip3 should work without the error:
pip3 install requests
Example in Action
To install pandas after removing the file:
sudo cp /usr/lib/python3.11/EXTERNALLY-MANAGED ~/backup/
sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED
pip3 install pandas
python3 -c "import pandas; print(pandas.__version__)"
Pro Tip
This is a last resort. If youโre tempted to do this, consider using a virtual environment or pipx instead. If you must, document the change and monitor for issues after OS updates.
Troubleshooting Common Issues
Even with these solutions, you might hit snags. Here are common problems and fixes:
1. Virtual Environment Still Shows the Error
- Cause: Youโre using the systemโs
pip3instead of the virtual environmentโs. - Fix: Verify the pip3 path:
which pip3
It should point to venv/bin/pip3. If not, reactivate the environment (source venv/bin/activate) or recreate it:
rm -rf venv
python3 -m venv venv
source venv/bin/activate
If the issue persists, ensure python3-venv is installed.
2. Package Not Found in System Repository
- Cause: Some packages (e.g., niche libraries) arenโt available via
apt,dnf, orbrew. - Fix: Use a virtual environment or pipx. For example:
pipx install obscure-package
3. Conda Environment Issues
- Cause: If youโre using Conda, the system
pip3might be used instead of Condaโs. - Fix: Activate your Conda environment and install pip:
conda activate my_env
conda install pip
pip install requests
Check the pip path:
which pip
It should point to your Conda environmentโs bin folder.
4. Docker-Specific Errors
- Cause: Docker images based on Debian/Ubuntu may include the
EXTERNALLY-MANAGEDfile. - Fix: Use a virtual environment in your Dockerfile:
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y python3 python3-venv
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install flask
Alternatively, use --break-system-packages if the container is isolated:
RUN pip install flask --break-system-packages
Best Practices for Avoiding the Error
To keep your Python workflow smooth and error-free:
- Always Use Virtual Environments: Make it a habit for every project to avoid conflicts.
- Prefer pipx for Apps: Install command-line tools with pipx for simplicity.
- Check System Packages First: Use
apt,dnf, orbrewfor libraries when possible. - Avoid Global Installs: Donโt use
sudo pip3 installor--break-system-packagesunless absolutely necessary. - Keep Python Updated: Ensure you have the latest Python and pip versions:
python3 -m pip install --upgrade pip
FAQs About the “externally-managed-environment” Error
Why did this error start appearing recently?
The error is tied to PEP 668, adopted by newer Linux distributions (e.g., Ubuntu 23.04+, Debian 12) and some macOS setups to protect system Python environments from pip conflicts.
Can I use --user to avoid the error?
Not always. Even --user installs can conflict with system packages, as they affect ~/.local/lib/python3.x/, which some system tools use.
Is it safe to use --break-system-packages?
Itโs risky and can break system tools. Use it only in controlled environments like Docker or VMs, and prefer --user to limit impact.
Whatโs the difference between pipx and a virtual environment?
Pipx automates virtual environments for apps, making them globally accessible. Virtual environments are manual and project-specific, ideal for libraries.
Iโm using Condaโwhy do I get this error?
You might be using the system pip3 instead of Condaโs. Activate your Conda environment and install pip with conda install pip.
Conclusion: Take Control of pip3 in 2025
The “error: externally-managed-environment” can be a roadblock, but itโs not the end of the world. By using virtual environments (the gold standard), pipx for apps, or your systemโs package manager, you can safely install Python packages without risking your systemโs stability. For those rare cases where you need to bypass PEP 668, the --break-system-packages flag or removing the EXTERNALLY-MANAGED file can workโbut use them sparingly and with caution.
Whether youโre building a web app, automating tasks, or just experimenting with Python, these solutions will keep your pip3 workflow smooth in 2025. Have you run into this error? Which solution worked for you? Share your experience on X or dive into your next Python project with confidence!
Resources
- PEP 668 Official Documentation โ Learn why the error exists.