Safeguard
Security Guides•

How do you stop pip from installing into the global Python?

Set pip's require-virtualenv option so installs outside a virtual environment fail. Learn the setting, where to put it, and why it limits damage.

Safeguard Security Team
4 min read

Turn on pip's require-virtualenv setting, either as a flag or through the PIP_REQUIRE_VIRTUALENV environment variable. Pip then exits with an error unless it is running inside a virtual environment. A mistaken install can no longer change the system Python, and a bad package stays inside a folder you can delete.

The setting

The pip general options reference describes it in one sentence: allow pip to only run in a virtual environment, and exit with an error otherwise. It lists two environment variable names, PIP_REQUIRE_VIRTUALENV and PIP_REQUIRE_VENV.

You can set it in three places, matching the precedence rules from the pip configuration guide.

# one command
python -m pip install --require-virtualenv requests

# a shell profile
export PIP_REQUIRE_VIRTUALENV=true

# a pip config file, applied to every command
python -m pip config set global.require-virtualenv true

The config file route uses the global prefix described in the pip config reference, which affects any command. Remember that environment variables override config files, so a user could set the variable to a false value. For strict control, set it where users cannot easily undo it, such as a managed image.

Why it matters

Installing into the global interpreter has a wider reach than people expect.

  1. Shared state. Packages in the global site directory are visible to every script run with that interpreter, including system tools on some operating systems.
  2. Hard cleanup. Removing a bad package from a shared environment can leave dependencies behind and break other things.
  3. Mixed trust. One developer's experiment becomes everyone's import.
  4. Privilege. Global installs often need elevated rights, which is the wrong time to run unreviewed install code.

A virtual environment limits the blast radius. Delete the folder and you are back to a clean state. See do you need a virtual environment for security and why pip says externally managed environment.

Where to enforce it

PlaceEffect
Developer shell profileStops accidental global installs on laptops
Base image for CIMakes jobs fail early when a script forgets to activate
Container imageForces a deliberate decision about the environment
Team onboarding docSets the habit

In a container, a global install can be the intended design, since the container is the isolation boundary. In that case, do not set the variable, and instead keep the image small and the install step hashed.

A rollout plan

  1. Set the variable in CI first. Failures show you scripts that rely on global installs.
  2. Fix those scripts to create and use a virtual environment.
  3. Roll it out to developer machines through the shell profile or a managed config.
  4. Check with pip config list that the setting is active.

Limits

The setting does not make installs safe. A package installed into a virtual environment can still run code when you import it. It only controls where installs go. Combine it with hash checking and review of new dependencies.

Safeguard's dependency scanning shows which packages ended up in a project's environment, so unexpected additions are easier to spot. A free scan is available at gold.safeguard.sh/scan.

Quick check

Does require-virtualenv block installs inside a venv?

No. It only blocks pip when it is not running in a virtual environment.

Can a user override the setting?

Possibly. Environment variables and command line options take precedence over config files. Set it in a managed place for stricter control.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.