I have tried to install any python packages on Ubuntu 24.04, but found I cannot do that as in 22.04
PEP668 said it is for avoiding package conflict between system-wide package and user installed package.
example:
$ pip install setuptools --user
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.
But if I do that with pipx
:
$ pipx install setuptools
No apps associated with package pip or its dependencies. If you are attempting to install a library, pipx should not be used. Consider using pip or a similar tool instead.
I am really confused with current rules and can not install any package to user local env.
How can I manage my user local environment now? And how can I use latest pip
(not linux-distro version) and other packages by default for current user?
My Environment (dockerfile just for reproduce):
FROM ubuntu:24.04
# add python
RUN apt install -y python3-pip python3-venv python-is-python3 pipx
USER ubuntu
WORKDIR /app
I know I can use some env manage tools (pyenv) to do that, but is there any built-in method to bring my user local env back?
3
Answers
You tried to follow the instructions for installing an application. You need to follow the instructions for installing a package:
Use
python3 -m venv whateverpath
to create a virtual environment in the specified path, andwhateverpath/bin/python
andwhateverpath/bin/pip
to run that virtual environment’s Python or to install packages in that virtual environment.Alternatively, if you are absolutely sure you would rather take the risk of breaking your system than use a virtual environment, you can run
pip
with the--break-system-packages
flag, as thepip
output said. This will not be safe, even if you use--user
.As mentioned by @user2357112, creating a virtual environment is mandatory for any project requiring external packages.
Once you have pip3 and venv installed, go to your directory to create the yourenv environment:
Visually, you know you’re in thanks to the modified prompt that begins with (yourenv).
Don’t forget to update pip within the virtual env, and you’re good to go!
You can write pip instead of pip3 now.
To exit yourenv, you have to write deactivate unlike to a pipenv environment where Ctrl+D is enough:
Enjoy 🙂
Externally Managed Environments
One point I’d like to emphasize from the formal PEP668 specification
The intent here is to protect Ubuntu tooling written in python from breaking when you update or modify packages (eg update manager, system config printer, landscape client, etc)
Your local binary of python is symlinked to the system binary. See this post to understanding user package management
Containers and/or Virtual Environments
I’ll echo the previous answers here: you run your programs from within a virtual environment (a thorough tutorial; my preferred method is with miniconda)
Or, if you’re more familiar with Docker then there are a number of python version specific images
For example
Test System
I’m not sure why you were installing all those python packages in your docker image — I had to remove the RUN line to get my image to build
Docker build error using your example image