Skip to content

PHP composer build cache on PaaS: getting deploy time from 90s to 12s

A composer install on every deploy re-downloads packages the previous build already pulled. Caching the right directories on the right boundary cuts PHP deploy time by 6-8x.

The deploy log shows 87 seconds spent on composer install --no-dev --optimize-autoloader, every deploy, on every node. The application has 73 packages in composer.lock, none of which have changed in three weeks. The packages are being downloaded and unpacked from scratch each time because the build container is ephemeral and nobody told the platform to keep the composer cache between runs. Trim that and the same deploy lands in 11-13 seconds.

What’s happening

composer install does three things: resolves the lock file (cheap), downloads each package to a local cache (slow if cold, free if warm), and copies the package files into vendor/ (medium). On a fresh build container, the local cache is empty, so step two pulls every package from packagist.org over the network. On the next build, if the cache directory survived, packagist gets a 304 Not Modified for the manifest and the package files come straight off disk. The win on a typical PHP app is 70-80% off the install time.

The cache directory composer uses is ~/.composer/cache (or $COMPOSER_HOME/cache), with subdirectories files/ (zip archives of every package version pulled) and repo/ (packagist manifests). The vendor/ directory itself is build output — don’t cache that across deploys unless your platform handles it as a build artifact. Caching vendor/ directly causes stale dependency bugs when composer.lock changes but the cache doesn’t invalidate.

PaaS platforms expose cache directories on different boundaries. Some give you a per-application persistent disk that survives builds. Some give you a build cache attached to the build pipeline, separate from runtime storage. Some require you to declare cache paths in a paas.yml or equivalent and the platform mounts them at build time. The mechanism varies; the principle does not: persist the composer cache directory between builds, key it on the OS + PHP minor version + composer major version (so a PHP 8.2 → 8.3 upgrade doesn’t reuse incompatible archives), and let it grow until composer’s own GC trims it.

Three other gains stack on top of the cache once it’s working:

  • composer install --no-dev --optimize-autoloader --classmap-authoritative skips dev dependencies entirely and generates a classmap that bypasses the PSR-4 lookup at runtime. The build saves time and the application saves microseconds per request.
  • --prefer-dist (the default since composer 2) downloads zip archives instead of git-cloning each package. With the cache warm, this is what makes 304 responses possible.
  • Setting process-timeout in composer.json to a sensible value (300s) prevents a stuck network read from holding the build container for 30 minutes.

How to verify

What composer thinks its cache directory is:

composer config --global cache-dir
# /root/.composer/cache
composer config --global cache-files-dir
# /root/.composer/cache/files

How big the cache currently is and how full it is per package — if this is empty after a build, the cache is not persisting:

du -sh ~/.composer/cache 2>/dev/null
# 412M    /root/.composer/cache

find ~/.composer/cache/files -name '*.zip' | wc -l
# 891

How much of an install is actually downloading versus reading from cache — composer logs each step with timings under -v:

composer install --no-dev -v 2>&1 | \
  awk '/Downloading|Installing|Reading|Loading/ {print}' | head -30

Lines saying “Reading from cache” or “Loading from cache” are the warm path. Lines saying “Downloading” mean network — count them to see how much of your build is cache miss.

End-to-end, before and after caching:

time composer install --no-dev --optimize-autoloader --no-interaction
# real    0m87.245s   <- cold cache, fresh build container
# real    0m11.802s   <- warm cache, same lockfile

The fix

Two things to wire up: persist the composer cache across builds, and tighten the install command itself.

The cache path varies by platform — find the one that survives between builds (often called “build cache”, “deploy cache”, or “persistent volume mounted at build time”). On platforms that expose a paas.yml-style descriptor, declare it explicitly. A representative deploy hook configuration:

# paas.yml or equivalent platform descriptor
name: my-php-app

build:
  # Tell the platform to persist this between builds.
  # Keyed by the application + the build environment fingerprint.
  cache:
    - /root/.composer/cache
    - vendor

  hooks:
    pre-build: |
      # Make sure the composer cache directory exists even on first run
      mkdir -p /root/.composer/cache
      composer config --global cache-dir /root/.composer/cache

    build: |
      # Install runtime deps only, with autoloader optimizations
      composer install \
        --no-dev \
        --no-interaction \
        --no-progress \
        --prefer-dist \
        --optimize-autoloader \
        --classmap-authoritative

    post-build: |
      # Optional: trim the cache if it grows past a threshold
      composer global require composer/cache-clear --no-interaction || true
      composer clear-cache --gc

For platforms without a YAML descriptor, the equivalent is usually a composer.json scripts section that runs in the platform’s build phase, plus an environment variable telling composer where to put the cache:

{
  "config": {
    "process-timeout": 300,
    "sort-packages": true,
    "optimize-autoloader": true,
    "classmap-authoritative": true,
    "preferred-install": {
      "*": "dist"
    }
  },
  "scripts": {
    "build:install": [
      "@composer install --no-dev --no-interaction --no-progress --prefer-dist --optimize-autoloader --classmap-authoritative"
    ],
    "post-install-cmd": [
      "@php artisan optimize:clear",
      "@php artisan event:cache",
      "@php artisan route:cache",
      "@php artisan view:cache"
    ]
  }
}

The Laravel artisan block at the end is framework-specific cache priming — different frameworks expose different commands but the principle is the same: do the slow work once at build time, not on each request.

Two further wins to layer on once the cache is working.

Pin composer’s own version so a surprise minor release doesn’t invalidate the cache. Put composer/composer in a separate setup step that uses composer self-update --pinned-version or install composer via the OS package manager at a fixed version, and bake it into the build container image — don’t curl https://getcomposer.org/installer | php on every build.

For deploys triggered from CI rather than the platform, mount the same cache directory in CI and composer install there, then sync vendor/ as a build artifact to the platform. This shaves the platform-side install to zero and is the right pattern if you already have a build pipeline that runs lint, tests, and security scans. The trade-off: CI environment must match the PaaS environment (same PHP version, same extensions) or you’ll deploy a vendor/ built for the wrong target.

Finally, if composer install is still slow with a warm cache, the bottleneck is no longer the network — it’s the autoloader dump on a large codebase, or post-install-cmd scripts that run framework caches. Profile with composer install --profile and the heaviest steps will surface.


Optimizing the PHP build pipeline on PaaS — cache wiring, parallelism, framework cache priming — is part of how we run Cloud PaaS and deployment management. Tell us what you’re deploying and we’ll scope it.