Score:6

pip error on Ubuntu: externally-managed-environment × This environment is externally managed

bd flag

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?

Score:3
bd flag

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. Use a repo version

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 env, run:

python3 -m venv env

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

source env/bin/activate

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

pip install requests

The files will get installed under the env/ directory.

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

deactivate

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

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

Or: 3. Use pipx

pipx lets you install and run Python applications in isolated environments. This is the recommend 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

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>
Score:2
nr flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.