As a Python developer, you likely use pip regularly to install and manage Python packages. However, you may not realize that pip maintains a local cache of downloaded package files to speed up future installations. This caching mechanism is helpful, but sometimes the cache needs to be cleared to ensure you have the latest versions of packages or to free up disk space.
In this comprehensive guide, we‘ll cover everything you need to know about clearing, managing, and disabling the pip cache, including:
- What is the pip cache and how it works
- When you should clear the pip cache
- Step-by-step instructions for clearing the pip cache
- Using the pip cache command (pip 20.2+)
- Manually deleting the cache
- Disabling the cache with
--no-cache-dir - Alternative methods for managing the cache
- Additional pip commands for analyzing cache contents
Whether you‘re troubleshooting package installations or looking to reclaim disk space, understanding how to control the pip cache is an important skill for any Python developer. Let‘s take an in-depth look.
Overview: What is the Pip Cache?
The pip cache refers to the local storage space pip utilizes to store copies of downloaded package archives and pre-compiled distribution files.
Specifically, it contains two main components:
- The Wheelhouse: stores the wheel files (.whl files) for built distributions
- HTTP Cache: stores the raw .tar.gz/.zip source archives downloaded from PyPI or other package indexes
When you install a package via pip install, pip will first check if the required distribution file already exists in the cache directory before downloading it again from the internet. This makes repeated installations much faster.
However, there are some downsides to this caching behavior:
- Cache can grow very large, consuming extra disk space unnecessarily
- Corrupt packages can get stuck in the cache
- Outdated packages won‘t get updated if they already exist in the cache
Knowing when and how to clear the cache is thus an important aspect of effectively leveraging pip.
When Should You Clear the Pip Cache?
You primarily need to wipe the pip cache in two scenarios:
1. You are having issues installing or upgrading packages
If pip is failing to install or upgrade a package properly, it may be caused by a corrupted distribution file cached locally. Clearing the cache forces pip to freshly download the latest files.
2. You want to reclaim disk space
The pip cache tends to continuously grow as more and more wheels and source archives accumulate over time. On data-sensitive machines, clearing old packages out regularly is recommended.
Additionally, you may also consider clearing the cache when:
- Switching between Python versions and want packages recompiled
- Unable to reproduce installs across different environments
- Experimenting and want to start fresh
Now let‘s go over how to actually clear the cache through various methods.
Clearing the Pip Cache with the Cache Command
Recent versions of pip (20.2+) include a dedicated pip cache command for managing the package cache. This interface makes many cache operations much simpler, including clearing it.
Here is an overview of the key subcommands:
View Cache Details
pip cache info
This prints out useful statistics like location, size, and number of files:
Package index page cache location: /home/.cache/pip
Package index page cache size: 22KB
Number of HTTP files: 54
Wheels location: /home/.cache/pip/wheels
Wheels size: 983MB
Number of wheels: 827
List Cache Contents
pip cache list
This lists out the filenames of packages stored in the cache. Useful for inspecting contents.
Selectively Remove Packages
pip cache remove <pattern>
Delete individual packages from cache by specifying glob-style patterns. For example:
pip cache remove numpy*
Purge All Cached Packages
pip cache purge
Completely wipe the pip cache and remove everything. Simple yet effective.
As you can see, pip cache makes clearing the pip cache quite straightfoward. However, if you are relying on an older pip version, manual techniques are required instead.
Manually Clearing the Pip Cache
If running pip 19.x or earlier without the pip cache command, you‘ll need to handle cache deletion manually.
This involves directly locating and deleting the folder containing the pip cache files from your file system.
Identify Cache Location
First, determine where your pip cache directory resides using the pip.ini configuration file.
The default locations are:
Windows:
%APPDATA%\pip\Cache
macOS/Linux:
~/.cache/pip
But always check pip.ini as the path could be customized.
Delete Cache Folder
Once you‘ve confirmed the location, delete the entire directory to completely clear the cache:
# Linux example
rm -rf ~/.cache/pip
# Windows example
rmdir /s /q %APPDATA%\pip\Cache
And that‘s it! With the folder removed, pip will rebuild the cache from scratch.
Warning: Be careful to only remove the pip cache directory itself and not any other important folders.
Disabling Caching with --no-cache-dir
In addition to clearing the cache, you can also fully disable all caching behavior by using the --no-cache-dir option:
pip install pandas --no-cache-dir
This forces pip to ignore the cache entirely and freshly download the latest package version each time.
Disabling the cache can be helpful in situations where:
- You want to test installs across different systems
- Troubleshooting cache-related errors
- Validating upgrades work properly
However, installs will generally take much longer without any caching. Not recommended for everyday package management.
Now let‘s explore a couple alternative methods for managing the cache as well.
Alternative Pip Cache Techniques
If completely nuking the pip cache seems too aggressive for your needs, consider these less destructive options:
Prune the Cache
The pip-cache tool provides more advanced cache management functionality, including the ability to prune the cache:
pip-cache prune
This will selectively remove only outdated and unneeded packages from the cache instead of deleting everything.
Use Separate Cache Per Project
Rather than sharing a global cache, you can configure pip to utilize a local cache just for the packages in your virtual environment or project.
This helps avoid cache conflicts when working on multiple projects while still enjoying faster installs.
Periodically Refresh Cache
Set up a recurring task to wipe and rebuild the cache every so often, such as once a month. This approach balances keeping most of caching‘s benefits while reducing stale packages.
The right cache management strategy ultimately depends on your specific needs and workflow.
Additional Useful Pip Cache Commands
Here are some other helpful pip commands related to inspecting and analyzing the state of your package cache:
Get cache location:
pip debug cache list-dir-vars
Check cache stats:
pip debug cache stats
List packages in cache:
pip list --format=columns | grep -F "."
Use these commands to gain more visibility into what‘s currently stored in your cache.
Summary
Effectively leveraging pip‘s caching mechanisms while avoiding common downsides involves understanding when and how to clear out the built-up cache contents.
Key takeaways:
- Clear the pip cache when troubleshooting issues or freeing up disk space
- Use
pip cache(pip 20.2+) to easily view cache details and purge packages - Manually delete the cache folder if relying on older pip versions
- Disable caching selectively with
--no-cache-dir - Alternative methods exist like pruning and per-project caches
With these pip cache management best practices, you can boost package installation performance while avoiding headaches from outdated and corrupt packages sticking around.
Now you can install Python packages with more confidence and less frustration!


