How to Fix Python Pip Install Error in Ubuntu 24.04

Last updated: March 26, 2024 — 1 Comment

When using pip command to install a Python package in Ubuntu 24.04 will output “error: externally-managed-environment“. Here are a few workarounds to ‘fix’ the issue.

As the terminal output shows you, it’s the change due to PEP 668. Since Ubuntu 23.04, it recommends Python-specific package management tools (e.g., pip) to install packages using a virtual environment, to avoid conflicts to packages installed by OS package managers. Though, user can still force pip to install into interpreter’s global context just like before.


The workarounds to this issue so far include:

  • Install the Python package from system repository if exist.
  • Force pip to install just like before in 22.04.
  • Use pipx instead (Python apps only).
  • Manually create virtual environment and install package into it.

Option 1: Install Python package from System Repository

Debian and Ubuntu include a few thousands of Python packages in system repositories. Before installing a package via pip, it’s better to first take a look if a corresponding OS package is available.

To do so, either use Synaptic package manager (available in App Center) or run sudo apt install python3- command and press “Tab” to list available choices.

Also the packages.ubuntu.com web page provides search function to find packages from system repository.

Option 2: Force pip to install just like before

As the terminal error output shows, user can bypass by adding --break-system-packages flag.

For example, force install imagesearch library via command:

pip install --user imagesearch --break-system-packages

Without typing --break-system-packages every time running the pip install command, you may write the rule into configuration file. To do so, simply run commands:

mkdir -p ~/.config/pip
echo -e "[global]\nbreak-system-packages=true" > ~/.config/pip/pip.conf

The commands will first create ~/.config/pip folder if not exist, then create pip.conf config file and write following lines into it:

[global]
break-system-packages=true

After that, you can use pip install command to install Python packages just like in Ubuntu 22.04 and earlier

Option 3: Use pipx

Pipx is an alternative command line tool to install and run Python applications in isolated environments. It’s however for Pythons apps only.

To install and setup pipx, simply run command:

sudo apt install pipx
pipx ensurepath

Then you can install a Python app via pipx, for example cowsay, use command:

pipx install cowsay

Then run it via:

pipx run cowsay --text "Mooo"

And use command to uninstall Python package:

pipx uninstall cowsay

For more about pipx, either run pipx --help or go to its project page.

Option 4: Manually Create Virtual Environment for Installing Python package

1. If you want to manually create virtual environment for installing a Python package in Ubuntu 24.04, then open terminal and run command:

python3 -m venv ~/.venv/cowsay

This command will create .venv/cowsay directory in your user home (replace cowsay to your own name). By running ls ~/.venv/cowsay, you can see that the folder contains a copy of Python executables and libraries.

2. After running the command above to create the virtual Python environment, you may then run the pip command from that directory to install a Python package. For example:

~/.venv/cowsay/bin/pip install cowsay

Finally, run the installed Python app from that virtual environment by running command:

~/.venv/cowsay/bin/cowsay -t "Mooooo"

You can install more Python packages into this single environment (.venv/cowsay in my case), or run python3 -m venv ~/.venv/NAME_HERE again to create more virtual environments to use for other packages.

Add the Virtual Environment to your PATH

Without running the full path to pip and python executable files, you may add the virtual python environment to your PATH.

To do so, either run command which works only in current terminal window:

export PATH=$HOME/.venv/cowsay/bin:$PATH

Or add the command above into user’s .profile file by running the command below. So, it will work anywhere in next login! To do so, first run command to edit user profile file:

nano ~/.profile

When file opens, scroll down add new line export PATH=$HOME/.venv/cowsay/bin:$PATH (replace cowsay to yours), finally press Ctrl+S to save, Ctrl+X to exit.

Summary:

In this tutorial, I taught how to workaround the pip install command error in Ubuntu 24.04.

For lazy men who want to make it work like before, just add --break-system-packages command option, though it might break things. While, the recommended way is to either install a replacement through OS package (if any), or create virtual environment and install the Python package into it.

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via ubuntuhandbook1@gmail.com Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

One response to How to Fix Python Pip Install Error in Ubuntu 24.04

  1. Thank you very much! Hugs from Brazil!

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*