How to Cache Dependencies in GitHub Actions (Every Ecosystem)
Caching dependencies is the highest-leverage CI speedup. Here are the correct keys and paths per ecosystem.
Use actions/cache (or the built-in cache in setup-* actions) with a key that changes only when your lockfile changes, and a restore-key fallback.
The pattern
.github/workflows/ci.yml
- uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: npm-Paths by ecosystem
| Ecosystem | Cache path | Key on |
|---|---|---|
| npm | ~/.npm | package-lock.json |
| yarn | ~/.cache/yarn | yarn.lock |
| pnpm | ~/.local/share/pnpm/store | pnpm-lock.yaml |
| pip | ~/.cache/pip | requirements*.txt |
| Maven | ~/.m2/repository | pom.xml |
| Gradle | ~/.gradle/caches | *.gradle* |
| Go | ~/go/pkg/mod | go.sum |
| Cargo | ~/.cargo + target | Cargo.lock |
| Bundler | vendor/bundle | Gemfile.lock |
Tip: setup-* actions cache for you
Many setup-node, setup-python, etc. actions have a cache: input that handles this automatically - prefer it when available.
Key takeaways
- Key on the lockfile hash; add a restore-key prefix.
- Cache the download/store dir, not the rebuilt output.
- Prefer the built-in cache in setup-* actions.
Related guides
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…
How to Speed Up GitHub Actions: 9 High-Impact TacticsMake GitHub Actions faster: caching, parallelization, test splitting, Docker layer caching, slimmer images, a…
GitHub Actions Workflow Template for Node.jsA ready-to-use GitHub Actions workflow for Node.js with caching and best practices - copy, commit, and run fa…