Python

Pip Clear Cache

Pip is the most popular and versatile package installer for Python. It offers a command-line utility that allows you to manage Python packages easily.

Using pip, you can perform actions such as downloading, installing, updating, and uninstalling packages with simple commands.

Pip uses a caching mechanism that allows you to download and install Python packages faster. It works by storing a cache of the downloaded packages on the local wheel.

The caching mechanism allows pip to improve the download and installation of the packages. This is because pip does not need to download already existing packages.

In some cases, pip may not need to redownload a package when updating if it already exists in the cache.

This tutorial will discuss how to work with the pip cache, how to manage the pip cache using various commands, and how to clear it when you need to start afresh.

Pip cache command

In the recent version of pip (i.e., Pip 20.2 and above), you can use the pip cache command to manage the pip cache.

The command syntax:

$ pip cache <action>

Let us discuss each action below:

Pip cache dir

This command allows you to get the directory where the pip cache is stored on the target system.

The command:

$ pip cache dir

The command should return the directory where the pip cache is located. Example output on Windows and Linux is as shown below:

pip cache dir
c:\users\username\appdata\local\pip\cache

On Linux:

$ pip cache dir
/home/debian/.cache/pip

Pip show cache info

You can use the pip cache info command as shown to get information about the pip cache:

$ pip cache info
Package index page cache location: /home/debian/.cache/pip/http
Package index page cache size: 0 bytes
Number of HTTP files: 0
Wheels location: /home/debian/.cache/pip/wheels
Wheels size: 0 bytes
Number of wheels: 0

The command should return a sample output as:

Pip show filenames and packages in the cache

Pip provides us with the command to view the filenames and directories stored in the cache.

$ pip cache list <pattern>

Pip remove package from cache

To remove a specific package from the cache, you can use the remove action as:

$ pip cache remove <pattern>

The command allows you to specify a specific pattern to match a particular package.

NOTE that the specified pattern can be a glob expression or the name of a target package.

For example, to remove all the files from the cache, you can run:

$ pip cache remove *                                                                                                                                                                                                                                                              
Files removed: 163

NOTE that the command will remove all the files from the cache. Be careful if you do not wish to clear your cache.

Pip clean cache

You may need to reset the pip cache to default in some instances. For that, you can use the purge action as:

$ pip cache purge

Pip Install package without cache

If you want to install a package without looking up the file in the pip cache, we can use the –no-cache-dir.

The command syntax is:

$ pip install package_name --no-cache-dir

For example, to install TensorFlow without looking up the pip cache, run the command:

$ pip install tensorflow --no-cache-dir

The command above should force pip to redownload the package even if it is stored in the cache.

Manually purge pip cache

If you use an older version of pip and have no access to the pip cache command, you can clear the pip cache by manually removing the cache directory.

On Linux:

$ sudo rm ~/.cache/pip
$ sudo rm -rf /root/.cache/pip

On Windows:

rd /s /q "%appdata%\local\pip\cache"

Closing

This tutorial taught you the fundamentals of working with the pip cache. We also covered two methods to clear the pip cache depending on your installed pip version.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list