The pip cache isn’t just a place to store downloaded packages; it’s a sophisticated system that actively prevents redundant downloads, dramatically speeding up repeated installations by serving already-fetched wheels directly from disk.

Let’s see it in action. Imagine you’re setting up a new Python environment and need requests and numpy:

$ pip install requests numpy

The first time you run this, pip will go out to PyPI, download the wheels for requests and numpy, and then install them. Crucially, it also stores these downloaded wheels in its cache.

Now, let’s say you’re setting up another environment, or perhaps you accidentally delete your venv and need to reinstall:

$ pip install requests numpy

This time, pip will first check its cache for requests and numpy. If it finds them, it will skip the download from PyPI entirely and use the cached wheels directly. The output will look slightly different, indicating that it’s using the cache:

Collecting requests
  Using cached requests-2.31.0-py3-none-any.whl (62 kB)
Collecting numpy
  Using cached numpy-1.26.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.2 MB)
...
Successfully installed numpy-1.26.0 requests-2.31.0

The "Using cached" lines are the magic. You just saved network bandwidth and time.

The mental model for pip’s cache is simple: it’s a local repository of package wheels. When you request a package, pip consults this local repository before hitting the network. If a matching wheel (correct package, version, and Python/platform compatibility) is found, it’s used. If not, it’s downloaded from the remote index (like PyPI) and then added to the cache for future use.

The primary lever you control with the cache is its location and whether it’s used at all. The default cache directory is usually located in your user’s home directory, often under .cache/pip on Linux/macOS or %LOCALAPPDATA%\pip\Cache on Windows.

You can explicitly control the cache location using the PIP_CACHE_DIR environment variable:

$ export PIP_CACHE_DIR="/path/to/my/custom/pip/cache"
$ pip install requests

This will make pip use /path/to/my/custom/pip/cache as its cache directory. If it doesn’t exist, pip will create it.

You can also disable the cache entirely, which is useful for ensuring you always get the absolute latest versions from the index or for debugging:

$ pip install --no-cache-dir requests

This command tells pip to ignore the cache for this specific installation. It will download requests from PyPI even if a cached version is available, and it won’t add the newly downloaded wheel to the cache.

The cache mechanism works by creating a directory structure within the cache root that mirrors the package and version. Inside, you’ll find .whl files. The naming convention for these cached files is crucial: it includes the package name, version, and sometimes build metadata, ensuring that pip selects the exact correct wheel for your environment. If you have multiple Python versions or platforms installed, pip will maintain separate cache entries for each, preventing compatibility issues.

A common misconception is that the cache always speeds things up. While true for repeated identical installs, if you frequently upgrade packages or switch between different Python versions or platforms on the same machine, the cache might grow large and potentially contain stale or incompatible wheels. Regularly clearing the cache can be beneficial in such scenarios. You can manually delete the contents of the PIP_CACHE_DIR, or use pip’s built-in command for this:

$ pip cache purge

This command will remove all cached wheels from your pip cache directory. It’s a safe operation that simply frees up disk space and ensures subsequent installs will perform fresh downloads.

The next concept to explore is how pip handles different package types and the implications for caching, particularly with source distributions versus wheels.

Want structured learning?

Take the full Pip course →