48

I upgraded to Ubuntu 23.04. Now, when I run a pip command (installed using sudo apt install python3-pip), I get this error:

$ pip install --user <foobar>
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.

What does this mean? How can I avoid this error?

What if I want to install a package user-wide (--user), not system-wide? How do I do that?

Flimm
  • 44,031

6 Answers6

47

There's a good article on OMGUbuntu about this: 3 Ways to Solve Pip Install Error on Ubuntu 23.04

Here's the summary. There are three ways to approach this problem:

1. Install the Python package using APT

For instance, if you want to install the requests Python library, you can install it using APT instead, like this:

sudo apt install python3-requests

This will install this library system-wide.

Not all packages available on PyPI have been packaged and included in the Debian/Ubuntu repositories, so this method won't work for some packages.

Or: 2. Create a virtual environment using venv or virtualenv

Make sure venv is installed by running:

sudo apt install python3-venv

To create a new virtual environment in a directory named .venv, run:

python3 -m venv .venv

To activate this virtual environment (which modifies the PATH environment variable), run this:

source .venv/bin/activate

Now you can install a library like requests in this virtual environment:

pip install requests

The files will get installed under the .venv/ directory.

If you want to leave the virtual environment, you can run:

deactivate

If you don't want to run source .venv/bin/activate and deactivate, then you can run the executable by prefixing its path, like this:

 $ .venv/bin/pip install requests
 $ .venv/bin/python3
 >>> import request
 >>> help(requests)

Or: 3. Use pipx

pipx lets you install and run Python applications in isolated environments. This is the recommended way to install PyPI packages that represent command-line applications.

To install pipx, run:

 sudo apt install pipx

pipx needs ~/.local/bin/ to be in your PATH. You can automatically modify your shell configuration (such as ~/.bashrc) to modify PATH appropriately by running:

 pipx ensurepath

(You may need to close your terminal application and open it again for the changes to take effect.)

Now you can install a package from PyPI, like this:

 pipx install pycowsay

And you can run the command that you just installed, like this:

$ pycowsay Mooo!

< Mooo! >

\ ^^ \ (oo)_______ ()\ )/
||----w | || ||

As you can see, pipx installed a symlink in ~/.local/bin/ to the executable in a virtual environment:

$ ls -l ~/.local/bin/pycowsay
lrwxrwxrwx 1 flimm flimm 50 May 24 11:19 /home/flimm/.local/bin/pycowsay -> /home/flimm/.local/pipx/venvs/pycowsay/bin/pycowsay*

Or: 4. Pass --break-system-packages flag:

If you want to ignore the warning, you can pass the --break-system-packages flag:

pip install --break-system-packages --user <foobar>

This method is not recommended, because you may find yourself with mysterious broken installations of Python packages months or years later, after you've forgotten that you used --break-system-packages and installed other conflicting Python packages.

Flimm
  • 44,031
10

In a terminal, delete this file with:

sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED

and everything will be OK!

TommyPeanuts
  • 1,147
3

Had the same error on my Ubuntu 23.04. I tried pip install {package_name} --break-system-packages It worked for me. Hope it helps. Thank you.

2

A quick solution to this problem is directly using the python inside the env.

1.- Create the virtual environment: python -m venv <env_name>

2.- Activated it: source <env_name>/bin/activate

3.- Install the package you need: sudo venv/bin/python3 -m pip install <package_name>

This should fix it.

0

If you've got the venv set up and are still getting the error, make sure the venv folder has the correct permissions. I spent a long time spinning my wheels before I chown'd the folder to my username. Then do not use sudo.

Check folder owner:

  1. Go to the parent folder

  2. List the folder's owner with:

     ls -Al
    
  3. If the folder is owned by root in group root:

     chown -R username:username ./venvfolder/
    
-1

I had created the conda virtual environment, and facing the same issue with pip , so conda install kafka-python worked for me

Raj
  • 1
  • 1