composer install vs composer update: what each one changes, and which to run in production
composer install reproduces composer.lock; composer update rewrites it. Here is what each command does, when to use which, and a safe production deploy routine.
On this page
The short answer: composer install installs exactly what composer.lock says. composer update works out new versions from composer.json and rewrites composer.lock. In production and CI you almost always want install. You run update on purpose, on a branch, when you want newer dependencies.
What composer install does
- If
composer.lockexists, Composer reads it and installs those exact versions, down to the commit hash, intovendor/. - It checks the lock file still matches
composer.json. If you editedcomposer.jsonwithout updating the lock, you get a warning: “The lock file is not up to date with the latest changes in composer.json”. - If there is no lock file,
installbehaves likeupdateand creates one.
Because the lock file pins everything, two machines that run composer install on the same commit get identical vendor/ directories. That is the point of committing composer.lock for applications.
What composer update does
- It reads the constraints in
composer.json(for example"guzzlehttp/guzzle": "^7.8"). - It resolves the newest versions that satisfy every constraint together, including the dependencies of your dependencies.
- It writes the result to
composer.lockand installs it.
You can limit the blast radius:
# Update one package (and nothing else)
composer update monolog/monolog
# Update one package and the dependencies it needs to move
composer update monolog/monolog --with-dependencies
# See what would change without touching anything
composer update --dry-run
Not sure what a constraint such as ^7.8 or ~2.1 actually allows? Paste it into the Composer constraint tester.
Side by side
| composer install | composer update | |
|---|---|---|
| Reads | composer.lock |
composer.json |
Writes composer.lock |
Only if it is missing | Always |
| Result is reproducible | Yes | No: depends on what was released today |
| Use in CI and production | Yes | No |
| Use to get new versions | No | Yes |
A safe production routine
composer install --no-dev --prefer-dist --no-interaction --optimize-autoloader
--no-devskipsrequire-devpackages such as PHPUnit.--optimize-autoloader(or-o) builds a class map, which is faster in production.- Run
composer validate --strictandcomposer check-platform-reqsin CI to catch a stale lock file or a missing PHP extension before deploy.
Never run composer update on a production server. If a dependency resolves differently there, you have deployed code nobody tested.
Libraries are different
For a reusable library, the lock file only affects your own development. Projects that install your library ignore it and resolve versions from your composer.json. Many library authors still commit the lock file for reproducible CI, then add a second CI job that runs composer update --prefer-lowest to test the oldest versions their constraints allow.
Common questions
Should I commit composer.lock? For applications, yes, always. For libraries it is optional.
Why did install pick a version that is not the newest? Because the lock file says so. That is intended. Run composer outdated to see what newer versions exist.
What does composer update --lock do? It refreshes the lock file’s hash after a trivial composer.json change (such as the description) without upgrading packages.