Should composer.lock be committed to version control?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The short answer: Yes, commit composer.lock for applications. No, don't commit it for libraries. The lock file records the exact versions of every dependency installed, ensuring all developers and deployment environments use identical packages. This distinction between applications and libraries is important because they have fundamentally different dependency resolution needs.
For Applications: Always Commit
When building a deployable application (website, API, CLI tool), commit composer.lock:
Why?
- Reproducible builds:
composer installreadscomposer.lockand installs the exact versions recorded. Without the lock file,composer installfalls back tocomposer updatebehavior, resolving the latest compatible versions — which may differ from what was tested. - Team consistency: Every developer gets identical dependencies, eliminating "works on my machine" issues.
- Deployment safety: Production gets the same packages that passed CI, not whatever versions happen to be latest at deploy time.
The Workflow
For Libraries: Don't Commit
When building a reusable library (a Composer package that others require), do not commit composer.lock:
Why?
- Libraries should be flexible: A library's
composer.jsondefines version constraints (^8.0). The consuming application'scomposer.lockdetermines the actual installed version. - Testing across versions: Without a lock file, CI tests the library against the latest compatible dependencies, catching compatibility issues early.
- Avoiding conflicts: If a library ships with a lock file, contributors may get confused about whether to use
installorupdate.
install vs update
| Command | Behavior | When to use |
composer install | Reads composer.lock, installs exact versions | Day-to-day development, CI, deployment |
composer update | Resolves latest compatible versions, updates composer.lock | Intentional dependency updates |
composer update --lock | Updates lock file hash without changing versions | After editing composer.json metadata |
CI/CD Pipeline
Use composer install (not update) in CI for applications to ensure deterministic builds.
Handling Merge Conflicts
composer.lock is a large JSON file that often conflicts during merges:
Comparison with Other Ecosystems
| Ecosystem | Lock file | Commit for apps? |
| PHP / Composer | composer.lock | Yes |
| Node.js / npm | package-lock.json | Yes |
| Node.js / Yarn | yarn.lock | Yes |
| Python / pip | requirements.txt (manual) | Yes |
| Python / Poetry | poetry.lock | Yes |
| Ruby / Bundler | Gemfile.lock | Yes |
| Rust / Cargo | Cargo.lock | Yes (binaries), No (libraries) |
The same principle applies across all ecosystems: commit lock files for deployable applications, omit them for reusable libraries.
Common Pitfalls
- Running
composer updatein production: Always usecomposer installin production.composer updatemay pull in untested versions. - Ignoring lock file changes: If
composer.lockchanges unexpectedly in a PR, review the diff — a dependency may have been updated intentionally or accidentally. - Outdated lock file: If
composer.jsonis edited manually (changing version constraints), runcomposer updateto sync the lock file. A stale lock file causescomposer installwarnings. - Platform requirements:
composer.lockrecords the PHP version and extensions available at lock time. Deploying on a different PHP version may cause issues. Useconfig.platform.phpincomposer.jsonto pin the target PHP version. --no-devin production: Usecomposer install --no-devfor production to skip development dependencies (PHPUnit, debugging tools).
Summary
- Applications: Always commit
composer.lockfor reproducible, deterministic builds - Libraries: Don't commit
composer.lock— let consuming applications resolve versions - Use
composer installfor installing from the lock file (daily development, CI, deployment) - Use
composer updateonly for intentional dependency version changes - The same application-vs-library rule applies in npm, Yarn, Poetry, Bundler, and Cargo
Related reading
- Should Gemfile.lock be included in .gitignore?
- Should I add the Visual Studio 2015 .vs folder to source control?
- Should I be adding the Django migration files in the .gitignore file?
- Should I check in folder node_modules to Git when creating a Node.js app on Heroku?
- Should I git ignore xcodeproject/project.pbxproj file?
- Should I use past or present tense in git commit messages?
- Should I use SVN or Git?
- Should the mvnw files be added to the repository?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.