Commit composer.lock for deterministic PHP installs
Commit Composer's lock file and run composer install in CI to keep dependency versions identical across environments.
04 Aug 2025, 11:36 UTC

The problem: dependency drift between laptops and production
A PHP application works on a developer's machine. On deployment, composer install pulls a newer patch release of a logging library. The new release changes a method signature, and a background job fails. The team spends an hour debugging before discovering that development and production resolved different versions of the same dependency.
This drift happens when composer.lock is missing from version control or when someone runs composer update on the server. A lock file is Composer's record of the exact dependency versions and content hashes resolved for a project. Committing it makes installs deterministic: every environment installs the same package set.
The thesis: commit the lock file, run install, avoid update in production
For applications, commit composer.lock to Git. In CI and production, run composer install, which reads the lock file and installs the recorded versions. Run composer update only on a local branch or in a controlled dependency-review process, because it regenerates the lock file from composer.json constraints and can introduce unplanned upgrades.
How install and update differ
composer install: readscomposer.lock, validates it againstcomposer.json, and installs the locked versions. It ignores version ranges incomposer.jsonexcept to confirm the lock is in sync.composer update: re-resolves dependencies fromcomposer.json, writes a newcomposer.lock, and installs the result.
Composer verifies installed package integrity against recorded hashes when they are present in the lock file. If the lock file is absent, Composer solves from composer.json, which means minor or patch versions can differ between runs.
Worked example: add a package and verify reproducibility
Run these commands in your project directory. You need write access to the project folder and, for Git commands, a configured repository.
- Check the Composer major version before relying on lock-file behavior:
composer --version
Expected: a version string. Older Composer versions may not understand newer lock metadata.
- Add a package. This updates
composer.jsonand regeneratescomposer.lock:
composer require monolog/monolog:^3.0
Risk: if your existing constraints are loose, other packages may also update. Review the changed files before committing.
- Commit both files together:
git add composer.json composer.lock
git commit -m "Add monolog/monolog and lock resolved versions"
- In CI or a clean checkout, remove vendor and install from the lock:
rm -rf vendor
composer install --no-interaction --prefer-dist
Expected: Composer downloads distributions matching the lock file and writes vendor/. It should not modify composer.lock. This command needs network access to the package repository.
- Verify the lock file did not change during install:
git diff -- composer.lock
Expected: no output. If the file changed, your lock was out of sync with composer.json.
You can also run composer validate to check consistency without modifying files.
Trade-off: libraries and platform mismatch
Library maintainers often exclude composer.lock because a committed lock constrains consumers who depend on the library. Applications should commit it; libraries usually should not.
Platform entries in the lock—such as the PHP version and installed extensions—reflect the machine that generated it. If your development machine runs a different PHP version than production, the lock may record platform requirements that do not match. Use config.platform in composer.json to declare the target platform so resolution stays consistent:
{
"config": {
"platform": {
"php": "8.2",
"ext-json": "*"
}
}
}
This does not install PHP; it tells Composer to resolve packages as if the target platform matches those values.
Closing checklist
- Commit
composer.lockfor applications. - Run
composer install, notcomposer update, in CI and deployment scripts. - Run
composer validatebefore merging dependency changes. - Test reproducibility by deleting
vendor/and confirminggit diff -- composer.lockis empty after install.
If you change composer.json, update the lock in the same pull request and review the diff. A lock file only prevents drift when it stays in sync with the manifest.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.