1. The caching mechanism of pip and conda
1. pip cache mechanism
pip
It is the official Python package management tool. When installing Python packages, it will be downloaded..whl
The 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,pip
The default cache path is as follows:
-
Windows:
%LOCALAPPDATA%\pip\Cache
-
Linux/macOS:
~/.cache/pip
2. Conda's caching mechanism
conda
is a powerful package management tool, mainly used to manage Python and its related dependencies. Its cache mechanism is more thanpip
More complicated, except for download.tar.bz2
or.conda
Package, it will also cache the unzipped package to speed up subsequent environment creation and update operations.
Default cache path
-
Windows:
C:\Users\<username>\Anaconda3\pkgs
-
Linux/macOS:
~/anaconda3/pkgs
or~/miniconda3/pkgs
2. Modify the cache path of pip
In some cases, you may want to changepip
The 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 reduce
pip
The overhead of dependency on download.
Method 1: Use pip config to modify the cache path (recommended)
You can usepip config
Command to modify the global cache path. For example,pip
The 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 itpip
configuration 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
andpip
Similar,conda
It 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,conda
Will download.tar.bz2
or.conda
Files are stored inpkgs_dirs
In 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_dirs
Whether 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
conda
The 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 --all
Clean 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?
andpip
different,conda
usepkgs_dirs
To 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 config
Or manually edit.condarc
。
2. How to clean up the cache of pip and conda?
ifpip
orconda
The caches take up too much disk space, you can clean them with the following command:
Clean uppip
cache:
pip cache purge
Clean upconda
cache:
conda clean --all
This will clean upconda
Downloaded package cache, index cache and unused environment files.
3. Conda: Is there any permission problem when sharing cache paths?
If multiple users share the sameconda
Cache directories (for example in server or multi-user environments), you may encounter permission issues. Recommended to usechmod
andchown
Set 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 useicacls
Command 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!