Resolving the “Externally-Managed-Environment” Error in Python

Externally-Managed-Environment Error Solution

If you are a user of a debian-based Linux OS, then you might have run into a problem recently in the debian 11 or later version. When you try to install any package using Python that is managed by your operating system, you will get “error: externally-managed-environment”.

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.
hint: See PEP 668 for the detailed specification.

Why Does This Happen?

This message indicates that your Python environment is controlled by your operating system’s package manager (like apt on Debian-based systems). As a result, direct package installations using pip are restricted to avoid conflicts with system packages.

Operating systems like Debian or Ubuntu manage Python packages to ensure security and compatibility.

Installing packages system-wide can lead to security issues, dependencies issues, and can also break programs that are used by the operating system.

To avoid these issues Debian or Ubuntu have this security, which does not allow to install packages system-wide.

How to Resolve this Error?

Resolve Error

Here are some solutions to overcome this issue.

1. Installing system-wide packages

If you are installing packages that are available through your system package manager, then use apt:

sudo apt install python3-xyz

Replace xyz with the actual package name, for example:

sudo apt install python3-virtualenv

2. Using a Virtual Environment

Use Virtual Environment for installing packages that are not available in your system package manager.

1. Create a Virtual Environment:

python3 -m venv path/to/venv 

2. Activate the Virtual Environment:

source path/to/venv/bin/activate

3. Install Packages Using pip:

pip install package_name

Learn about virtual environments here.

3. Using pipx for Applications:

pipx is a great tool; it runs applications independently in isolated virtual environments.

pipx features:

  • Isolation: Each application gets its own virtual environment.
  • Global Access: Installed applications can be used from any directory.
  • Easy Installation: Simple command-line interface for installation.
  • Automatic Updates: Can be configured to automatically update installed applications.

Let’s see how to install and use it.

1. install pipx

sudo apt install pipx

2. Use pipx to install applications:

pipx install application_name

More information about pipx is here.

If you have a solid reason to install packages system-wide, then you can use the --break-system-packages flag, but be cautious:

Conclusion

“externally-managed-environment” is just an error because of some security measures to make the operating system more secure and compatible.

Resolving this issue is simple and straight forward.

This issue can be resolved by using virtual environments, system package managers, and pipx.

Reference:

https://peps.python.org/pep-0668

FAQs

Check Our Other Blogs