The purpose of pip's `--no-cache-dir` option

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# The purpose of pip's --no-cache-dir option

The --no-cache-dir option is used to disable the cache.

shell
pip install requests --no-cache-dir pip3 install requests --no-cache-dir

using no cache dir flag

Pip caches:

  • HTTP responses - pip first checks its local cache to determine if it has a suitable response stored locally that hasn't expired. If the condition is met, the local response is reused

  • locally built wheels - pip attempts to use wheels from its local wheel cache instead of rebuilding the project

Pip's caching is turned on by default with the intent to save time on duplicate downloads and builds.

The main purposes of pip's --no-cache-dir option are:

  • reducing the amount of space pip takes up on your machine.
  • reducing the size of your Docker images.
  • forcing pip to download a package from the remote repository because the locally cached files diverge.
  • if you run pip without a specific user, e.g. in a Docker container that doesn't have a user added, you're running pip as root. The installed packages and the cached files are owned by root and not accessible by other users.

Using the --no-cache-dir option disables pip's cache.

shell
pip install requests --no-cache-dir pip3 install requests --no-cache-dir

You can use the pip cache dir command to get the directory where pip's cache is stored on your machine.

shell
pip cache dir

pip cache dir

The location is likely going to be the following.

shell
# ๐Ÿ‘‡๏ธ On Linux ~/.cache/pip # ๐Ÿ‘‡๏ธ On macOS ~/Library/Caches/pip # ๐Ÿ‘‡๏ธ On Windows %LocalAppData%\pip\Cache

You can use the pip cache info command to get an overview of the contents of pip's cache.

shell
pip cache info

pip cache info

The pip cache info command shows the total size of the cache and the location where the HTTP responses and the locally built wheel files are stored.

# Removing all wheel files related to a package

If you need to remove all wheel files related to a single package from the cache, use the pip cache remove <package> command.

shell
pip cache remove requests

pip cache remove package

# Clear all wheel files from pip's cache

If you want to clear all wheel files from pip's cache, you would use the pip cache purge command.

shell
pip cache purge

# List all wheel files from pip's cache

If you need to list all wheel files from pip's cache, you would use the pip cache list and pip cache list <package> commands.

shell
# ๐Ÿ‘‡๏ธ List all wheel files from pip's cache pip cache list # ๐Ÿ‘‡๏ธ List all wheel files related to requests from pip's cache pip cache list requests

As previously mentioned, the way to disable pip's default behavior of caching HTTP responses and wheels is to use the --no-cache-dir option after the command.

shell
pip install requests --no-cache-dir pip3 install requests --no-cache-dir

You will mostly see the option used when using Docker to reduce the size of images.

If you need to force a reinstall of a specific package, you can use either of the following options.

shell
pip install requests --no-cache-dir pip install requests --ignore-installed pip install requests --force-reinstall

The --ignore-installed option ignores the installed packages and overwrites them.

The --force-reinstall option reinstalls all packages even if they are up-to-date.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev