Caching Python Installs in GitHub Actions

Matt Kornfield
4 min readJan 27, 2023

The journey of 1000 packages begins with a single pip install

Saving ~3 minutes with some caching!

The Beginning: No Caching

In the beginning, our basic GitHub Actions Python install steps were:

- uses: actions/setup-python@v4
with:
python-version: '3.9'
- run: make python-install

I was surprised by how long it took to download packages for our larger Python project. In that install step there were some pretty slow packages to install (ahem, pytorch).

I said to myself, we spend 3–4 minutes re-downloading the same packages all day. Why can’t we just cache the dang things?

The Middle: Using the Built-in Python Caching Action

Well of course it turns out you can! And GitHub offers a pretty neat way.

I looked around for what readily available GitHub Actions there were that used the Actions caching feature, and stumbled upon this option for the setup action. So yeah caching was as easy as:

- uses: actions/setup-python@v4
with:
python-version: '3.9'
cache: 'pip'
- run: make python-install

Now when our jobs ran, if they had the same requirements.txt file, python version, and OS, then they would reuse the cache entries.

--

--