After installing packages to your system, the pip package installer keeps a copy of the installed packages in the cache folder.
This is useful because the next time you want to install the same package, pip can just retrieve the cache instead of downloading the package again from the internet.
But pip’s cache folder easily grow in size, as you can see when running the pip cache info
command:
Package index page cache location: /Users/nsebhastian/Library/Caches/pip/http
Package index page cache size: 1743.8 MB
Number of HTTP files: 719
Locally built wheels location: /Users/nsebhastian/Library/Caches/pip/wheels
Locally built wheels size: 1265.2 MB
Number of locally built wheels: 20
As you can see in the example output above, the cache size has reached 3GB in size. The packages stored in the cache folder will never be removed by pip.
If you need to clear the cache folder, there are two options you can choose:
- Clear a specific package using
pip cache remove
- Clear all cached packages using
pip cache purge
Let’s see examples of how to use these options in practice.
1. Use the pip cache remove command
The pip cache remove
command is used to remove a specific package from your pip’s cache folder.
To see all packages available in the cache folder, you need to run the pip cache list
command:
pip cache list
# or
pip3 cache list
Output:
- libclang-15.0.6.1-py2.py3-none-any.whl (38 kB)
- openai-0.26.4-py3-none-any.whl (67 kB)
- openai-0.26.5-py3-none-any.whl (67 kB)
- pandas-1.1.5-cp311-cp311-macosx_13_0_arm64.whl (8.9 MB)
- pandas-1.2.0-cp311-cp311-macosx_13_0_arm64.whl (9.3 MB)
- pycocotools-2.0.6-cp310-cp310-macosx_13_0_arm64.whl (83 kB)
- pyodbc-4.0.35-cp311-cp311-macosx_10_9_universal2.whl (131 kB)
- pyspark-3.3.1-py2.py3-none-any.whl (281.8 MB)
- pyspark-3.3.2-py2.py3-none-any.whl (281.8 MB)
- pyspark-3.3.2-py2.py3-none-any.whl (281.8 MB)
- setuptools-67.2.0-py3-none-any.whl (1.1 MB)
- sklearn-0.0.post1-py3-none-any.whl (2.9 kB)
- wget-3.2-py3-none-any.whl (9.7 kB)
You can remove a specific package by running the command pip cache remove <package name>
as shown below:
pip cache remove sklearn
# or
pip3 cache remove sklearn
The cache for the sklearn
package will be removed from pip.
If you want to clean all files from the cache, then you can use the command below.
2. Use the pip cache purge
The pip cache purge
command is used to clear all files stored in pip’s cache folder.
Here’s an example of running the command:
$ pip3 cache purge
Files removed: 531
# Check for the cache info
$ pip3 cache info
Package index page cache location: /Users/nsebhastian/Library/Caches/pip/http
Package index page cache size: 0 bytes
Number of HTTP files: 0
Locally built wheels location: /Users/nsebhastian/Library/Caches/pip/wheels
Locally built wheels size: 49 kB
Number of locally built wheels: 0
# List available cached package
$ pip3 cache list
No locally built wheels cached.
As you can see, running the pip cache purge
command cleans the cache folder completely.
Cleaning the pip cache folder is safe, but please make sure that you’re not currently running an installation process or you might encounter some errors.
If you have limited disk space, pip also allows you to install packages without saving the build files in the cache folder, so you might want to try that.
I hope this tutorial is helpful. See you in other tutorials! 🙌