skip to Main Content

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


  1. You tried to follow the instructions for installing an application. You need to follow the instructions for installing a package:

    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.
    

    Use python3 -m venv whateverpath to create a virtual environment in the specified path, and whateverpath/bin/python and whateverpath/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 the pip output said. This will not be safe, even if you use --user.

    Login or Signup to reply.
  2. 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:

    $ python3 -m venv yourenv
    $ . yourenv/bin/activate
    

    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.

    (yourenv) $ pip install -U pip
    (yourenv) $ pip install package1 package2
    

    To exit yourenv, you have to write deactivate unlike to a pipenv environment where Ctrl+D is enough:

    (yourenv) $ deactivate
    $ 
    

    Enjoy 🙂

    Login or Signup to reply.
  3. Externally Managed Environments

    One point I’d like to emphasize from the formal PEP668 specification

    It also standardizes an interpretation of the sysconfig schemes so that, if a Python-specific package manager is about to install a package in an interpreter-wide context, it can do so in a manner that will avoid conflicting with the external package manager and reduces the risk of breaking software shipped by the external package manager.

    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

    How can I manage my user local environment now?

    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

    FROM python:3.9
    
    WORKDIR /app
    
    COPY requirements.txt .
    
    RUN pip install --no-cache-dir -r requirements.txt
    
    COPY ./app /app
    

    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 -t ubuntu-test-sys .
    docker run ubuntu-test-sys
    docker ps
    docker exec -it dcb3330497e9 sh
    
    # python --version
    Python 3.9.18
    
    # python3 --version
    Python 3.9.18
    
    # ls -lah /usr/local/bin/ | grep python
    lrwxrwxrwx 1 root root    7 Dec 19 20:58 python -> python3
    lrwxrwxrwx 1 root root   14 Dec 19 20:58 python-config -> python3-config
    lrwxrwxrwx 1 root root    9 Dec 19 20:58 python3 -> python3.9
    lrwxrwxrwx 1 root root   16 Dec 19 20:58 python3-config -> python3.9-config
    -rwxr-xr-x 1 root root  18K Dec 19 20:58 python3.9
    -rwxr-xr-x 1 root root 3.0K Dec 19 20:58 python3.9-config
    
    # ls -lah /usr/bin/ | grep python
    lrwxrwxrwx 1 root root     24 Mar 13  2023 pdb3.11 -> ../lib/python3.11/pdb.py
    lrwxrwxrwx 1 root root     31 Apr  9  2023 py3versions -> ../share/python3/py3versions.py
    lrwxrwxrwx 1 root root     10 Apr  9  2023 python3 -> python3.11
    -rwxr-xr-x 1 root root   6.6M Mar 13  2023 python3.11
    

    Docker build error using your example image

     => ERROR [2/3] RUN apt install -y python3-pip python3-venv python-is-python3 pipx                                                   0.3s
    ------
     > [2/3] RUN apt install -y python3-pip python3-venv python-is-python3 pipx:
    #0 0.205
    #0 0.205 WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
    #0 0.205
    #0 0.208 Reading package lists...
    #0 0.220 Building dependency tree...
    #0 0.221 Reading state information...
    #0 0.221 E: Unable to locate package python3-pip
    #0 0.221 E: Unable to locate package python3-venv
    #0 0.221 E: Unable to locate package python-is-python3
    #0 0.221 E: Unable to locate package pipx
    ------
    Dockerfile:4
    --------------------
       2 |
       3 |     # add python
       4 | >>> RUN apt install -y python3-pip python3-venv python-is-python3 pipx
       5 |
       6 |     USER ubuntu
    --------------------
    ERROR: failed to solve: process "/bin/sh -c apt install -y python3-pip python3-venv python-is-python3 pipx" did not complete successfully: exit code: 100
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search