How does Composer handle dependency versioning, and what is the difference between composer.json and composer.lock?

Last updated 15 June 2026

Composer resolves dependencies from the version constraints in composer.json, picks the best compatible versions, and freezes that exact resolution in composer.lock for reproducible installs.

  • Versioning with semantic constraints:

    • ^1.2: allows updates that don't change the leftmost non-zero digit (1.2 up to but not including 2.0).

    • ~1.2: allows the last specified digit to increase (1.2 up to but not 2.0; ~1.2.3 up to 1.3).

    • Exact (1.2.3) or wildcards (1.2.*) are also supported.

  • composer.json vs composer.lock:

    • composer.json is the intent: which packages and what ranges are acceptable.

    • composer.lock is the result: the exact versions installed, so teammates and servers get the same builds.

  • Workflow: composer install honors the lock file; composer update re-evaluates constraints and refreshes the lock.

  • https://medium.com/the-artisans-journal/understanding-composer-composer-json-composer-lock-install-update-and-the-rest-of-the-basics-a5b67d627ed5
  • https://getcomposer.org/doc/articles/versions.md
  • https://madewithlove.com/blog/tilde-and-caret-constraints/
  • https://getcomposer.org/doc/01-basic-usage.md
  • https://phpcodez.com/difference-between-composer-json-and-composer-lock-files/
  • https://medium.com/@meetmehta554/what-is-composer-understanding-composer-json-and-composer-lock-fcb516764c09
  • https://www.trakked.io/en/blog/composer-json-understanding-version-constraints-and-dependencies
  • https://laraveldaily.com/post/difference-composer-json-composer-lock
All PHP interview questions