Pip – Change directory of pip cache on Linux?

Question:

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?

Asked By: user12314098

||

Answers:

TL;TR;: So long story short – do not change XDG_CACHE_HOME globally unless you really sure you want to do that. Changing XDG_CACHE_HOME globally with use of export like some people suggested, would not only affect pip but also other apps as well. You simply do not want to mess that much because it’s simply not necessary.


So what are your alternatives then? You could be using pip‘s --cache-dir <dir> command line argument instead or, at least, if you want to go that way, you could 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, but, but… there is not need to touch XDG_CACHE_HOME at all, as pip can have own configuration file, in which you can override all of the defaults to match your needs, including alternative location of cache directory. Moreover, all command line switches have accompanying environment variables that pip checks at runtime, which looks like the cleanest approach for your tweakings.

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

export PIP_CACHE_DIR=<path>

or per invocation:

PIP_CACHE_DIR=<path> pip ...

or, you create said pip’s configuration file and set it there.

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

Answered By: Marcin Orlowski

To simplify the other answer:

# find the config file location under variant "global"
pip config list -v

# create the file and add
[global]
cache-dir=/path/to/dir

# test if it worked
pip config list
pip cache dir
Answered By: Simon
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.