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.

pip externally managed error

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 theofficial PEP-668 documentation. If you wish to revert or override this mechanism, you can take three approaches.

deleting EXTERNALLY-MANAGED file

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.xxand delete theEXTERNALLY-MANAGEDfile in the directory. Here are the commands to do so:

That’s all you need to do to fix the error. If you choose to restore this mechanism, create the same file again withthe touch command:

using venv solves the externally managed error

Now you should be able to install packages seamlessly with pip or pip3.

2. Use Virtual Environments to Install pip Packages

Another approach you’re able to take is usingvirtual 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:

installing packages with pipx

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, theopenaiPython 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/binfolder 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:

On Arch-based systems:

On Fedora/CentOS/RHEL:

Once you’ve installed pipx, use it just like you’d use pip to install packages. Here’s a sample command:

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.