Unlike other operating systems, modern Linux distributions (especially Ubuntu 24.04+) implement PEP 668 which prevents pip from installing packages system-wide by default. This creates the infamous externally-managed-environment error.
pip3 install some-package
# error: externally-managed-environment
# × This environment is externally managed- Ubuntu/Debian systems manage Python packages through
apt(Advanced Package Tool) - Installing packages with
pipcan conflict with system-managed packages - This could potentially break system tools that depend on specific package versions
- Linux distributions implement this restriction to protect system stability
The restriction is enforced by a file located at:
/usr/lib/python3.12/EXTERNALLY-MANAGEDWe successfully restored normal pip install functionality by removing the restriction file. Here's what we did:
# Found the file causing the restriction
find /usr -name "EXTERNALLY-MANAGED" 2>/dev/null
# Result: /usr/lib/python3.12/EXTERNALLY-MANAGEDcat /usr/lib/python3.12/EXTERNALLY-MANAGED
# Shows the error message and suggested alternatives# Renamed (not deleted) the file to restore pip functionality
sudo mv /usr/lib/python3.12/EXTERNALLY-MANAGED /usr/lib/python3.12/EXTERNALLY-MANAGED.backup# Now works without any flags or workarounds
pip3 install langchain-couchbase --upgrade- ✅ Normal
pip installbehavior restored - ✅ No need for
--break-system-packagesflags - ✅ No need for virtual environments or workarounds
- ✅ System remains stable (file backed up, not deleted)
- ✅ Can be reverted if needed
# Create isolated environment
python3 -m venv myproject_env
source myproject_env/bin/activate
pip install package-name# Install applications in isolated environments
sudo apt install pipx
pipx install package-name# Use apt when packages are available
apt search python3-package-name
sudo apt install python3-package-name# Use flag for each installation
pip3 install package-name --break-system-packages# Make pip always use override
mkdir -p ~/.config/pip
echo -e "[global]\nbreak-system-packages = true" > ~/.config/pip/pip.conf| ✅ Advantages | ❌ Other Methods' Drawbacks |
|---|---|
Standard pip behavior |
Virtual envs: Extra complexity |
| No flags needed | pipx: Limited to applications |
| Works with all tools | apt: Many packages unavailable |
| Simple and direct | Override flag: Must remember every time |
| Easily reversible | Global config: Harder to troubleshoot |
- File was backed up, not deleted
- System Python remains untouched
- Can be restored anytime:
sudo mv /usr/lib/python3.12/EXTERNALLY-MANAGED.backup /usr/lib/python3.12/EXTERNALLY-MANAGED
# Your Python installation
python3 --version # System Python (usually latest stable)
which python3 # /usr/bin/python3
pip3 install package # ✅ Now works normally!❌ pip3 install package # Error: externally-managed-environment
❌ Complex workarounds needed
❌ Extra steps for every installation
❌ Inconsistent development experience✅ pip3 install package # Works perfectly!
✅ Standard Python workflow
✅ No extra steps or flags
✅ Professional development experience-
Use the standard pip workflow we restored:
pip3 install package-name pip3 install --upgrade package-name pip3 uninstall package-name
-
Keep packages updated:
pip3 list --outdated pip3 install --upgrade package-name
-
Know your Python location:
which python3 # /usr/bin/python3 python3 --version # Your system Python version
-
System-critical packages:
- Avoid upgrading packages that system tools depend on
- If in doubt, check what uses a package:
apt rdepends python3-package-name
-
Major system changes:
- Don't delete system Python
- Don't modify core system files unnecessarily
-
Don't delete system Python:
# NEVER do this! sudo rm -rf /usr/bin/python3 # Would break your system
-
Don't install conflicting versions of system packages:
- Be careful with packages also available through
apt
- Be careful with packages also available through
Your project uses Poetry, which manages its own virtual environment:
# Project setup (already configured)
poetry install # Install dependencies
poetry shell # Activate environment
poetry add package # Add new packages
poetry run python script.py # Run scripts# Simple approach (now that pip works)
pip3 install package-name
# Or create project-specific environment if preferred
python3 -m venv new_project
cd new_project
source bin/activate
pip install requirementssudo apt update
sudo apt install python3-pip# For user installation
pip3 install --user package-name
# For system-wide (our restored functionality)
pip3 install package-name # Should work now# Check what's installed
pip3 list
# Uninstall conflicting package
pip3 uninstall old-package
pip3 install new-packageIf you ever want to restore the original restriction:
# Restore the original file
sudo mv /usr/lib/python3.12/EXTERNALLY-MANAGED.backup /usr/lib/python3.12/EXTERNALLY-MANAGED
# Verify restriction is back
pip3 install test-package # Should show externally-managed error again- PEP 668 implemented in Ubuntu 24.04+ and recent Debian versions
- Our solution works for Ubuntu 22.04, 24.04, and newer
- File location:
/usr/lib/python3.X/EXTERNALLY-MANAGED
- May use different approaches or not implement PEP 668
- Usually no EXTERNALLY-MANAGED file
- Standard pip behavior typically works out of the box
- Generally doesn't restrict pip
- Uses rolling releases with latest Python
- Standard pip workflow usually works
Is removing EXTERNALLY-MANAGED safe?
- ✅ Yes, when done carefully (we backed up the file)
- ✅ System Python remains protected
- ✅ You still have control over what you install
- ✅ Can be reverted instantly
Security best practices:
- Only install packages you trust
- Keep packages updated
- Review package dependencies
- Use
pip install --userfor user-only packages when appropriate
# Check installed packages and locations
pip3 show package-name
# Install specific version
pip3 install package-name==1.2.3
# Install from requirements file
pip3 install -r requirements.txt
# Generate requirements file
pip3 freeze > requirements.txt
# Check for security vulnerabilities
pip3 audit# See what apt packages are python-related
apt list --installed | grep python3
# Find conflicts between pip and apt packages
dpkg -l | grep python3
pip3 list- No workflow disruption: Standard pip commands work
- No extra complexity: No virtual environments unless you want them
- Tool compatibility: All Python tools work normally
- Industry standard: Matches Python development everywhere else
- Backed up original: Can restore anytime
- Non-destructive: System Python untouched
- Transparent: You know exactly what changed
- Documented: Full understanding of the process
Your Linux system now has the same professional Python development experience as macOS and Windows, while maintaining system stability and security.
Test your setup right now:
# These should all work perfectly
python3 --version # Shows your system Python
which python3 # Shows /usr/bin/python3
pip3 --version # Shows pip version
pip3 install --upgrade pip # Updates pip itself
pip3 list # Shows installed packages🎉 Congratulations! You now have a professional Linux Python development environment that works exactly as expected.
This approach combines the simplicity of standard Python workflows with the stability and security of Linux systems.