skip to Main Content
pip install -r requirements.txt
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.

I wish someone would explain to me what to do and how to solve it

5

Answers


  1. This is due to your distribution adopting PEP 668 – Marking Python base environments as “externally managed”.

    TL;DR: Use a venv:

    python3 -m venv .venv
    source .venv/bin/activate
    python3 -m pip install -r requirements.txt
    

    Long story: Your distribution is trying to protect you against mixing apt provided packages and pip provided packages. Mixing two package managers (apt and pip here) is always a bad idea and the source of many issues.

    PEP 668 is a way for distributions to explicitly tell users to avoid falling is this pit. Your distribution told you three solutions in the message, but only the 2nd one applies cleanly to your use case:

    • Using apt install python3-xxx. It does not cleanly apply for you as you’re having a requirements.txt, not a single dependency. It would work if you have only a few requirements in the file and can do it manually for each, like apt install python3-xxx python3-yyy python3-zzz. In this case there’s no weird mixing of package managers: you installed python3 using apt, you’re installing your dependencies using apt: no surprises.
    • Using a venv: python3 -m venv .venv then source .venv/bin/activate, then pip install -r requirements.txt. In this case the installation is contained inside the .venv directory: no mixing of what apt does and what pip does, no surprises.
    • Using pipx which does not cleanly apply to your case, pipx is good to install and use a program, like pipx install black, but here you need to install libraries listed in a requirement file, not a program.

    There’s another way, that I often use myself because I often need multiple different Python versions:

    • Use a Python not provided by your distrib, so it does not mess with apt installed things and does not adopt PEP 668. I often compile many Python interpreters myself using a short bash function which use a --prefix=~/.local/, for testing purposes. With those Pythons I use either a venv either a good old pip install, in this case pip will install it in ~/.local/, again no clash between apt and pip, no bad surprises.
    Login or Signup to reply.
  2. i faced the same issue it was because of python3.11
    i switched to python3.10 and it working fine now.

    Login or Signup to reply.
  3. the message already passes the solution

    × 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.
    

    can you try this

    sudo apt install python3-<package>
    

    it worked for me

    Login or Signup to reply.
  4. I was faced with this error inside pyenv after upgrading to Ubuntu 23.04. As part of this upgrade, the system Python was also upgraded from 3.10 to 3.11. Re-installing pyenv didn’t help. I had to:

    1. remove all instances of pip discovered via which pip (and also sudo which pip as it kept pointing me to .pyenv/shims and root user didn’t have pyenv installed)
    2. switch to any pyenv installed python with pyenv global <version>
    3. install pip back with get-pip.py:
      wget https://bootstrap.pypa.io/get-pip.py
      python get-pip.py
      rm get-pip.py
      
    Login or Signup to reply.
  5. use --break-system-packages at the end of pip

    this will install package in you local user directory ~/.local/lib/python3.11

    pip install xyz --break-system-packages
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search