SoFunction
Updated on 2025-04-13

Several ways to modify pip and conda cache paths in Linux

1. The caching mechanism of pip and conda

1. pip cache mechanism

pipIt is the official Python package management tool. When installing Python packages, it will be downloaded..whlThe files and source code are stored in the local cache directory so that they can be reused directly during subsequent installations without re-downloading.

Default cache path

Under different operating systems,pipThe default cache path is as follows:

  • Windows%LOCALAPPDATA%\pip\Cache
  • Linux/macOS~/.cache/pip

2. Conda's caching mechanism

condais a powerful package management tool, mainly used to manage Python and its related dependencies. Its cache mechanism is more thanpipMore complicated, except for download.tar.bz2or.condaPackage, it will also cache the unzipped package to speed up subsequent environment creation and update operations.

Default cache path

  • WindowsC:\Users\<username>\Anaconda3\pkgs
  • Linux/macOS~/anaconda3/pkgsor~/miniconda3/pkgs

2. Modify the cache path of pip

In some cases, you may want to changepipThe cache path, for example:

  • Insufficient storage space: The default cache path takes up a lot of storage, and you can move it to a USB drive or an external hard drive.
  • Shared cache: Share caches between multiple environments to avoid repeated downloads of the same package.
  • Docker container optimization: Adjust the cache path to reducepipThe overhead of dependency on download.

Method 1: Use pip config to modify the cache path (recommended)

You can usepip configCommand to modify the global cache path. For example,pipThe cache path is modified to/home/your_path

pip config set -dir "/home/your_path"

If you want to restore the default settings, you can use the following command:

pip config unset -dir

You can run the following command to verify that the configuration is successful:

pip config list

You should be able to see outputs like this:

-dir = /home/your_path

Method 2: Modify using environment variables (temporary)

If you only want to modify the cache path in the current terminal session, you can use environment variablesPIP_CACHE_DIR

export PIP_CACHE_DIR="/home/your_path"

On Windows, you can use:

set PIP_CACHE_DIR=C:\my_custom_cache\pip

Notice: This method is temporary, and the default cache path will be restored after the terminal is closed.

Method 3: Modify the configuration file (permanent)

You can also manually modify itpipconfiguration file to permanently change the cache path:

echo "[global]" >> ~/.pip/
echo "cache-dir = /home/your_path" >> ~/.pip/

Windows users can modifyC:\Users\<username>\pip\File, add the following content:

[global]
cache-dir = C:\my_custom_cache\pip

3. Modify the cache path of conda

andpipSimilar,condaIt also allows users to modify cache paths to optimize storage usage or improve flexibility in environmental management.

Method 1: Use conda config to modify the cache path (recommended)

By default,condaWill download.tar.bz2or.condaFiles are stored inpkgs_dirsIn the directory. You can add a new cache directory using the following command:

conda config --add pkgs_dirs /home/your_path

You can run the following command to checkpkgs_dirsWhether the modification was successful:

conda config --show pkgs_dirs

If you want to remove a cache path, you can use:

conda config --remove pkgs_dirs /home/your_path

Method 2: Manually modify the .condarc configuration file

condaThe configuration file is located in~/.condarc(Windows inC:\Users\<username>\.condarc). You can edit this file manually and add the following content:

pkgs_dirs:
  - /home/your_path

After modification, it can be runconda clean --allClean up the old cache and make sure the new settings take effect.

4. Frequently Asked Questions and Solutions

1. Why can’t conda’s pkgs_dirs be modified directly using the export method?

andpipdifferent,condausepkgs_dirsTo manage cache paths, environment variables will not be automatically read. Therefore, you cannot pass directlyexport CONDA_PKGS_DIRS=...To modify the cache path, you must useconda configOr manually edit.condarc

2. How to clean up the cache of pip and conda?

ifpiporcondaThe caches take up too much disk space, you can clean them with the following command:

Clean uppipcache

pip cache purge

Clean upcondacache

conda clean --all

This will clean upcondaDownloaded package cache, index cache and unused environment files.

3. Conda: Is there any permission problem when sharing cache paths?

If multiple users share the samecondaCache directories (for example in server or multi-user environments), you may encounter permission issues. Recommended to usechmodandchownSet appropriate permissions, for example:

sudo chown -R $USER:$USER /home/pi/udisk/.cache/conda
chmod -R 755 /home/pi/udisk/.cache/conda

On Windows, you can useicaclsCommand to manage permissions.

This is the end of this article about several methods to modify the pip and conda cache paths in Linux. For more related contents of modifying the pip and conda cache paths in Linux, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!