如何在Debian/Ubuntu Linux上安装Composer

时间:2020-01-09 10:39:18  来源:igfitidea点击:

升级Mediawiki安装时收到一条错误消息,内容如下:

Error: your composer.lock file is not up to date, run “composer update” to install newer dependencies

如何在Debian或Linux上为PHP安装安装composer命令运行LAMP或LEMP堆栈的Ubuntu Linux?
如何在正在运行的Ubuntu Linux服务器上安装和使用Composer,并解决该问题以进行Mediawiki安装和/或管理和安装项目的PHP软件包以及相关性?

说明:Composer是PHP的依赖项管理器工具。
它会为您的项目安装和更新库。
它不是像yum命令或apt-get命令这样的软件包管理器。
它旨在与每个单独的项目(如Mediawiki或您自己的用PHP编写的自定义应用程序)一起使用。
有许多使用php编写的开源项目和框架,都使用Composer来管理和安装它们的软件包和依赖项。
在本教程中,您将学习如何在Debian或Ubuntu Linux服务器上全局安装composer。
以下说明也应与RHEL/CentOS Linux 7.x或任何其他现代Linux发行版一起使用。

问题

当您尝试使用以下命令将MediaWiki更新到v1.26.0时:

[[email protected]:~/wiki]$ `php update.php`

您将收到以下错误:

MediaWiki 1.26.0 Updater

composer/semver: not installed, 1.0.0 required.
liuggio/statsd-php-client: 1.0.12 installed, 1.0.16 required.
oyejorge/less.php: not installed, 1.7.0.9 required.
mediawiki/at-ease: not installed, 1.1.0 required.
oojs/oojs-ui: 0.11.3 installed, 0.12.12 required.
wikimedia/assert: not installed, 0.2.2 required.
wikimedia/cdb: 1.0.1 installed, 1.3.0 required.
wikimedia/composer-merge-plugin: 1.0.0 installed, 1.3.0 required.
wikimedia/ip-set: not installed, 1.0.1 required.
wikimedia/utfnormal: 1.0.2 installed, 1.0.3 required.
wikimedia/wrappedstring: not installed, 2.0.0 required.
zordius/lightncandy: 0.18 installed, 0.21 required.
Error: your composer.lock file is not up to date, run "composer update" to install newer dependencies

要解决此问题,您需要安装Composer工具。

安装

确保在您的Debian或Ubuntu服务器(14.04 LTS或更早版本)上安装了curl和php-cli,curl及其相关工具:

$ sudo apt-get install curl php5-cli git

输出示例:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
php5-cli is already the newest version.
The following extra packages will be installed:
  git-man libcurl3 liberror-perl patch rsync
Suggested packages:
  gettext-base git-daemon-run git-daemon-sysvinit git-doc git-el git-email
  git-gui gitk gitweb git-arch git-bzr git-cvs git-mediawiki git-svn ed
  diffutils-doc openssh-server
The following NEW packages will be installed:
  curl git git-man libcurl3 liberror-perl patch rsync
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,011 kB of archives.
After this operation, 23.4 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libcurl3 amd64 7.35.0-1ubuntu2.5 [173 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty-updates/main rsync amd64 3.1.0-2ubuntu0.1 [283 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty-updates/main curl amd64 7.35.0-1ubuntu2.5 [123 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main liberror-perl all 0.17-1.1 [21.1 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ trusty-updates/main git-man all 1:1.9.1-1ubuntu0.1 [698 kB]
Get:6 http://archive.ubuntu.com/ubuntu/ trusty-updates/main git amd64 1:1.9.1-1ubuntu0.1 [2,627 kB]
Get:7 http://archive.ubuntu.com/ubuntu/ trusty-updates/main patch amd64 2.7.1-4ubuntu2.3 [86.4 kB]
Fetched 4,011 kB in 1s (2,181 kB/s)
Selecting previously unselected package libcurl3:amd64.
(Reading database ... 16719 files and directories currently installed.)
Preparing to unpack .../libcurl3_7.35.0-1ubuntu2.5_amd64.deb ...
Unpacking libcurl3:amd64 (7.35.0-1ubuntu2.5) ...
Selecting previously unselected package rsync.
Preparing to unpack .../rsync_3.1.0-2ubuntu0.1_amd64.deb ...
Unpacking rsync (3.1.0-2ubuntu0.1) ...
Selecting previously unselected package curl.
Preparing to unpack .../curl_7.35.0-1ubuntu2.5_amd64.deb ...
Unpacking curl (7.35.0-1ubuntu2.5) ...
Selecting previously unselected package liberror-perl.
Preparing to unpack .../liberror-perl_0.17-1.1_all.deb ...
Unpacking liberror-perl (0.17-1.1) ...
Selecting previously unselected package git-man.
Preparing to unpack .../git-man_1%3a1.9.1-1ubuntu0.1_all.deb ...
Unpacking git-man (1:1.9.1-1ubuntu0.1) ...
Selecting previously unselected package git.
Preparing to unpack .../git_1%3a1.9.1-1ubuntu0.1_amd64.deb ...
Unpacking git (1:1.9.1-1ubuntu0.1) ...
Selecting previously unselected package patch.
Preparing to unpack .../patch_2.7.1-4ubuntu2.3_amd64.deb ...
Unpacking patch (2.7.1-4ubuntu2.3) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up libcurl3:amd64 (7.35.0-1ubuntu2.5) ...
Setting up rsync (3.1.0-2ubuntu0.1) ...
Removing any system startup links for /etc/init.d/rsync ...
update-rc.d: warning: default stop runlevel arguments (0 1 6) do not match rsync Default-Stop values (none)
Adding system startup for /etc/init.d/rsync ...
   /etc/rc0.d/K20rsync -> ../init.d/rsync
   /etc/rc1.d/K20rsync -> ../init.d/rsync
   /etc/rc6.d/K20rsync -> ../init.d/rsync
   /etc/rc2.d/S20rsync -> ../init.d/rsync
   /etc/rc3.d/S20rsync -> ../init.d/rsync
   /etc/rc4.d/S20rsync -> ../init.d/rsync
   /etc/rc5.d/S20rsync -> ../init.d/rsync
Setting up curl (7.35.0-1ubuntu2.5) ...
Setting up liberror-perl (0.17-1.1) ...
Setting up git-man (1:1.9.1-1ubuntu0.1) ...
Setting up git (1:1.9.1-1ubuntu0.1) ...
Setting up patch (2.7.1-4ubuntu2.3) ...
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
Processing triggers for ureadahead (0.100.0-16) ...

如果您使用的是Ubuntu Linux 16.04 LTS或更高版本,并且想使用PHP 7.x,请运行:

$ sudo apt install curl php7.0-cli git

安装Composer工具

接下来在Debian或Ubuntu Linux上的/usr/local/bin /目录下安装composer,如下所示:

$ curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

验证Composer

要验证安装类型:

$ composer

或者

$ /usr/local/bin/composer

输出示例:

______
  / ____/___  ____ ___  ____  ____  ________  _____
/ /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.0-dev (1c525b76f81123af180743d31c208c29351cf931) 2014-12-09 15:47:26
 
Usage:
  command [options] [arguments]
 
Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
 
Available commands:
  about           Short information about Composer
  archive         Create an archive of this composer package
  browse          Opens the package's repository URL or homepage in your browser.
  clear-cache     Clears composer's internal package cache.
  clearcache      Clears composer's internal package cache.
  config          Set config options
  create-project  Create new project from a package into given directory.
  depends         Shows which packages depend on the given package
  diagnose        Diagnoses the system to identify common errors.
  dump-autoload   Dumps the autoloader
  dumpautoload    Dumps the autoloader
  global          Allows running commands in the global composer dir ($COMPOSER_HOME).
  help            Displays help for a command
  home            Opens the package's repository URL or homepage in your browser.
  info            Show information about packages
  init            Creates a basic composer.json file in current directory.
  install         Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  licenses        Show information about licenses of dependencies
  lint            Run the lint script as defined in composer.json.
  list            Lists commands
  phpcs           Run the phpcs script as defined in composer.json.
  remove          Removes a package from the require or require-dev
  require         Adds required packages to your composer.json and installs them
  run-script      Run the scripts defined in composer.json.
  search          Search for packages
  self-update     Updates composer.phar to the latest version.
  selfupdate      Updates composer.phar to the latest version.
  show            Show information about packages
  status          Show a list of locally modified packages
  suggests        Show package suggestions
  test            Run the test script as defined in composer.json.
  update          Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate        Validates a composer.json and composer.lock

Composer如何工作?

要使用Composer,您需要创建或使用供应商提供的composer.json文件,例如mediawiki。
该文件定义了php应用程序或项目的依赖项。
这是来自mediawiki项目的示例文件:

{
        "name": "mediawiki/core",
        "description": "Free software wiki application developed by the Wikimedia Foundation and others",
        "keywords": ["mediawiki", "wiki"],
        "homepage": "https://www.mediawiki.org/",
        "authors": [
                {
                        "name": "MediaWiki Community",
                        "homepage": "https://www.mediawiki.org/wiki/Special:Version/Credits"
                }
        ],
        "license": "GPL-2.0+",
        "support": {
                "issues": "https://bugs.mediawiki.org/",
                "irc": "irc://irc.freenode.net/mediawiki",
                "wiki": "https://www.mediawiki.org/"
        },
        "require": {
                "composer/semver": "1.0.0",
                "cssjanus/cssjanus": "1.1.1",
                "ext-iconv": "*",
                "liuggio/statsd-php-client": "1.0.16",
                "oyejorge/less.php": "1.7.0.9",
                "mediawiki/at-ease": "1.1.0",
                "oojs/oojs-ui": "0.12.12",
                "php": ">=5.3.3",
                "psr/log": "1.0.0",
                "wikimedia/assert": "0.2.2",
                "wikimedia/cdb": "1.3.0",
                "wikimedia/composer-merge-plugin": "1.3.0",
                "wikimedia/ip-set": "1.0.1",
                "wikimedia/utfnormal": "1.0.3",
                "wikimedia/wrappedstring": "2.0.0",
                "zordius/lightncandy": "0.21"
        }
}

如何安装在composer.json文件中定义的依赖项?

只需执行以下命令即可下载并安装您的应用程序/项目的所有依赖项:

$ composer install

或切换到核心项目目录,例如/var/www/html/wiki并运行:

$ cd /var/www/html/wiki && composer install --no-dev

输出示例:

Loading composer repositories with package information
Installing dependencies from lock file
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.
  - Removing composer/semver (1.0.0)
  - Removing oyejorge/less.php (v1.7.0.9)
  - Removing mediawiki/at-ease (v1.1.0)
  - Removing wikimedia/avro (v1.7.7)
  - Removing nmred/kafka-php (v0.1.4)
  - Removing wikimedia/ip-set (1.0.1)
  - Removing firebase/php-jwt (v2.1.0)
  - Removing symfony/process (v2.7.3)
  - Removing ruflin/elastica (2.2.0)
  - Removing wikimedia/wrappedstring (v2.0.0)
  - Removing monolog/monolog (1.14.0)
  - Removing kzykhys/pygments (v1.0.0)
  - Removing wikimedia/assert (v0.2.2)
  - Removing wikimedia/composer-merge-plugin (v1.3.0)
  - Installing wikimedia/composer-merge-plugin (v1.0.0)
    Downloading: 100%         
 
  - Removing liuggio/statsd-php-client (v1.0.16)
  - Installing liuggio/statsd-php-client (v1.0.12)
    Downloading: 100%         
 
  - Removing oojs/oojs-ui (v0.12.12)
  - Installing oojs/oojs-ui (v0.11.3)
    Downloading: 100%         
 
  - Removing wikimedia/cdb (1.3.0)
  - Installing wikimedia/cdb (1.0.1)
    Downloading: 100%         
 
  - Removing wikimedia/utfnormal (v1.0.3)
  - Installing wikimedia/utfnormal (v1.0.2)
    Downloading: 100%         
 
  - Removing zordius/lightncandy (v0.21)
  - Installing zordius/lightncandy (v0.18)
    Downloading: 100%         
 
  - Installing leafo/lessphp (v0.5.0)
    Downloading: 100%         
 
Generating optimized autoload files

上面还将创建composer.lock文件,该文件可用于在以后将依赖关系更新到最新版本。

如何更新composer.lock文件?

如果没有.lock文件,则此工具将根据.json文件中的依赖关系创建一个文件。
要将依赖库获取到较新的版本,请运行以下命令,即,如果您已有一个composer.lock文件,则需要按以下方式运行它:

$ composer update

或切换到核心项目目录,例如/var/www/html/wiki并运行:

$ cd /var/www/html/wiki/ && composer update --no-dev

输出示例:

> ComposerHookHandler::onPreUpdate
Deprecation Notice: The Composer\Package\LinkConstraint\VersionConstraint class is deprecated, use Composer\Semver\Constraint\Constraint instead. in phar:///usr/local/bin/composer/src/Composer/Package/LinkConstraint/VersionConstraint.php:17
Deprecation Notice: The Composer\Package\LinkConstraint\LinkConstraintInterface interface is deprecated, use Composer\Semver\Constraint\ConstraintInterface instead. in phar:///usr/local/bin/composer/src/Composer/Package/LinkConstraint/LinkConstraintInterface.php:17
Loading composer repositories with package information
Updating dependencies
 
  - Removing leafo/lessphp (v0.5.0)
  - Removing wikimedia/composer-merge-plugin (v1.0.0)
  - Installing wikimedia/composer-merge-plugin (v1.3.0)
    Downloading: Connecting...
    Downloading: 100%         
 
  - Removing liuggio/statsd-php-client (v1.0.12)
  - Installing liuggio/statsd-php-client (v1.0.16)
    Downloading: Connecting...
    Downloading: 100%         
 
  - Installing mediawiki/at-ease (v1.1.0)
    Downloading: 100%         
 
  - Removing oojs/oojs-ui (v0.11.3)
  - Installing oojs/oojs-ui (v0.12.12)
    Downloading: 100%         
 
  - Removing wikimedia/cdb (1.0.1)
  - Installing wikimedia/cdb (1.3.0)
    Downloading: 100%         
 
  - Removing wikimedia/utfnormal (v1.0.2)
  - Installing wikimedia/utfnormal (v1.0.3)
    Downloading: 100%         
 
  - Removing zordius/lightncandy (v0.18)
  - Installing zordius/lightncandy (v0.21)
    Downloading: 100%         
 
  - Installing composer/semver (1.0.0)
    Downloading: 100%         
 
  - Installing oyejorge/less.php (v1.7.0.9)
    Downloading: 100%         
 
  - Installing wikimedia/assert (v0.2.2)
    Downloading: 100%         
 
  - Installing wikimedia/ip-set (1.0.1)
    Downloading: 100%         
 
  - Installing wikimedia/wrappedstring (v2.0.0)
    Downloading: 100%         
 
Writing lock file
Generating optimized autoload files

最终将MediaWiki更新到版本1.26.0

现在,由于Composer已经为您的php应用程序获取了必需的库,mediawiki将被更新而没有任何问题:

$ cd /var/www/html/wiki/
$ php update.php