# How to require a virtualenv when installing packages with pip?


By default, [pip](https://pydevtools.com/handbook/reference/pip.md) 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](https://pydevtools.com/handbook/explanation/what-is-a-virtual-environment.md) 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`):

```bash
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](https://pip.pypa.io/en/stable/topics/configuration/#location) (`pip.conf` on macOS/Linux, `pip.ini` on Windows):

```ini
[global]
require-virtualenv = true
```

> [!TIP]
> [uv](https://pydevtools.com/handbook/reference/uv.md) avoids this problem entirely — it installs into a project-specific virtual environment by default and never modifies the global Python environment.

## Learn More

- [pip configuration documentation](https://pip.pypa.io/en/stable/topics/configuration/)
- [Why should I use a virtual environment?](https://pydevtools.com/handbook/explanation/why-should-i-use-a-virtual-environment.md)
