If you make the upgrade to Ubuntu 23.04 and try to run ‘pip install’ you’ll notice it now throws an error – but it’s not a bug.
The reason why the pip install
command doesn’t work in Ubuntu 23.04 is down to an intentional shift in policy (also taken in Ubuntu’s upstream, Debian) to avoid conflicts between the Python package manager and Ubuntu’s underlying APT.
Basically, you can’t run pip install
outside a virtual environment in Ubuntu 23.04. If you try to, you get a “externally managed environment” error similar to this:
The good news is that there are a number of workarounds available that don’t involve risking your system’s stability by force-installing packages to override the policy change.
Contents
Installing Python Apps in Ubuntu 23.04
1. Use a repo version
If the Python package you require is in the Ubuntu/Debian repos you can install it from there. E.g., sudo apt install python3-foo
, where ‘foo’ is the name fo the package you want.
If you’re not sure if the package you want is in the repo just type python3-
then hit the tab
key to see a print out of everything available therein.
2. Create a virtual environment with venv
If the Python package you want to install is not in the Ubuntu repos, or you want to install a more recent version of it than Ubuntu has available, you can create a virtual environment using venv
.
First install it:
sudo apt install python3-venv
Then run:
python3 -m venv .venv/anynamehere
This will create a directory in your home folder called .venv
. Inside this is your virtual environment (named what you chose) with a copy of python and pip that you can use to install what you need inside of the environment.
For example, I created a virtual environment called ‘eternia’, then used the ‘pip’ binary inside of this to install (rather pointlessly as it no longer works, but it was the first one that came to mind) CLI Twitter client Rainbowstream:
This isn’t the most awful workaround but it is a little ‘messy’ as you need to remember to specifically refer to the virtual environment when you need to run something and, frankly.
Use pipx (recommended)
Finally, you may be able to use pipx
. The “catch” is that pipx does not work with Python libraries so if you need those, this choice is out. But, if what you want is a Python app then this it is leagues easier to use pipx.
Why?
Well, pipx a) automatically makes a virtual environment for each app you install, and then b) automatically makes a link to it in .local/bin
so that you can run it from the command line as though everything was as it used to be — nice!
Run this command to install pipx on Ubuntu:
sudo apt install pipx
Add it your PATH (so you can run things globally):
pipx ensurepath
Then install apps like so, where ‘foo’ is the name of the app you want to install:
pipx install foo
For example, to install the Please terminal start page tool I just run pipx install please-cli
followed by please
(as pipx automatically creates a symbolic link to it for me and I’ve run the pipx ensurepath
command).
Easy as that!
Wrapping Up
In this post I showed you three ways to workaround the pip policy change in Ubuntu 23.04 that means you can’t simply run the pip install as in earlier versions. Pipx is by far the easiest and most elegant solution as I reduces friction, allowing you to focus on installing and running your fave Python apps.