PHP › Composer

What is the difference between the composer.json and composer.lock files, and why is committing the lock file to version control important?

Intermediate

Answer

The composer.json file specifies the version constraints for your dependencies, such as accepting any compatible version within a major release, while the composer.lock file records the exact specific versions that were actually installed at a given point in time, and committing this lock file to version control ensures that every developer on a team, and your production server, install the exact same dependency versions, avoiding subtle bugs caused by different team members accidentally using slightly different library versions.
composer install
// installs the exact versions recorded in composer.lock rather than the loosest matching versions
Real-world example A team experiences a bug that only appears on one developer's machine, eventually tracing it back to that developer never having committed an updated composer.lock file, meaning their local environment was silently running a different library version than everyone else on the team.

Common follow-ups: What happens if you run composer update instead of composer install?;Should the composer.lock file always be committed to version control?