If you frequently work with Python and recently shifted to Ubuntu 23.04 or Fedora version 38, chances are you came across the "externally-managed-environment" error when installing packages with the Python package manager, pip.
This could be frustrating and quite surprising as this error would never pop up in the older versions of Ubuntu, Fedora, and other distributions. Let's learn what exactly is causing this error and how you can fix it quickly.
Why the "externally-managed-environment" Error Occurs
The latest versions of all Linux distributions are adopting the standards defined in PEP-668. These changes ensure that pip packages will not be installed in a global context by default.
This was implemented in an attempt to avoid conflict between the distribution's package manager and Python package management tools. You can learn about the details in the official PEP-668 documentation. If you wish to revert or override this mechanism, you can take three approaches.
1. Delete the "EXTERNALLY-MANAGED" File
This is the simplest fix to the pip "externally-managed" error. All you have to do is navigate to /usr/lib/python3.xx and delete the EXTERNALLY-MANAGED file in the directory. Here are the commands to do so:
cd /usr/lib/python3.11
sudo rm EXTERNALLY-MANAGED
That's all you need to do to fix the error. If you choose to restore this mechanism, create the same file again with the touch command:
sudo touch EXTERNALLY-MANAGED
Now you should be able to install packages seamlessly with pip or pip3.
2. Use Virtual Environments to Install pip Packages
Another approach you can take is using virtual environments for your Python project. Virtual environments are a great way to isolate program-specific packages from the operating system and also allow you to neatly organize your project.
Here's how to create a virtual environment and install packages with pip:
- First, create the virtual environment with:
python3 -m venv venv
- Source the activate file inside the bin directory using the source command:
source venv/bin/activate
Your shell prompt should be updated with the virtual environment name. Now you'll be able to install any Python packages you wish.
As you can see, the openai Python package couldn't be installed at first but after creating and switching to a virtual environment, it gets installed flawlessly.
3. Use pipx to Install Python Packages
pipx is a utility for installing Python packages in virtual and isolated environments. It automates steps like creating virtual environments for each package and creating symbolic links to the packages in the .local/bin folder so you can call each package from the shell at all times.
Using pipx to install packages helps you avoid the "externally-managed-environment" error as it installs packages in virtual environments. To use pipx, first, install it with the default package manager of your distribution.
On Ubuntu/Debian derivatives:
sudo apt-get install pipx
On Arch-based systems:
sudo pacman -S pipx
On Fedora/CentOS/RHEL:
sudo dnf install pipx
Once you've installed pipx, use it just like you'd use pip to install packages. Here's a sample command:
pipx install openai
To learn more about pipx, you can refer to its man page using the man command.
Now You Know How to Fix the "externally-managed" Error
After fixing the "externally-managed-environment" error, you should be able to continue working on your project or practicing general Python programming. If you're focused on the latter, you might want to check out the different gamified ways of learning programming that'll help you learn Python, or any language much quicker in a hands-on and rewarding way.