Six Lesser Known Composer Commands You Should Know

Composer is the go to package manager for PHP. If you’re a PHP developer you more than likely use it every day and run the commands require, install and update frequently. What you perhaps don’t realise is that there are a load of other Composer commands at our disposal that can make working with the package manager a lot easier.

There are many commands available, but today I am going to share six that I use and find helpful on daily basis.

show

First up is the show command. It lets you view all the installed packages of a project (including the dependencies) as well as view information about a specific package. All of this information can be found in the Composer lock file, but using show provides an easier and more convenient way of extracting the information.

To list out all installed packages with their version number and description just use show:

composer show

It’s sometimes useful to view this information as a dependency tree which can be done by passing either the --tree or -t flags:

composer show -t

If you want to filter the returned packages you can pass an extra string parameter using the wildcard *:

composer show 'symfony/*'

This will return all the installed Symfony packages. Note the quotation marks here, if you’re using a bash shell you shouldn’t need these, but if you use zsh you’ll get a ‘no matches found’ error if you don’t.

If you want to view information about a specific package include the package name:

composer show laravel/framework

This will show you things like the installed version, its license and dependencies and where it has been installed locally.

why

If you want to know why a particular package has been installed you can use the why command to determine which dependencies are requiring it:

composer why vlucas/phpdotenv

why is an alias of the depends command, but personally I find it easier to remember to use why. You can use the --tree or -t flags to view this information in a dependency tree:

composer why vlucas/phpdotenv -t

why-not

Sometimes one or more of your installed packages will prevent a package from being installed or updated. To check for blockages we can use the why-not command (an alias of prohibits). For example, Laravel recently released a new 5.8 version of the framework; we can use the why-not command to check for any packages that will prevent us updating the laravel/framework package:

composer why-not laravel/framework 5.8

Again, we can use the --tree or -t flags to view this information in a dependency tree:

composer why-not laravel/framework 5.8 -t

outdated

Before you run composer update you might want to check which installed packages have updates available. You can do this using the outdated command.

composer outdated

This one is an alias of composer show -lo.

The results come back colour coded to indicate the status of each package according to semantic versioning:

  • Green: the installed package is already the latest version
  • Yellow: there is an update available, but may involve breaking changes
  • Red: there is a minor update available (usually bug fixes)

If you only want to see the minor update versions highlighted you can use the --minor-only or -m flag with the outdated command:

composer outdated -m

status

I often find myself working with dependencies installed from source using the --prefer-source flag with install and/or update. Then if I’ve modified any of those dependencies I need a quick way of checking which packages have been modified. The status command provides a convenient method for doing this.

You can get a breakdown of the packages and files within them that have been modified locally using the --verbose or -v flag:

composer status -v

I find using the verbose flag is the most useful way of using this command.

licenses

Finally, it can be really useful to know what licenses each package you’ve got installed has. Composer has a handy licenses command for retrieving a complete list of these:

composer licenses

Thanks for Reading

So those are my six go to commands beyond the usual require, install and update. I hope that you’ve found this useful and learnt something new about Composer.

What Composer commands do you find most useful? Let me know on Twitter where you can follow me for updates on future blog posts.

Related Content

Published on