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.