如何在 Ubuntu 22.04 上安装 Composer [快速入门]

介绍

在本快速启动指南中,我们将安装 Composer在Ubuntu 22.04 服务器上。

对于本教程的更详细版本,以及每个步骤的更多解释,请参阅 如何在Ubuntu 22.04上安装和使用作曲家

前提条件

要遵循本指南,您需要作为sudo用户访问 Ubuntu 22.04 服务器。

步骤 1 - 安装依赖性

首先,更新您的包管理器缓存并安装所需的依赖,包括php-cli:

1sudo apt update
2sudo apt install php-cli unzip

步骤 2:下载和安装 Composer

请确保您位于您的主目录中,然后使用curl获取 Composer installer:

1cd ~
2curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php

接下来,我们将检查下载的安装程序是否匹配在 Composer Public Keys / Signatures页面上找到的最新安装程序的 SHA-384 哈希。

使用弯曲,获取最新的签名并将其存储在壳变量中:

1HASH=`curl -sS https://composer.github.io/installer.sig`

现在执行以下PHP代码来验证安装脚本是安全的运行:

1php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

您将看到以下输出:

1[label Output]
2Installer verified

<$>[注] :如果输出表示安装程序破坏,则需要重复下载和验证过程,直到您有验证的安装程序。

下列命令将下载并安装Composer作为名为composer的整个系统命令,在/usr/local/bin下:

1sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

你会看到类似于此的输出:

1[secondary_label Output]
2All settings correct for using Composer
3Downloading...
4
5Composer (version 2.3.5) successfully installed to: /usr/local/bin/composer
6Use it: php /usr/local/bin/composer

要测试您的安装,运行:

1composer
 1[secondary_label Output]
 2   ______
 3  / ____/___ ____ ___ ____ ____ ________ _____
 4 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
 5/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
 6\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
 7                    /_/
 8Composer version 2.3.5 2022-04-13 16:43:00
 9
10Usage:
11  command [options] [arguments]
12
13Options:
14  -h, --help Display help for the given command. When no command is given display help for the list command
15  -q, --quiet Do not output any message
16  -V, --version Display this application version
17      --ansi|--no-ansi Force (or disable --no-ansi) ANSI output
18  -n, --no-interaction Do not ask any interactive question
19      --profile Display timing and memory usage information
20      --no-plugins Whether to disable plugins.
21      --no-scripts Skips the execution of all scripts defined in composer.json file.
22  -d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
23      --no-cache Prevent use of the cache
24  -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
25...

这验证了 Composer 已成功安装在您的系统上,并且在整个系统中可用。

相关教程

以下是有关本教程的更多详细指南的链接:

https://andsky.com/tech/tutorials/如何安装-linux-apache-mysql-php-lamp-stack-on-ubuntu-22-04)

Published At
Categories with 技术
comments powered by Disqus