composer - install new package without updating other packages

Viewed 10334

I added a package in the require block of my composer.json.

I do a composer install and it says Nothing to install or update.

In my understanding, composer update would work but I shouldn't do that because it updates the versions of the other packages to the latest, but I haven't tested my code on them.

How do I install that new package?

3 Answers

For future readers, if you have already added package(s) to your require block in composer.json, it's as simple as listing them all after the update command to exclude existing packages from being updated. First, simulate update to ensure you're happy with the result:

composer update --dry-run vendor/project vendor2/project2

If okay, run it again with the --dry-run argument removed.

You can see the output of updating new/specific packages is different from that of:

composer update --dry-run

Your composer.lock file should be on version control or otherwise backed up so you can restore it & revert all packages in the event of failure.

Using composer require will also update other dependencies. We can install a new package without updating anything else like this:

composer require package/name --no-update

this will add your package to composer.json, leaving composer.lock intact.

composer update package/name

this will now install/update your new package, adding it to composer.lock. This will not update any other dependencies.

Related