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
--user
flag, 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
venv
Is Installed
On Debian/Ubuntu, ensure thepython3-venv
package 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-packages
to 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-MANAGED
file:
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
pip3
instead 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
pip3
might 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-MANAGED
file. - 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
, orbrew
for libraries when possible. - Avoid Global Installs: Don’t use
sudo pip3 install
or--break-system-packages
unless 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.