641

I am trying to install a specific version of a package using Composer. I tried composer install and composer require but they are installing the latest version of the package. What if I want an older version?

Jan Żankowski
  • 8,690
  • 7
  • 38
  • 52
gdaras
  • 9,401
  • 2
  • 23
  • 39
  • have a read on this answer http://stackoverflow.com/questions/15212381/composer-how-can-i-install-another-dependency-without-updating-old-ones – Anastasis Dec 01 '16 at 15:18

7 Answers7

1094

composer require vendor/package:version

for example:

composer require refinery29/test-util:0.10.2

alucic
  • 12,492
  • 2
  • 20
  • 25
  • 13
    what is the function ^ and ~ in version number? – Dedi Ananto Feb 07 '18 at 08:28
  • 5
    You can find the answer here https://stackoverflow.com/a/22345808/1522019 – alucic Feb 13 '18 at 11:21
  • 1
    @DediAnanto please look at this link from npm packages similar interaction. https://stackoverflow.com/questions/22343224/whats-the-difference-between-tilde-and-caret-in-package-json – okandas Jul 05 '19 at 13:12
  • `composer require vendor/package:version --with-all-dependencies` to make sure all of its dependency will be updated. – free2idol1 Oct 27 '22 at 11:34
254

Add double quotes to use "^" caret operator in version number.

composer require middlewares/whoops "^0.4"
Kamlesh
  • 5,233
  • 39
  • 50
Yoann Kergall
  • 2,993
  • 5
  • 22
  • 23
  • 4
    I don't see a point in time where you could pass [package name](https://github.com/composer/composer/blob/1.0.0/doc/03-cli.md#require) and [version](https://github.com/composer/composer/blob/1.0.0/src/Composer/Command/RequireCommand.php#L41) as two separate arguments. – x-yuri Jun 13 '18 at 11:04
  • 3
    @x-yuri passing package name and version as separate arguments works for me using composer 1.5.1 – bryonbean Nov 15 '18 at 16:53
  • 2
    While technically this isn't the "correct" answer for the OP's question, this is probably more useful for most people. – Rich Court Jul 02 '19 at 09:25
  • 1
    If you're using PowerShell and Git for Windows the caret needs to be escaped twice: "^^^^0.4" or just use Git Bash or cmd.exe. Ref: https://stackoverflow.com/questions/9600549/using-a-caret-when-using-git-for-windows-in-powershell#_=_ – Dean Or Nov 06 '22 at 17:38
52

As @alucic mentioned, use:

composer require vendor/package:version

or you can use:

composer update vendor/package:version

You should probably review this StackOverflow post about differences between composer install and composer update.

Related to question about version numbers, you can review Composer documentation on versions, but here in short:

  • Tilde Version Range (~) - ~1.2.3 is equivalent to >=1.2.3 <1.3.0
  • Caret Version Range (^) - ^1.2.3 is equivalent to >=1.2.3 <2.0.0

So, with Tilde you will get automatic updates of patches but minor and major versions will not be updated. However, if you use Caret you will get patches and minor versions, but you will not get major (breaking changes) versions.

Tilde Version is considered a "safer" approach, but if you are using reliable dependencies (well-maintained libraries) you should not have any problems with Caret Version (because minor changes should not be breaking changes.

milan.latinovic
  • 2,217
  • 1
  • 21
  • 26
35

just use php composer.phar require

For example :

php composer.phar require doctrine/mongodb-odm-bundle 3.0

Also available with install.

https://getcomposer.org/doc/03-cli.md#require https://getcomposer.org/doc/03-cli.md#install

Aize
  • 510
  • 3
  • 10
  • For the record, adding custom constraints is *not* available with `install` - only with `require`. If you were hoping to switch to a specific version and check-in your `composer.lock` file, you can, but you'd have to use `composer require` and then revert the change to `composer.json` afterwards. – mindplay.dk Dec 11 '17 at 12:17
  • Version as a separate argument? Was that ever available? – x-yuri Jun 13 '18 at 11:05
21

Suppose you want to install Laravel Collective. It's currently at version 6.x but you want version 5.8. You can run the following command:

composer require "laravelcollective/html":"^5.8.0"

A good example is shown here in the documentation: https://laravelcollective.com/docs/5.5/html

Jay
  • 229
  • 2
  • 3
  • Please share more details - the version constraint `^5.8.0` would not restrict Composer to use a specific version, but still allow multiple versions to be installed – Nico Haase Mar 14 '21 at 14:20
4

In your composer.json, you can put:

{
    "require": {
        "vendor/package": "version"
    }
}

then run composer install or composer update from the directory containing composer.json. Sometimes, for me, composer is hinky, so I'll start with composer clear-cache; rm -rf vendor; rm composer.lock before composer install to make sure it's getting fresh stuff.


Of course, as the other answers point out you can run the following from the terminal:

composer require vendor/package:version

And on versioning:
- Composer's official versions article
- Ecosia Search

Reed
  • 14,703
  • 8
  • 66
  • 110
4

I tried to require a development branch from a different repository and not the latest version and I had the same issue and non of the above worked for me :(

after a while I saw in the documentation that in cases of dev branch you need to require with a 'dev-' prefix to the version and the following worked perfectly.

composer require [vendorName]/[packageName]:dev-[gitBranchName]
silver
  • 1,633
  • 1
  • 20
  • 32