Skip to content

PHP Tutorial Composer

composer.lock

Lesson 252 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

composer.json says what you want: “league/commonmark, any 2.x version”. composer.lock says what you got: “league/commonmark version 2.10.3, from this exact source, plus these other 7 packages at these exact versions”.

Why a lock file exists

Without it, two people running composer install on different days could get different versions of the same package, and a bug could appear on one machine only. The lock file makes every install identical.

terminal
BASH
composer install     # reads composer.lock, installs exactly what it lists

What is inside

A long JSON list with, for each package, its name, exact version, download source and a hash. You never edit it by hand. Composer writes it when you run composer require, composer update or composer remove.

A trimmed example:

data.json
JSON
{
    "_readme": ["This file locks the dependencies of your project to a known state"],
    "content-hash": "9d1c3b4f...",
    "packages": [
        {
            "name": "league/commonmark",
            "version": "2.10.3",
            "source": { "type": "git", "url": "https://github.com/thephpleague/commonmark.git", "reference": "..." }
        }
    ],
    "packages-dev": [],
    "minimum-stability": "stable",
    "prefer-stable": false,
    "platform": []
}

The content-hash lets Composer warn you when composer.json has changed but the lock file was not updated.

Commit it?

Project typeCommit composer.lock?
Application or websiteYes. Every server and teammate then runs the same versions
Library meant to be installed by othersUsually no. Users resolve their own versions

The normal workflow

  1. Someone runs composer require x or composer update. The lock file changes.
  2. They commit composer.json and composer.lock.
  3. Everyone else, and the production server, run composer install.

When the two files disagree

If composer.json changed and composer.lock is out of date, composer install warns you:

Text
PLAIN
Warning: The lock file is not up to date with the latest changes in composer.json.

Run composer update (or composer update --lock to refresh only the hash) and commit the new lock file.

Merge conflicts

If two branches both changed composer.lock, do not merge it by hand. Take one version, then run composer update for the packages in question, or composer update --lock, and commit the result.

Security

A lock file can pin a version that later gets a security fix. Update on purpose from time to time, and run composer audit to see which locked versions have known problems.

info
composer.lock is for repeatability
Think of composer.json as your shopping list, and composer.lock as the receipt. You can always rebuy the same items from the receipt.