How to Fix the pip “externally-managed-environment” Error on Linux

If you’re a Python enthusiast who recently upgraded to Ubuntu 23.04, Fedora 38, or other modern Linux distributions, you may have encountered the frustrating “externally-managed-environment” error when attempting to install packages with pip, the Python package manager. This issue arises from the adoption of standards defined in PEP-668, aimed at preventing conflicts between the distribution’s package manager and Python’s package management tools. However, there are several effective approaches you can take to overcome this obstacle.

Solution 1: Remove the “EXTERNALLY-MANAGED” File

The most straightforward solution is to navigate to the /usr/lib/python3.xx directory and delete the EXTERNALLY-MANAGED file. Follow these commands:

cd /usr/lib/python3.11

sudo rm EXTERNALLY-MANAGED

This should immediately resolve the “externally-managed” error, allowing you to install packages smoothly with pip or pip3 once more. If you need to restore this mechanism later, simply recreate the file using:

sudo touch EXTERNALLY-MANAGED

Read: How to install pip on Ubuntu 18.04 or Ubuntu 20.04

Solution 2: Leverage Virtual Environments

Alternatively, you can utilize virtual environments for your Python projects. Virtual environments isolate project-specific packages from the operating system and provide a neat organizational structure. Create and activate a virtual environment, then install packages within it:

1. Create the virtual environment:

python3 -m venv venv

2. Activate the virtual environment:

source venv/bin/activate

3. Your shell prompt will update to reflect the active virtual environment. You can now install Python packages without encountering the “externally-managed-environment” error.

Read: How is the path environment variable managed in Linux/Ubuntu/Debian

Solution 3: Employ pipx for Package Installation

pipx is a utility designed to install Python packages in virtual and isolated environments, automating tasks like creating virtual environments and symbolic links. To use pipx and avoid the error, first install it using your distribution’s package manager:

# Ubuntu/Debian

sudo apt-get install pipx

# Arch-based systems

sudo pacman -S pipx

# Fedora/CentOS/RHEL

sudo dnf install pipx

Once installed, use pipx to install packages just as you would with pip:

Consult the `man` page for more information on pipx.

By implementing one of these three solutions, you’ll be well-equipped to conquer the “externally-managed-environment” error and continue working seamlessly with Python packages on your Linux distribution.

 


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Leave a Reply