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-distto 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-progressGotchas
- Cache the download cache, not
vendor/, so autoload maps regenerate correctly. - Without
composer.lock, key oncomposer.jsoninstead but expect looser hits.
Related guides
How to Cache Bundler Gems in GitHub ActionsCache installed Ruby gems in GitHub Actions by keying vendor/bundle on Gemfile.lock and configuring bundler t…
How to Build a Cache Key With hashFiles in GitHub ActionsUse the hashFiles expression to derive a cache key from one or more lockfiles in GitHub Actions, so the key c…