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?
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.
4. Override system settings (Not Recommended)
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
Pipx is a tool that lets you install and run applications in isolated virtual environments, providing isolation and global access to installed applications.
You can use pipx to install compatible applications, but some may need extra setup or dependencies. Follow specific app instructions.
Operating systems like Debian or Ubuntu restrict direct package installations to ensure security and compatibility with other programs installed on the system. Installing packages system-wide can cause conflicts, dependencies issues, and security vulnerabilities.
No, it is not recommended to bypass the security measures put in place by your operating system. Doing so can lead to compatibility issues and may put your system at risk. Only do this if you have a good reason and fully understand the consequences.
You can find a comprehensive guide on how to create virtual environments here. Additionally, there are several online resources available for learning more about virtual environments and their benefits. You can also refer to the official Python documentation for more information. https://docs.python.org/3/library/venv.html Overall, experimenting with creating and using virtual environments is the best way to understand their functionality. So, don’t be afraid to try it out!