Skip to content
Latchkey

How to Cache pip Downloads in GitHub Actions

Cache the directory from pip cache dir keyed on requirements.txt so pip reuses downloaded and built wheels.

pip keeps a download and wheel cache so it does not refetch or rebuild packages. Cache that directory (resolved with pip cache dir) keyed on your requirements files, not the installed site-packages.

Steps

  • Resolve the cache path with pip cache dir into a step output.
  • Cache that path keyed on hashFiles('**/requirements*.txt').
  • Run pip install -r requirements.txt.

Workflow

.github/workflows/ci.yml
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-python@v5
    with:
      python-version: '3.12'
  - id: pip
    run: echo "dir=$(pip cache dir)" >> "$GITHUB_OUTPUT"
  - uses: actions/cache@v4
    with:
      path: ${{ steps.pip.outputs.dir }}
      key: pip-${{ runner.os }}-${{ hashFiles('**/requirements*.txt') }}
      restore-keys: |
        pip-${{ runner.os }}-
  - run: pip install -r requirements.txt

Gotchas

  • actions/setup-python has a built-in cache: pip input that caches this automatically when you point it at your requirements file.
  • Cache the download cache, not the virtualenv; a stale env can mask dependency changes.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →