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 explain the differences, usage, and the significance of each in a project’s lifecycle.
The difference between composer.json and composer.lock in their purpose and usage:
🧾 composer.json
→ What you want
composer.json
→ What you want- Purpose: Declares your project’s dependencies.
- You edit this file.
- It contains:
- Packages your project requires
- Versions or version constraints (e.g.
"laravel/framework": "^9.0"
) - Autoloading info
- Scripts and other configuration
You write this in your file:
{ "require": { "laravel/framework": "^9.0" } }
🔸 This means:
“I want the laravel library, version 9.0 or higher, but not 10.0 or above.”
🔒 composer.lock
→ What you got
composer.lock
→ What you got- Purpose: Locks dependencies to specific versions.
- Composer generates this file when you run
composer install
orcomposer update
. - Ensures every team member (or server) installs the exact same versions of dependencies.
Think of it as your “receipt” showing exactly what was installed.
When you run
composer install
, Composer might install laravel version 9..0 (the latest matching your rule).Now,
composer.lock
will record:{ "name": "laravel/framework", "version": "9.0" }
🔸 This means:
“Laravel version 9.0.0 was actually installed.”
📄 composer.json
defines what you want (your dependency requirements).
👉 “I need Laravel 9.1 or higher”
📄 composer.lock
records what you got (the exact versions 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.
composer install vs composer update
– आसान भाषा में अंतर
🛠️ 1. composer install
– Jo version mil chuka hai, wahi chahiye
- Ye command
composer.lock
file ko padhta hai - Aur usme jo exact version likhe hain, wahi install karta hai
- Ye use tab karo jab:
- Aapne project clone kiya ho
- Aap chahte ho sab system pe same dependency version ho
📌 Use karo: Jab project clone karo ya production mein setup karo
🔧 2. composer update
– Mujhe naye version chahiye (allowed range ke andar)
- Ye command
composer.json
file ko padhta hai - Aur har dependency ka latest matching version download karta hai
composer.lock
file ko bhi update kar deta hai- Ye use tab karo jab:
- Aap packages ko latest version pe lana chahte ho
- Development ke time upgrade karna ho
📌 Use karo: Jab aap packages update karna chahte ho (aur team se sync karlo)
📌 एक लाइन में अंतर:
🔸
composer install
= Jo mila tha, wahi chahiye (composer.lock
)
🔸composer update
= Naya chahiye, lekin condition ke andar (composer.json
)
🎯 Composer Install vs Update – Clone karne ke baad kya karein?
Jab koi developer aapka PHP project GitHub se clone karta hai, to uske paas do important files hoti hain:
composer.json
– Aapne kaunse packages chahiye likha haicomposer.lock
– Aapko kaunse exact version mile woh likha hai
✅ Clone karne ke baad kya command chalani chahiye?
composer install
📌 Ye command kya karega?
- Ye
composer.lock
file ko padhega - Aur wahi exact version install karega jo aapke system pe chal raha tha
- Sab developers ke paas same version hoga, koi problem nahi
❌ Agar galti se composer update
chala diya?
- Ye
composer.json
padhega - Latest allowed versions install karega
- Aur
composer.lock
file ko update kar dega - Isse sabke system pe alag-alag version ho sakte hain — problem ho sakti hai
🔑 Simple Rule:
Project clone karo ✅
Fir sirfcomposer install
chalao ✅composer.lock
ko follow karo ✅
✅ Final Note:
Jab bhi project clone karo, hamesha
composer install
chalana chahiye taaki sabko same version mile aur code stable rahe.
Comments (0)
No comments yet. Be the first to comment!