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 dirinto 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.txtGotchas
actions/setup-pythonhas a built-incache: pipinput 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
How to Cache a Poetry Virtualenv in GitHub ActionsCache the Poetry-managed virtualenv in GitHub Actions by setting virtualenvs-in-project and caching .venv key…
How to Cache the uv Cache in GitHub ActionsCache the uv package cache in GitHub Actions, keyed on uv.lock, so the uv Python package manager reuses its w…