quickstart →

Caching dependencies in GitHub Actions: actions/cache and setup-* caching

Cache npm, pip, Go, Gradle and other dependencies in GitHub Actions with actions/cache or setup-* built-in caching, and make restores faster.

To cache dependencies in GitHub Actions, turn on the cache input of your language’s setup action (actions/setup-node, actions/setup-python, actions/setup-java; actions/setup-go caches by default), or add an actions/cache step that saves a directory such as ~/.npm under a key built from a hash of your lockfile. Cache the package manager’s download cache, not the installed node_modules: when the lockfile hasn’t changed, the next run restores it instead of downloading every package again. This page shows both approaches with examples for npm, pip, Go and Gradle, lists the actions/cache versions, and shows how to make restores faster.

Understanding caching in GitHub Actions#

Caching works by storing a copy of specific files or directories, like your project’s dependencies, between workflow runs. When your workflow runs again, it can reuse the cached files instead of regenerating or downloading them, which can often be time-consuming.

Why Caching?

  • Faster workflows: reusing previously downloaded or compiled files can drastically reduce build and setup times.
  • Reduced network latency: by avoiding repeated downloads, you also reduce potential network-related delays.
  • Improved reliability: minimizing external network requests decreases the chance of a failure due to network issues.

Implementing caching in your workflows#

GitHub Actions provides a built-in cache action that you can use to cache dependencies and other files. The key to effective caching is identifying what to cache and how to invalidate the cache when necessary (for example, when dependencies change).

Example: cache npm downloads with actions/cache#

Here’s how to cache Node.js dependencies using the cache action in a GitHub Actions workflow:

.github/workflows/ci.yml
name: Node.js CI
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- name: Cache npm downloads
uses: actions/cache@v6
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install Dependencies
run: npm ci
- name: Test
run: npm test

In this example:

  • path: we specify the directory to cache (~/.npm), which is npm’s download cache. We cache this rather than node_modules, so npm ci can install from it without hitting the network.
  • key: a unique key that represents the specific cache instance. We use a combination of the runner OS and a hash of the package-lock.json file. The hash changes if the dependencies change, creating a new cache entry for the next run.
  • restore-keys: used to restore from partially matching cache keys if there’s no exact match on the key. When package-lock.json changes, the ${{ runner.os }}-node- prefix still restores the most recent ~/.npm, so npm ci only downloads the packages that changed.

actions/cache saves in a post step at the end of the job, and only if the job succeeded and the exact key missed. A key that already exists is never overwritten. The cache-hit output is 'true' only on an exact match. To save even when tests fail, or to restore in one job and save in another, use actions/cache/restore and actions/cache/save (split restore and save).

The same pattern for pip and other package managers#

Caching is not limited to Node.js or npm. For most other package managers, the same pattern works once path points at the tool’s cache directory and key hashes its dependency files. Ruby with Bundler is different: actions/cache’s own examples ↗ say a cache step alone is not enough there, and recommend ruby/setup-ruby with bundler-cache: true (see setup actions below).

For example, here’s the same pattern for pip on a Linux runner:

.github/workflows/ci.yml
- name: Cache pip
uses: actions/cache@v6
with:
path: ~/.cache/pip # macOS: ~/Library/Caches/pip, Windows: ~\AppData\Local\pip\Cache
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-

actions/cache versions#

The examples on this page use actions/cache@v6. Each major tag points to the latest release in its line. As of November 2026:

Major tagLatest releaseRuntime
actions/cache@v6v6.1.0Node.js 24 (node24)
actions/cache@v5v5.1.0Node.js 24 (node24)
actions/cache@v4v4.3.0Node.js 20 (node20)

The restore-only and save-only sub-actions, actions/cache/restore and actions/cache/save, take the same tags and run on the same runtime. Node.js 20 reached end of life on April 30, 2026, so move v4 pins to v6. Cache keys and scopes notes what changed between v4, v5 and v6.

Built-in caching via setup actions#

Before you reach for actions/cache at all, check whether your setup-* action can do it for you. Most of them take a cache input that wires up the correct path and a key built from a hash of your lockfile or other dependency files.

.github/workflows/ci.yml
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm # also: yarn, pnpm
.github/workflows/ci.yml
- uses: actions/setup-python@v7
with:
python-version: "3.14"
cache: pip # also: pipenv, poetry

Since setup-node@v6, npm caching is on by default when package.json declares npm in its packageManager (or devEngines.packageManager) field. Set package-manager-cache: false to turn it off in privileged workflows that shouldn’t restore a cache.

GitHub’s dependency caching reference ↗ maps package managers to the setup actions that cache them:

Package managersActionTurn caching on with
npm, Yarn, pnpmactions/setup-node ↗cache: npm, yarn or pnpm
pip, pipenv, Poetryactions/setup-python ↗cache: pip, pipenv or poetry
Gradle, Mavenactions/setup-java ↗cache: gradle or maven (also sbt)
RubyGemsruby/setup-ruby ↗bundler-cache: true
Go modules and build cacheactions/setup-go ↗On by default (cache: false turns it off)
.NET NuGetactions/setup-dotnet ↗cache: true, with a committed packages.lock.json

For Gradle, GitHub’s own Gradle guide uses gradle/actions/setup-gradle ↗ instead. It caches by default and covers more of Gradle’s state, such as build outputs and the configuration cache. Leave cache: gradle off actions/setup-java when you use it, because two caches of the Gradle user home can interfere:

.github/workflows/ci.yml
- uses: actions/setup-java@v6
with:
distribution: temurin
java-version: 21
- uses: gradle/actions/setup-gradle@v6
- run: ./gradlew build

By default, setup-gradle saves the cache only from jobs on the default branch; jobs on other branches restore it read-only. Its default cache provider is proprietary: as of November 2026 it is free for public repositories and in free preview for private ones. cache-provider: basic switches to an open-source provider built on actions/cache.

By default, actions/setup-node and actions/setup-go hash a dependency file in the repository root: the lockfile for setup-node, and go.mod for setup-go since v6. When the file sits in a subdirectory, as in a monorepo, or you want to key on another file, set cache-dependency-path, for example cache-dependency-path: web/package-lock.json or cache-dependency-path: go.sum. setup-go caches by default, so a hand-written actions/cache step for Go’s module and build caches only duplicates it. A Go job needs no cache step at all:

.github/workflows/ci.yml
- uses: actions/setup-go@v7
with:
go-version-file: go.mod # caches modules and build outputs by default
- run: go test ./...

The setup actions are the simplest correct option for plain dependency caching, and you should prefer them. Reach for a hand-written actions/cache step when you need something the setup action does not cover: a compiler cache, a non-standard directory, or control over when the cache is saved, with the split restore and save actions.

How to reduce cache restore time#

A cache restore is a download plus a decompression, so its time grows with the size of the archive and drops with the bandwidth to the cache. On GitHub-hosted runners, a few hundred megabytes restore in seconds, while multi-gigabyte caches (Docker layers, Rust target/, Gradle) can take minutes. In rough order of payoff:

  1. Cache less. Cache the package manager’s download cache (~/.npm, ~/.cache/pip, or Cargo’s ~/.cargo/registry/index, ~/.cargo/registry/cache and ~/.cargo/git/db), not the installed tree (node_modules, a virtualenv) or build outputs that are cheap to recreate. Check the size in the restore step’s log.
  2. Split big caches. Put the dependency cache and the compiler cache in separate steps with their own keys, so they invalidate separately: a lockfile bump then misses only on the dependency cache, and the compiler cache isn’t saved again under a new key. Every run still downloads both. Steps in a job run one after the other, so to restore the two at the same time, put them in a parallel: group. Their cache-hit outputs are available once the group finishes.
  3. Restore only what a job needs. Lint jobs rarely need the build cache. Use actions/cache/restore in the jobs that read, and save from one job only.
  4. Skip the restore on an exact hit you don’t need. lookup-only: true checks whether the key exists without downloading it, which lets a setup job decide whether a rebuild is needed.
  5. Move the cache closer. The restore speed is capped by the network between the runner and the cache store. A cache in the same cloud region as the runner can download at the instance’s full network speed, and a disk restored from a snapshot skips the archive download and extraction. RunsOn offers both: see On RunsOn.

Best practices for caching#

  • Be specific with cache keys: use precise keys to avoid restoring the wrong cache. Including the platform, language version, and a hash of dependency files in the cache key is a good practice.
  • Cache inputs; pass outputs as artifacts: cache deterministic inputs, such as downloaded dependencies, and compiler or build caches keyed on their inputs. Pass build outputs to later jobs as artifacts.
  • Understand cache limits and eviction: each repository gets 10 GB of cache for free; over the limit, GitHub evicts the least recently used entries, and by default it deletes any entry unused for 7 days (limits and eviction). As of November 2026, a repository can also upload at most 200 cache entries a minute and download 1,500. Past that, the cache step logs a warning and the job carries on without the cache, so a wide matrix that saves one entry per leg can lose saves.
  • Save from trusted workflows: since June 2026, runs of low-trust triggers such as pull_request_target get read-only access to the default branch’s cache by default, so fill it from a push workflow on main (who can write the cache).
  • Keep secrets out of the cache: anyone who can open a pull request can read the base branch’s caches, so never cache tokens, credentials, or config files that hold them.
  • Know how cache scope works: a run can read the caches of its own branch, of the default branch and, for a pull request, of its base branch, so a new branch misses until the default branch’s cache is warm (cache scope).

Conclusion#

Caching is a powerful tool in your GitHub Actions workflows, enabling faster builds, reducing network dependency, and making your CI/CD process more efficient. This article focused on the basics of dependency caching. From here:

On RunsOn#

RunsOn’s Magic Cache serves actions/cache and the setup-* caches from an S3 bucket in your AWS account, in the runner’s region, with no 10 GB cap; entries expire after the stack’s retention window, 10 days by default. It is opt-in: on Flex, add extras=s3-cache to the job label and run runs-on/action@v2 before any cache step; on Fleet, the platform team enables the s3-cache extra in the runner definition. Your cache steps stay as they are.

.github/workflows/ci.yml
jobs:
build:
runs-on: runs-on=${{ github.run_id }}/runner=2cpu-linux-x64/extras=s3-cache
steps:
- uses: runs-on/action@v2
- uses: actions/checkout@v7

For the biggest caches, sticky disks restore an EBS volume from a snapshot before each job, so there is no archive to download or extract. Our cache benchmark compares restore times across providers. Magic Cache replaces runs-on/cache, RunsOn’s older drop-in action, now legacy.