Setting up package caching for pip and npm

If you’re on a metered Internet connection (like me), downloading and re-downloading packages to setup your Python virtualenvs and Node.js projects isn’t very fun. Besides wasting data, it’s slow.

These package managers actually have caching built-in, though it needs to be configured. Follow the instructions below to configure Python’s pip, Node.js’ NPM, and Twitter’s Bower. I assume you’re on Linux and want to use XDG-compliant directories.

Python pip

Configuring a cache directory for pip will cache both source and wheel-based packages.

cat >> ~/.pip/pip.conf << EOF
[global]
download_cache = ~/.cache/pip
EOF
mkdir -p ~/.cache/pip

pip’s package cache caches things by URL, not by package, which makes pip’s caching mechanism not work with mirrors. The pip manual describes a mechanism for fast and local installs if this a problem for you.

Node.js NPM

NPM does cache packages by default, but in a non-XDG compliant directory. Reconfigure it as so:

npm config set cache ~/.cache/npm
mkdir -p ~/.cache/npm
mv ~/.npm/* ~/.cache/npm/
rm -rf ~/.npm/

See NPM’s documentation for package caching for further detail.

Twitter Bower

Bower, like NPM, also caches packages by default in a non-XDG compliant directory. Reconfigure it, and create a .bowerrc:

bower cache-clean
cat >> ~/.bowerrc << EOF
{
  "cache": "$HOME/.cache/bower/"
}
EOF
mkdir -p ~/.cache/bower

None of pip, npm, or bower do any purging of outdated or unused packages. You will have to manage this yourself, unless you are OK of these caches growing without bound (disk space is cheap these days).

Attempting to manage these caches is tricky: since modern UNIXes no longer update last accessed times (aka atime) on files, you can’t purge packages on last use. I leave figuring out the best way to purge these directories as an exercise to the reader (please leave a comment!).

Comments

Comments powered by Disqus