How to require a virtualenv when installing packages with pip?

By default, pip allows installing packages into the global Python environment. This can lead to dependency conflicts and break system tools. Configuring pip to require a virtual environment prevents accidental global installs.

Using an environment variable

Set the PIP_REQUIRE_VIRTUALENV environment variable to true in your shell profile (e.g. ~/.bashrc, ~/.zshrc):

export PIP_REQUIRE_VIRTUALENV=true

With this set, running pip install outside a virtual environment produces an error:

ERROR: Could not find an activated virtualenv (required).

Using pip configuration

Alternatively, add the following to your pip configuration file (pip.conf on macOS/Linux, pip.ini on Windows):

[global]
require-virtualenv = true

Tip

uv avoids this problem entirely — it installs into a project-specific virtual environment by default and never modifies the global Python environment.

Learn More

Last updated on

Please submit corrections and feedback...