Friday, October 28, 2022

[SOLVED] Pip - Change directory of pip cache on Linux?

Issue

I heard changing XDG_CACHE_DIR or XDG_DATA_HOME fixes that but I did

export XDG_CACHE_DIR=<new path>
export XDG_DATA_HOME=<new path>

I've also tried

pip cache dir --cache-dir <new path>

and

pip cache --cache-dir <new path>

and

--cache-dir <new path>

and

python --cache-dir <new path>

from https://pip.pypa.io/en/stable/reference/pip/#cmdoption-cache-dir
and when I type

pip cache dir

It's still in the old location. How do I change the directory of pip cache?


Solution

TL;TR;: Changing XDG_CACHE_HOME globally like with use of export will affect other apps too, not just pip alone and you do not want the mess this can cause. Do NOT do this unless really necessary!


You should be using pip's --cache-dir <dir> command line argument instead or at least, if you want to go that way, override XDG_CACHE_HOME value for pip invocation only:

XDG_CACHE_HOME=<path> pip ...

which also can be made more permanent by using shell alias feature:

alias pip="XDG_CACHE_HOME=<path> pip"

BUT... you do not need to touch XDG_CACHE_HOME at all, as pip can have own configuration file where you can override all the defaults to your liking, including alternate cache directory location. Moreover, all command line switches have accompanying env variable that pip looks for at runtime, which looks like the cleanest approach.

In your case, --cache-dir can be provided via PIP_CACHE_DIR. So you can either set it globally:

export PIP_CACHE_DIR=<path>

or per invocation:

PIP_CACHE_DIR=<path> pip ...

or create said configuration file.

See docs for more information about pip config file and variables.



Answered By - Marcin Orlowski
Answer Checked By - David Goodson (WPSolving Volunteer)