pythonlinuxpip

Pip - Change directory of pip cache on Linux?


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;: do not change XDG_CACHE_HOME globally unless you are sure you really want to do that.


    Changing XDG_CACHE_HOME globally, like some people suggested would not only affect pip but also other apps as well. You simply do not want to mess that deep, because it's simply not necessary in most of the cases. So what are your alternatives then? You could be using pip's --cache-dir <dir> command line option instead:

    pip --cache-dir=<dir> ...
    

    Or you could override value of XDG_CACHE_HOME variable for pip invocation only:

    XDG_CACHE_HOME=<path> pip ...
    

    which also can be made more permanent by i.e. 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 tweaks.

    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.