Skip to content

Commit df744a1

Browse files
authored
add notes on how to handle "error: externally-managed-environment" during install/upgrade (#2160)
1 parent 541496e commit df744a1

File tree

8 files changed

+100
-1
lines changed

8 files changed

+100
-1
lines changed

docs/deployment/add-semgrep-other-ci.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ To create a `SEMGREP_APP_TOKEN`, follow these steps:
6666
1. Add Semgrep to your CI pipeline. Do either of the following:
6767
1. Reference or add the [Semgrep Docker image](https://hub.docker.com/r/semgrep/semgrep). This is the recommended method.
6868
2. Add `pip install semgrep` into your configuration file as a step or command, depending on your CI provider's syntax.
69+
- If you see an error during the installation process due to your Python environment being externally managed by a package manager, see [Semgrep's article for instructions on how to proceed](/kb/semgrep-appsec-platform/error-externally-managed-environment).
6970
2. Add `semgrep ci` as a step or command.
7071
3. Set the `SEMGREP_APP_TOKEN` environment variable within your configuration file.
7172

docs/getting-started/quickstart.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ You must have Python 3.9 or later installed on the machine where the Semgrep CLI
7777
# install through pip
7878
python3 -m pip install semgrep
7979

80+
# if you get the following error "error: externally-managed-environment",
81+
# see semgrep.dev/docs/kb/semgrep-appsec-platform/error-externally-managed-environment
82+
8083
# confirm installation succeeded by printing the currently installed version
8184
semgrep --version
8285
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.

docs/kb/semgrep-code/run-specific-version.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Install a specific Semgrep version using pip's version syntax:
2323

2424
<pre class="language-bash"><code>python3 -m pip install semgrep==<span className="placeholder">x.y.z</span></code></pre>
2525

26+
If you see an error during the installation process due to your Python environment being externally managed by a package manager, see [Semgrep's article for instructions on how to proceed](/kb/semgrep-appsec-platform/error-externally-managed-environment).
27+
2628
Confirm installation:
2729

2830
```

docs/semgrep-ci/sample-ci-configs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ You can customize the scan by entering custom rules or other rulesets to scan wi
477477

478478
To run Semgrep CI on any other provider, use the `semgrep/semgrep` image, and run the `semgrep ci` command with `SEMGREP_BASELINE_REF` set for diff-aware scanning.
479479

480-
**Note**: If you need to use a different Docker image or are not running in Docker, install Semgrep CI by `pip install semgrep`.
480+
**Note**: If you need to use a different Docker image or are not running in Docker, install Semgrep CI by `pip install semgrep`. If you see an error during the installation process due to your Python environment being externally managed by a package manager, see [Semgrep's article for instructions on how to proceed](/kb/semgrep-appsec-platform/error-externally-managed-environment).
481481

482482
By setting various [CI environment variables](/semgrep-ci/ci-environment-variables), you can run Semgrep in the following CI providers:
483483

docs/semgrep-code/semgrep-pro-engine-intro.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ Cross-file analysis uses a separate `semgrep` binary. To update to the latest ve
9393

9494
```bash
9595
python3 -m pip install --upgrade semgrep
96+
97+
# if you get the following error "error: externally-managed-environment",
98+
# see semgrep.dev/docs/kb/semgrep-appsec-platform/error-externally-managed-environment
9699
```
97100

98101
</TabItem>

docs/update.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ brew upgrade semgrep
3232
# macOS, Linux, or Windows Subsystem for Linux (WSL) users, using pip
3333
python3 -m pip install --upgrade semgrep
3434

35+
# If you get the following error "error: externally-managed-environment", see
36+
# semgrep.dev/docs/kb/semgrep-appsec-platform/error-externally-managed-environment
37+
python3 -m pipx install --upgrade semgrep
38+
3539
# confirm your Semgrep installation
3640
semgrep --version
3741
```

src/components/procedure/_install-cli.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ brew install semgrep
1111
# macOS, Linux, or Windows Subsystem for Linux (WSL) users
1212
python3 -m pip install semgrep
1313

14+
# if you get the following error "error: externally-managed-environment",
15+
# see semgrep.dev/docs/kb/semgrep-appsec-platform/error-externally-managed-environment
16+
1417
# confirm
1518
semgrep --version
1619
```

0 commit comments

Comments
 (0)