Sort packages without adding/updating dependencies

To quote from the Composer manual “Config” section:

sort-packages

Defaults to false. If true, the require command keeps packages sorted by name in composer.json when adding a new package.

So if you run

composer config sort-packages true

you command Composer to add the following to your composer.json file:

{
    "config": {
        "sort-packages": true
    }
}

The next time you do a composer require <vendor-name>/<project-name>[:<version-constraint>[@<stability-flag>]] it will automatically sort the whole list. You can “re-require” a configured package – one that is already required by the root package, cf. composer-show(1) w/ –installed – to only sort the existing list of dependencies without additional changes, see as well the answer by Darryl Hein about this and more information.


Change the Default sort-packages Value (false)

The setting can also be configured globally:

composer config --global sort-packages true

In which case the $COMPOSER_HOME/config.json will be altered, instead of the project-level composer.json file, the root package or configuration.

Note: The root configuration supersedes the global configuration for this sort-packages setting as long as it is not unset:

composer config --unset sort-packages

For the change log see the answer by Darryl Hein.

Leave a Comment