|
| 1 | +--- |
| 2 | +description: Learn how to handle externally managed environment errors when installing Semgrep using pip. |
| 3 | +tags: |
| 4 | + - Semgrep AppSec Platform |
| 5 | + - Semgrep Code |
| 6 | + - Semgrep Secrets |
| 7 | + - Semgrep Supply Chain |
| 8 | +--- |
| 9 | + |
| 10 | +# error: externally-managed-environment |
| 11 | + |
| 12 | +If your Python environment is [externally managed by a package manager](https://packaging.python.org/en/latest/specifications/externally-managed-environments/), you can't use `pip` for system-wide installations. This results in the `externally-managed-environment` when you try to use `pip` to install Semgrep. |
| 13 | + |
| 14 | +Error message on macOS: |
| 15 | + |
| 16 | +```console |
| 17 | +error: externally-managed-environment |
| 18 | + |
| 19 | +× This environment is externally managed |
| 20 | +╰─> To install Python packages system-wide, try brew install |
| 21 | + xyz, where xyz is the package you are trying to |
| 22 | + install. |
| 23 | + |
| 24 | + If you wish to install a Python library that isn't in Homebrew, |
| 25 | + use a virtual environment: |
| 26 | + |
| 27 | + python3 -m venv path/to/venv |
| 28 | + source path/to/venv/bin/activate |
| 29 | + python3 -m pip install xyz |
| 30 | + |
| 31 | + If you wish to install a Python application that isn't in Homebrew, |
| 32 | + it may be easiest to use 'pipx install xyz', which will manage a |
| 33 | + virtual environment for you. You can install pipx with |
| 34 | + |
| 35 | + brew install pipx |
| 36 | + |
| 37 | + You may restore the old behavior of pip by passing |
| 38 | + the '--break-system-packages' flag to pip, or by adding |
| 39 | + 'break-system-packages = true' to your pip.conf file. The latter |
| 40 | + will permanently disable this error. |
| 41 | + |
| 42 | + If you disable this error, we STRONGLY recommend that you additionally |
| 43 | + pass the '--user' flag to pip, or set 'user = true' in your pip.conf |
| 44 | + file. Failure to do this can result in a broken Homebrew installation. |
| 45 | + |
| 46 | + Read more about this behavior here: <https://peps.python.org/pep-0668/> |
| 47 | + |
| 48 | +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. |
| 49 | +hint: See PEP 668 for the detailed specification. |
| 50 | +``` |
| 51 | + |
| 52 | +Error message on Ubuntu: |
| 53 | + |
| 54 | +```console |
| 55 | +error: externally-managed-environment |
| 56 | + |
| 57 | +× This environment is externally managed |
| 58 | +╰─> To install Python packages system-wide, try apt install |
| 59 | + python3-xyz, where xyz is the package you are trying to |
| 60 | + install. |
| 61 | + |
| 62 | + If you wish to install a non-Debian-packaged Python package, |
| 63 | + create a virtual environment using python3 -m venv path/to/venv. |
| 64 | + Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make |
| 65 | + sure you have python3-full installed. |
| 66 | + |
| 67 | + If you wish to install a non-Debian packaged Python application, |
| 68 | + it may be easiest to use pipx install xyz, which will manage a |
| 69 | + virtual environment for you. Make sure you have pipx installed. |
| 70 | + |
| 71 | + See /usr/share/doc/python3.12/README.venv for more information. |
| 72 | + |
| 73 | +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. |
| 74 | +hint: See PEP 668 for the detailed specification. |
| 75 | +``` |
| 76 | + |
| 77 | +## How to fix this error |
| 78 | + |
| 79 | +For most users, the simplest solution is to install [`pipx`](https://github.com/pypa/pipx), then run `pipx install semgrep` to install Semgrep. `pipx` automatically creates and manages virtual environments for standalone Python applications. |
| 80 | + |
| 81 | +You can also install Semgrep using [`homebrew`](https://brew.sh/). |
| 82 | + |
| 83 | +If you're already using a custom Python virtual environment, you can install Semgrep in this existing environment instead. |
0 commit comments