php 如何使用 Composer 安装特定版本的软件包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40914114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:17:05  来源:igfitidea点击:

How to install a specific version of package using Composer?

phpcomposer-phpdependency-management

提问by gdaras

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

我正在尝试使用 Composer 安装特定版本的软件包。我试过了composer installcomposer require但他们正在安装最新版本的软件包。如果我想要旧版本怎么办?

回答by alucic

composer require vendor/package:version

composer require vendor/package:version

for example:

例如:

composer require refinery29/test-util:0.10.2

composer require refinery29/test-util:0.10.2

回答by Yoann Kergall

Add double quotes to use caret operator in version number.

添加双引号以在版本号中使用插入符运算符。

composer require middlewares/whoops "^0.4"

回答by Aize

just use php composer.phar require

只需使用 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#requirehttps://getcomposer.org/doc/03-cli.md#install

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

回答by milan.latinovic

As @alucic mentioned, use:

正如@alucic 提到的,使用:

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.

您可能应该查看这篇 StackOverflow 帖子,了解 composer install 和 composer update 之间的差异

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

关于版本号的问题,您可以查看Composer 文档上的版本,但简而言之:

  • 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
  • 波浪号版本范围 ( ~) - ~1.2.3 相当于 >=1.2.3 < 1.3.0
  • 插入符号版本范围 ( ^) - ^1.2.3 相当于 >=1.2.3 < 2.0.0

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

因此,使用波浪号,您将获得补丁的自动更新,但不会更新次要和主要版本。但是,如果您使用Caret,您将获得补丁和次要版本,但不会获得主要(重大更改)版本。

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.

波浪号版本被认为是一种“更安全”的方法,但如果您使用可靠的依赖项(维护良好的库),则插入符号版本应该不会有任何问题(因为微小的更改不应该破坏更改。

回答by Jay LampStack

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:

假设您要安装 Laravel Collective。它目前是 6.x 版,但您需要 5.8 版。您可以运行以下命令:

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

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

文档中显示了一个很好的示例:https: //laravelcollective.com/docs/5.5/html

回答by Reed

In your composer.json, you can put:

在您的 中composer.json,您可以放置​​:

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

then run composer installor composer updatefrom the directory containing composer.json. Sometimes, for me, composer is hinky, so I'll start with composer clear-cache; rm -rf vendor; rm composer.lockbefore composer installto make sure it's getting fresh stuff.

然后运行composer installcomposer update从包含composer.json. 有时,对我来说,作曲家很笨拙,所以我会从composer clear-cache; rm -rf vendor; rm composer.lock之前开始以composer install确保它得到新鲜的东西。



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

关于版本控制:
- Composer 的官方版本文章
- Ecosia Search