Skip to content
Latchkey

How to Cache Composer Dependencies in GitHub Actions

Cache the Composer cache directory keyed on composer.lock so packages are fetched once and reused.

Composer caches downloads in a directory reported by composer config cache-files-dir. Cache that path keyed on composer.lock, then run composer install to reuse it.

Steps

  • Read the cache directory with composer config cache-files-dir.
  • Cache that path keyed on composer.lock.
  • Run composer install --prefer-dist to pull from the cache.

Workflow

.github/workflows/ci.yml
steps:
  - id: composer-cache
    run: echo "dir=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT"
  - uses: actions/cache@v4
    with:
      path: ${{ steps.composer-cache.outputs.dir }}
      key: composer-${{ runner.os }}-${{ hashFiles('composer.lock') }}
      restore-keys: composer-${{ runner.os }}-
  - run: composer install --prefer-dist --no-progress

Gotchas

  • Cache the download cache, not vendor/, so autoload maps regenerate correctly.
  • Without composer.lock, key on composer.json instead but expect looser hits.

Related guides

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