The proper way to install Python libraries and applications is to install them in a Python virtual environment whenever possible (the exceptions to this rule are quite rare).
The error message describes two common ways to accomplish this: either by creating a virtual environment yourself, or for applications, by using pipx—a tool which will create a virtual environment for you and install the application in that virtual environment.
pipx is strongly recommended for installing applications, i.e., when you will primarily use the installed code from the command line. On Debian systems and Debian-based systems such as Ubuntu, you can install pipx using apt, and then use pipx to install the application:
apt install pipx
pipx install some-python-application
For libraries, i.e., when you will use the code primarily by importing it in your own projects. Typically, you should create a virtual environment yourself. You can do this with venv from the standard library:
python -m venv my-venv
my-venv/bin/pip install some-python-library
See also this answer on a duplicate question for more details.
(Commonly, your own project may need several libraries. Make one virtual environment and install the libraries that your project needs side by side in that virtual environment.)
If you know you don’t need to use your system long-term (e.g. because you’re working in a container or CI environment like Docker or GitHub Actions), and you're still sure that you want to install packages "system-wide" and risk breaking your system (for example, by overwriting libraries that were part of tools written in Python that came with your system), Pip needs to be given permission to do so.
There are a few ways to do this:
For a single use of pip, add the
--break-system-packagesargument to the command.Add these lines to
~/.config/pip/pip.conf(this will enable every future run of Pip to break system packages:[global] break-system-packages = trueUse Pip's
configcommand to edit the above file (credit to The Matt from the comments):python3 -m pip config set global.break-system-packages true
Theoretically, removing or renaming the "marker" file (/usr/lib/python3.x/EXTERNALLY-MANAGED) would also disable the block, but this is a bad idea. The file was put there for a reason, and it's at least as easy to use the intended mechanisms instead.
The proper way to install Python libraries and applications is to install them in a Python virtual environment whenever possible (the exceptions to this rule are quite rare).
The error message describes two common ways to accomplish this: either by creating a virtual environment yourself, or for applications, by using pipx—a tool which will create a virtual environment for you and install the application in that virtual environment.
pipx is strongly recommended for installing applications, i.e., when you will primarily use the installed code from the command line. On Debian systems and Debian-based systems such as Ubuntu, you can install pipx using apt, and then use pipx to install the application:
apt install pipx
pipx install some-python-application
For libraries, i.e., when you will use the code primarily by importing it in your own projects. Typically, you should create a virtual environment yourself. You can do this with venv from the standard library:
python -m venv my-venv
my-venv/bin/pip install some-python-library
See also this answer on a duplicate question for more details.
(Commonly, your own project may need several libraries. Make one virtual environment and install the libraries that your project needs side by side in that virtual environment.)
If you know you don’t need to use your system long-term (e.g. because you’re working in a container or CI environment like Docker or GitHub Actions), and you're still sure that you want to install packages "system-wide" and risk breaking your system (for example, by overwriting libraries that were part of tools written in Python that came with your system), Pip needs to be given permission to do so.
There are a few ways to do this:
For a single use of pip, add the
--break-system-packagesargument to the command.Add these lines to
~/.config/pip/pip.conf(this will enable every future run of Pip to break system packages:[global] break-system-packages = trueUse Pip's
configcommand to edit the above file (credit to The Matt from the comments):python3 -m pip config set global.break-system-packages true
Theoretically, removing or renaming the "marker" file (/usr/lib/python3.x/EXTERNALLY-MANAGED) would also disable the block, but this is a bad idea. The file was put there for a reason, and it's at least as easy to use the intended mechanisms instead.
I have had this error since Python 3.11+.
I resolved it by moving the EXTERNALLY-MANAGED file elsewhere:
sudo mv /usr/lib/python3.11/EXTERNALLY-MANAGED /usr/lib/python3.11/EXTERNALLY-MANAGED.old
As Alok and JackLeEmmerdeur warn:
This deletion of file is not safe. This can lead to Broken Package Management, Conflicting Installations and Permission Issues
If that occurs, this change can be reverted by moving the .old file back.
Videos
Hello, I have recently been trying to install the pyautogui using pip like normal. When I encountered the environment was externally managed error. I tried multiple times and I have never found a solution. I even made a venv and tried to run the command in there. I don't know if I was doing it wrong but it still showed me the error. I reinstalled pip, same thing. I delete pip and python, reinstall both no difference. I even tried brew to see if there was a way to download it, nothing. I would greatly appreciate any sort of help thank you very much.
error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try brew install xyz, where xyz is the package you are trying to install. If you wish to install a Python library that isn't in Homebrew, use a virtual environment: python3 -m venv path/to/venv source path/to/venv/bin/activate python3 -m pip install xyz If you wish to install a Python application that isn't in Homebrew, it may be easiest to use 'pipx install xyz', which will manage a virtual environment for you. You can install pipx with brew install pipx You may restore the old behavior of pip by passing the '--break-system-packages' flag to pip, or by adding 'break-system-packages = true' to your pip.conf file. The latter will permanently disable this error. If you disable this error, we STRONGLY recommend that you additionally pass the '--user' flag to pip, or set 'user = true' in your pip.conf file. Failure to do this can result in a broken Homebrew installation. Read more about this behavior here: <https://peps.python.org/pep-0668/>
Every time I try to pip install a library through the Linux terminal, it says:
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.There's a good article on OMGUbuntu about this: 3 Ways to Solve Pip Install Error on Ubuntu 23.04
Here's the summary. There are three ways to approach this problem:
1. Install the Python package using APT
For instance, if you want to install the requests Python library, you can install it using APT instead, like this:
sudo apt install python3-requests
This will install this library system-wide.
Not all packages available on PyPI have been packaged and included in the Debian/Ubuntu repositories, so this method won't work for some packages.
Or: 2. Create a virtual environment using venv or virtualenv
Make sure venv is installed by running:
sudo apt install python3-venv
To create a new virtual environment in a directory named .venv, run:
python3 -m venv .venv
To activate this virtual environment (which modifies the PATH environment variable), run this:
source .venv/bin/activate
Now you can install a library like requests in this virtual environment:
pip install requests
The files will get installed under the .venv/ directory.
If you want to leave the virtual environment, you can run:
deactivate
If you don't want to run source .venv/bin/activate and deactivate, then you can run the executable by prefixing its path, like this:
$ .venv/bin/pip install requests
$ .venv/bin/python3
>>> import request
>>> help(requests)
Or: 3. Use pipx
pipx lets you install and run Python applications in isolated environments. This is the recommended way to install PyPI packages that represent command-line applications.
To install pipx, run:
sudo apt install pipx
pipx needs ~/.local/bin/ to be in your PATH. You can automatically modify your shell configuration (such as ~/.bashrc) to modify PATH appropriately by running:
pipx ensurepath
(You may need to close your terminal application and open it again for the changes to take effect.)
Now you can install a package from PyPI, like this:
pipx install pycowsay
And you can run the command that you just installed, like this:
$ pycowsay Mooo!
-----
< Mooo! >
-----
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
As you can see, pipx installed a symlink in ~/.local/bin/ to the executable in a virtual environment:
$ ls -l ~/.local/bin/pycowsay
lrwxrwxrwx 1 flimm flimm 50 May 24 11:19 /home/flimm/.local/bin/pycowsay -> /home/flimm/.local/pipx/venvs/pycowsay/bin/pycowsay*
Or: 4. Pass --break-system-packages flag:
If you want to ignore the warning, you can pass the --break-system-packages flag:
pip install --break-system-packages --user <foobar>
This method is not recommended, because you may find yourself with mysterious broken installations of Python packages months or years later, after you've forgotten that you used --break-system-packages and installed other conflicting Python packages.
In a terminal, delete this file with:
sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED
and everything will be OK!