156

Suppose I'm writing a library A, that depends on another library, monolog for instance.

I want to install the latest version of monolog, so I just put this inside composer.json:

{
    "require": {
        "monolog/monolog": "*.*.*"
    }
}

Then I run $ php composer.phar install.

I was expecting to find the version installed, inside composer.lock, but it's not there:

{
    "hash": "d7bcc4fe544b4ef7561918a8fc6ce009",
    "packages": [
        {
            "package": "monolog/monolog",
            "version": "dev-master",
            "source-reference": "2eb0c0978d290a1c45346a1955188929cb4e5db7"
        }
    ],
    "packages-dev": null,
    "aliases": [

    ],
    "minimum-stability": "dev",
    "stability-flags": [

    ]
}

I need the version because I want to tie my library to a specific set of versions, eg: If I find the version is 1.3.5, in my composer.json I would like to put something like this:

    "require": {
        "monolog/monolog": "1.3.*"
    }

Any ideas?

Andy
  • 4,901
  • 5
  • 35
  • 57
HappyDeveloper
  • 12,480
  • 22
  • 82
  • 117
  • 1
    A somewhat related tip for this question: if you are using PhpStorm as your IDE, any `composer.json` files will show you next to each dependency the actual version you have installed. – BadHorsie Mar 01 '22 at 13:40

8 Answers8

233

I know it's an old question, but...

composer.phar show

Will show all the currently installed packages and their version information. (This was shown in previous versions of Composer only when using the now-deprecated -i option.)

To see more details, specify the name of the package as well:

composer.phar show monolog/monolog

That will show many things, including commit MD5 hash, source URL, license type, etc.

Mr. Lance E Sloan
  • 3,297
  • 5
  • 35
  • 50
Ross Deane
  • 3,100
  • 2
  • 19
  • 25
  • 13
    `php composer.phar show -a` and `php composer.phar show package/name` are also both helpful. – bishop Aug 26 '14 at 01:04
  • 1
    For `dev-master` packages, this is useful, because it also shows the commit hash. So you need to go to GitHub, find that commit hash, check the date, and then find the tag with the nearest date before that, to really find out what "version" you are using – andrewtweber Feb 03 '16 at 19:39
  • 3
    it can also be find in composer.lock file. – Hafiz Jun 08 '17 at 13:48
  • To list all packages starting with a certain string, you can use the `*` wildcard: `composer show 'package/*'` – meyegui Jul 28 '23 at 11:58
76

You can use composer show like this:

composer show package/name
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Kévin Ferradj
  • 876
  • 7
  • 7
21

If you're just interested to get the output as the package version number like: 1.7.5 or 1.x-dev or dev-master.

Linux console snippet (composer & sed):

composer show 'monolog/monolog' | sed -n '/versions/s/^[^0-9]\+\([^,]\+\).*$/\1/p'

or (composer, grep & cut):

composer show 'monolog/monolog' | grep 'versions' | grep -o -E '\*\ .+' | cut -d' ' -f2 | cut -d',' -f1;
hakre
  • 193,403
  • 52
  • 435
  • 836
Jimmix
  • 5,644
  • 6
  • 44
  • 71
11

You can use show all, specially when dont have package.json file, get available packages from packagist.org:

composer show "monolog/monolog" --all

Also you can specify versions

composer show "monolog/monolog" 1.* --all
Mohsen
  • 4,049
  • 1
  • 31
  • 31
7

If you are using git version control system, you will search easily for any package

composer show |grep packagename

For Example

composer show |grep monolog

If you aren't installing git, you can install grep program from this link, link it with environment variables and write same previous command in Cmd

If you don't know how to link program with environment variables, view this link after linking it write the same command on the above

Eng_Farghly
  • 1,987
  • 1
  • 23
  • 34
6

Technically "dev-master" is the exact version that you ended up using there. It is the development branch, and thus the very latest version.

The best place to look for available versions for composer packages is Packagist since that's the place composer loads the versions from when you install packages. The monolog versions are listed on http://packagist.org/packages/monolog/monolog.

naderman
  • 1,060
  • 11
  • 12
5

If you want to check the version within PHP itself, you can use the composer Runtime Utilities:

\Composer\InstalledVersions::getVersion('my/package')

See https://getcomposer.org/doc/07-runtime.md for more information.

Koeno
  • 1,493
  • 2
  • 16
  • 30
0

To find a package by name run this command ( laravel is an example)

composer search laravel

To find info about a package run this command ( laravel/laravel is an example)

composer show -a laravel/laravel
Namig Hajiyev
  • 1,117
  • 15
  • 16