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.txtPoetry: 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-interactionKey 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
How to Cache Dependencies in GitHub Actions (Every Ecosystem)Cache dependencies in GitHub Actions with actions/cache - correct keys and paths for npm, yarn, pnpm, pip, Ma…
How to Reduce npm Install Time in CICut npm install time in GitHub Actions: use npm ci, cache the npm cache or node_modules, prune dev deps, and…
How to Warm Dependency Caches in CIPre-populate CI dependency caches from your default branch so pull-request runs always hit a warm cache inste…
CI Caching Explained: Speed Up Pipelines Without Breaking ThemHow CI caching works, what to cache (dependencies, build outputs, Docker layers), how cache keys and restore…