Composer 2 cheatsheet

Composer 2: Dependencies

14 common Composer 2 tasks.

Install dependencies

Install the exact versions listed in composer.lock, or resolve and write a lock file if none exists.

composer install

Add a package

Add a package to composer.json and install it.

composer require guzzlehttp/guzzle

Add a development package

Add a package needed only during development, such as a test framework.

composer require --dev phpunit/phpunit

Add a package at a version

Require a version constraint. ^7.0 allows any 7.x release at or above 7.0.

composer require guzzlehttp/guzzle:^7.0

Remove a package

Remove a package from composer.json and the vendor folder.

composer remove guzzlehttp/guzzle

Update all packages

Update dependencies to the newest versions allowed by composer.json, and rewrite composer.lock.

composer update

Caution: This can change many package versions at once. Run your tests and review the lock file diff.

Update one package

Update a single package and the dependencies it needs.

composer update vendor/package --with-dependencies

Find outdated packages

List installed packages that have newer versions.

composer outdated

Find outdated direct dependencies

Limit the list to packages you require directly.

composer outdated --direct

List installed packages

List installed packages with their versions.

composer show

Show the dependency tree

Print installed packages and their dependencies as a tree.

composer show --tree

Find why a package is installed

List the packages that depend on a given package.

composer why monolog/monolog

Find why a version can't be installed

Explain which requirements block installing a package or version.

composer why-not symfony/console 7.0

Check for security advisories

Check installed packages for known vulnerabilities.

composer audit

Was this page helpful?