In Composer based applications, composer.json and composer.lock have differing functions. composer.json describes the required packages and the specific versions or ranges of those versions needed whereas composer.lock ensures that the exact versions of the packages installed on each environment are identical. This blog explains their differences, usage, and the significance of each in a project’s lifecycle.

The difference between composer.json and composer.lock lies in their purpose and usage:

composer.json

  • This file outlines all the dependencies that the project needs.
  • Developers are free to modify this file and include the package title and version details (e.g., ^2.0, >=1.5).

composer.lock

Composer creates this file the first time a project is installed. This is done to make sure that every team member uses the same versions of dependencies for the project.

This file contains the version numbers of all downloaded dependencies.

Key Difference:

composer.lock explains what dependencies were pulled to maintain stability.
composer.json explains what dependencies are required for the project.

Example

Let’s assume the following is your composer.json file content: 

{
  "require": {
    "laravel/framework": "^9.0"
  }
}

This indicates that your project needs the Laravel framework and everything that offers version 9.0 as well as advanced versions that are compatible (like 9.1, 9.2 etc).

When you run

composer install

The Composer will start with the maximum accepted version – in this example, 9.2.1.

{
  "packages": [
    {
      "name": "laravel/framework",
      "version": "9.2.1"
    }
  ]
}

This ensures that every developer who runs composer install later gets exactly Laravel 9.2.1, avoiding unexpected updates.

📄 composer.json (Defines Requirements)
👉 “I need Laravel 9.0 or higher”

📄 composer.lock (Locks Exact Version Installed)
👉 “I have installed Laravel 9.2.1”

📌 Key Concept:

composer.json = Specifies the range of acceptable versions.
composer.lock = Records the exact version installed.