externally-managed-environment
Problem Statement
When running pip install xyz
on Debian, Ubuntu, or other Debian-based Linux distributions, you may encounter:
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.
This occurs because Python environments managed by the OS package manager (APT) are marked as externally managed. Adding packages via pip
in these environments can:
- Conflict with system-installed Python packages
- Break system tools depending on specific versions
- Render your OS unstable or unusable
The error is a protective mechanism introduced by PEP 668 to prevent accidental corruption of system-managed Python installations.
Recommended Solutions
1. Install Packages System-Wide Using APT (Ideal)
When available, use Debian-packaged versions of Python packages:
sudo apt install python3-xyz
LIMITATIONS
Use this approach only if:
- The package exists in your distribution's repositories
- You require the exact version provided by your distribution
2. Use Virtual Environments (Recommended for Development)
Isolate project dependencies in a virtual environment:
# Create a virtual environment
python3 -m venv .venv
# Activate the environment (Linux/Mac)
source .venv/bin/activate
# Windows
.venv\Scripts\activate
# Install packages safely
pip install xyz
Execute scripts directly without activation:
.venv/bin/python your-script.py
.venv/bin/pip install xyz
Why this works?
Virtual environments create isolated Python installations with their own:
site-packages
directory- Environment paths
- Dependency trees
This prevents conflicts with system-managed packages.
3. Use pipx for Python Applications
Install command-line Python tools system-wide without conflicts:
# Install pipx
sudo apt install pipx
# Verify PATH setup
pipx ensurepath
# Install Python applications globally
pipx install ruff # Example tool
BEST FOR
- Standalone Python applications (e.g.,
black
,poetry
) - Tools executed from the command line
Special Cases
Docker Containers
Avoid breaking packages in containers. For Debian-based images:
# Install Debian-packaged Python instead of bypassing warnings
RUN apt install python3-xyz
# Or create a virtual environment
RUN python3 -m venv /venv \
&& /venv/bin/pip install xyz
Conda/Mamba Environments
If using Conda and seeing this error, ensure Conda's pip is active:
conda install pip # Refresh Conda-managed pip
pip install xyz # Now uses Conda's isolated environment
Development Containers (Devcontainer)
Specify explicit Python versions to avoid system conflicts:
// In .devcontainer.json
"features": {
"ghcr.io/devcontainers/features/python:1": {
"version": "3.13" // Avoid "os-provided"
}
}
Ansible Tools
Install dependencies in existing pipx
environments:
pipx runpip ansible install netaddr
Last Resort: Override Safeguards
RISK OF SYSTEM BREAKAGE
Only use when:
- You understand and accept the risks
- No other options solve your use case
Temporary Override (Single Command)
Add the override flag:
pip install --break-system-packages xyz
Persistent Configuration (Not Recommended)
Set a global flag:
pip config set global.break-system-packages true
ALTERNATIVE RISK
Never delete the EXTERNALLY-MANAGED marker file:
rm /usr/lib/python3.*/EXTERNALLY-MANAGED # THIS MAY BREAK YOUR SYSTEM
Best Practices Summary
Use Case | Solution |
---|---|
System tools/libraries | sudo apt install python3-xyz |
Project development | Virtual environments (python3 -m venv .venv ) |
CLI applications | pipx install xyz |
Ignoring errors (unsafe) | --break-system-packages |
Container environments | Virtual environments or APT |
Implementing these practices ensures: ✅ System stability
✅ Dependency isolation
✅ Reproducible installations
✅ Adherence to Python packaging standards