29

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?

6
  • have you tried --cache-dir link Commented Oct 3, 2020 at 4:02
  • @PaulBaiju yea I mentioned above that I have. Commented Oct 3, 2020 at 4:04
  • Not pip cache --cache-dir <new path> just --cache-dir <new path> Commented Oct 3, 2020 at 4:07
  • @PaulBaiju yea says "--cache-dir: command not found" Commented Oct 3, 2020 at 4:08
  • 2
    What @PaulBaiju is suggesting is pip <command> --cache-dir <path>. But this only affects the current command, not permanently AFAIK. For example pip install something --cache-dir ~/.mycache. Commented Oct 3, 2020 at 4:28

3 Answers 3

49

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Why do you add ellipsis (...) to the end of some commands? Are they placeholders for additional arguments, and if so, what?
No. It indicates there should be more in that command invocation (i. e. arguments of pip) but I skip that as it is irrelevant for what we want to achieve. In other words as no matter what these dots represent, it won't affect the main trick we pulling
There should be or there could be? If there should be more arguments, they should be relevant, right? I tried pip --cache-dir=<dir> (without the extra arguments), but it doesn’t seem to have any effect, since when I run pip cache dir, the cache dir has not been updated.
18

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

Comments

3

To simplify the Simon's answer even further:

pip config --user set global.cache-dir /your/desired/path

The --user option says that this setting should be stored in the user-scope pip configuration file and the global prefix instructs that the cache dir should be used for all pip commands. For more info on that check the pip configuration manual page.

Check if it worked with:

pip cache dir

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.