DEV Community

Cover image for How to find the exact version of a composer package?
Alex
Alex

Posted on

How to find the exact version of a composer package?

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Sometimes you may need to know the versions of installed libraries, this article will show you how to do that.

Check if composer installed

The first thing you'll need to do is to check if composer is installed. You can do this by running any composer command., for example --version.

composer --version

# output
Composer version 2.2.4 2022-01-08 12:30:42
Enter fullscreen mode Exit fullscreen mode

If composer commands do not work, you will need install it first: Download composer.

Check the versions of all installed packages

You can check which packages are installed in the working directory by running one of the following commands - show or info:

composer show

# or

composer info
Enter fullscreen mode Exit fullscreen mode

It will show you package names, versions and descriptions

curl/curl                         2.5.0    cURL class for PHP
symfony/http-client               v6.3.2   Provides powerful methods to fetch HTTP resources synchronously or asynchronously
symfony/http-foundation           v6.3.4   Defines an object-oriented layer for the HTTP specification
symfony/yaml                      v6.3.3   Loads and dumps YAML files
Enter fullscreen mode Exit fullscreen mode

Check versions of globally installed packages

Sometimes you may need to check which packages are installed globally in the system. The show command should be run with the global keyword:

composer global show
Enter fullscreen mode Exit fullscreen mode

And you will see a list similar to the previous one, but containing only globally installed packages

curl/curl 2.5.0 cURL class for PHP
Enter fullscreen mode Exit fullscreen mode

Check the version of a specific package

For example, if you need to check what version of symfony/http-foundation is installed, you can do this as follows:

composer show symfony/http-foundation
Enter fullscreen mode Exit fullscreen mode

It will give you a lot of information about the package: name, description, version, requirements, conflicting packages and much more:

name     : symfony/http-foundation
descrip. : Defines an object-oriented layer for the HTTP specification
keywords : 
versions : * v6.3.4
type     : library
...
requires
php >=8.1
symfony/deprecation-contracts ^2.5|^3
symfony/polyfill-mbstring ~1.1
symfony/polyfill-php83 ^1.27
...
conflicts
symfony/cache <6.2
Enter fullscreen mode Exit fullscreen mode

If this amount of information is redundant, you can grep the versions only:

composer show symfony/http-foundation | grep versions

# output
versions : * v6.3.4
Enter fullscreen mode Exit fullscreen mode

The same results can be achieved using info command

composer info symfony/http-foundation | grep versions

# output
versions : * v6.3.4
Enter fullscreen mode Exit fullscreen mode

Check the package versions from a specific author/vendor

If you want to check the package version from a specific author/vendor, for example check the versions of installed Symfony components, you can pass a package mask using wildcards:

composer show "symfony/*"
Enter fullscreen mode Exit fullscreen mode

It will only show packages that start with symfony/

symfony/http-client               v6.3.2   Provides powerful methods to fetch HTTP resources synchronously or asynchronously
symfony/http-foundation           v6.3.4   Defines an object-oriented layer for the HTTP specification
symfony/yaml                      v6.3.3   Loads and dumps YAML files
Enter fullscreen mode Exit fullscreen mode

Bonus: composer why/depends

The composer show command shows the list of all installed packages, and you may find some unknown names in this list, e.g. libraries you didn't install, but which are present in your application. If you are curious how this happened, you can run the why or depends command:

composer why symfony/service-contracts

# or 

composer depends symfony/service-contract
Enter fullscreen mode Exit fullscreen mode

It tells you which other packages depend on a particular one and why it was installed even though it was not required directly:

symfony/http-client  v6.3.2  requires   symfony/service-contracts (^2.5|^3)  
symfony/translation  v6.3.3  conflicts  symfony/service-contracts (<2.5)
Enter fullscreen mode Exit fullscreen mode

Summary

Composer helps you manage your dependencies, as well as keep track of what version of certain packages are installed. The composer show or composer info commands can be used to find out the exact version of a composer package.

The composer why or composer depends commands are also useful to check why a particular package is installed or what other packages need it and in what version.

Top comments (0)