Skip to main content
Tutorial Free

Difference Between composer.json and composer.lock

March 20, 2023
2 min read

In every Laravel project, you will have two files, composer.json and composer.lock. What is the difference between them?


In composer.json you specify what packages should be installed, and with what versions. For example:

{
"require": {
laravel/breeze": "^1.19",
},
}

This tells the composer to install the larave/breeze package, with version higher than 1.19.

For all the possible syntax options of ^ and * symbols for the versions, check Composer documentation here.

Now, which exact version is installed at any moment? Here comes the composer.lock file.

When you run the composer install command, it checks the composer.lock for the exact locked package version which has been already installed previously, during the previous composer install.

For example:

{
// ...
"name": "laravel/breeze",
"version": "v1.19.1",
"source": {
"type": "git",
"url": "https://github.com/laravel/breeze.git",
"reference": "4bbb1ea3476901c4f5fc706f8d80e4eac31c3afb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/breeze/zipball/4bbb1ea3476901c4f5fc706f8d80e4eac31c3afb",
"reference": "4bbb1ea3476901c4f5fc706f8d80e4eac31c3afb",
"shasum": ""
},
// ...
}

Here, you can see that Laravel Breeze v1.19.1 was installed the last time.

When you run the composer update command, the composer checks if there is a newer package, and if there is, downloads the new package version and updates the composer.lock content with the new version.

You can see the composer.lock difference for the laravel/breeze package after running composer update, which updated the package version from 1.19.1 to 1.19.2 below:

composer.lock diff

TIP: On production servers, always use composer install and never composer update, because sometimes updating packages without testing could break your application. Use composer update locally, then commit the updated composer.lock to the repository, and then run composer install on the production server.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

H
hariharan2684 ✓ Link copied!

Simple and nice

BM
Briere Mostafa Amine ✓ Link copied!

Thank you

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.