Skip to content
Latchkey

How to Cache pip and Poetry Dependencies in CI

Reinstalling Python packages on every run is wasted time. Cache the wheels and the virtualenv and installs drop to seconds.

setup-python can cache pip and Poetry downloads out of the box. For full speed, also cache the resolved virtualenv.

pip: built-in cache

setup-python caches the pip download cache keyed on your requirements file.

.github/workflows/ci.yml
- uses: actions/setup-python@v5
  with:
    python-version: '3.12'
    cache: pip
- run: pip install -r requirements.txt

Poetry: cache the virtualenv

Point Poetry at an in-project venv and cache it keyed on poetry.lock for the fastest installs.

.github/workflows/ci.yml
- uses: actions/setup-python@v5
  with:
    python-version: '3.12'
    cache: poetry
- run: poetry install --no-interaction

Key on the lockfile

Cache keys should hash requirements.txt or poetry.lock so a dependency bump invalidates cleanly and you never run on stale packages.

Key takeaways

  • setup-python cache=pip / cache=poetry covers most cases.
  • Cache the virtualenv for the fastest Poetry installs.
  • Key caches on the lockfile so bumps invalidate correctly.

Related guides

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