如何在 Ubuntu 16.04 上安装和配置 Composer
在本文中,我们将学习如何配置和安装 Composer。Composer 是一种用于管理 PHP 依赖项的工具,它有助于简化项目依赖项的安装和更新,并显示项目需求所需的相应版本。
先决条件
- 一台安装了 Ubuntu 16.04 的机器。
- 具有 root 权限的非 root 用户。
安装依赖项
在安装 Composer 之前,我们需要使用以下命令更新系统。
$ sudo apt-get update
系统更新完成后,我们将继续进行安装设置,在此过程中我们将安装运行 Composer 所需的软件包。
$ sudo apt-get install curl php-cli git -yReading package lists... Done Building dependency tree Reading state information... Done git is already the newest version (1:2.7.4-0ubuntu1). curl is already the newest version (7.47.0-1ubuntu2.2). The following NEW packages will be installed: php-cli 0 upgraded, 1 newly installed, 0 to remove and 112 not upgraded. Need to get 2,920 B of archives. After this operation, 11.3 kB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 php-cli all 1:7.0+35ubuntu6 [2,920 B] Fetched 2,920 B in 0s (9,032 B/s) Selecting previously unselected package php-cli. (Reading database ... 92686 files and directories currently installed.) Preparing to unpack .../php-cli_1%3a7.0+35ubuntu6_all.deb ... Unpacking php-cli (1:7.0+35ubuntu6) ... Setting up php-cli (1:7.0+35ubuntu6) ...
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
安装 Composer
我们将使用 Composer 官方文档中提供的安装说明。
Composer 将安装在 /usr/local/bin 目录下。
使用以下命令在 /tmp 文件夹中下载 Composer。
$ sudo php -r "copy('https://getcomposer.org.cn/installer', '/tmp/composer-setup.php');"
下载脚本后,我们将使用以下命令安装软件包,并使用 --install-dir 标记将 Composer 安装到 /usr/local/bin 目录下。
$ sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer Output: All settings correct for using Composer Downloading... Composer (version 1.4.1) is successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
安装完成后,我们将运行以下命令来验证安装和版本。
$ composer --version Output: Composer version 1.4.1 2017-03-10 09:29:45
最后,我们将使用以下命令从 /tmp 目录中删除安装文件。
$ rm -rf /tmp/composer-setup.php
安装所需的软件包
要使用 Composer 与项目一起使用,我们需要一个 composer.json 文件,该文件告诉 Composer 项目需要哪些依赖项。我们需要在项目文件夹中手动创建 composer.json 文件。
还要注意,当您向项目添加依赖项时,我们需要使用 Composer 运行必要的命令,这将自动添加依赖项,无需手动添加。
Composer 使用以下步骤来为项目安装软件包:
它识别应用程序需要哪些类型的库。
它将在 Packagist.org(Composer 的官方存储库)中搜索合适的库。
我们可以选择项目所需的正确依赖项。
运行 Composer 需要一个命令来将依赖项包含在 composer.json 中并安装软件包。
为项目创建一个文件夹,并切换到用户父目录中的该项目。
$ cd ~$ $ mkdir demoporj $ cd demoproj
运行以下命令创建 composer.json 文件。
$ composer require cocur/slugify Output: Using version ^2.4 for cocur/slugify ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals The php.ini used by your command-line PHP is: /etc/php/7.0/cli/php.ini Now trying to download from source- Installing cocur/slugify (v2.4): Cloning f11f22d4e6 from cache Writing lock file Generating autoload files
包含自动加载脚本
Composer 有一个自动加载脚本,我们可以将其包含在项目中以获得自动加载功能,该功能使依赖项能够自动加载并定义自己的命名空间。我们只需要在加载任何类库之前在 PHP 脚本中包含 vendor/autoload.php 即可。
例如,我们需要包含此脚本以加载自动加载脚本并更新依赖项。
$ vi load.php <?php require __DIR__ . '/vendor/autoload.php'; echo ('Hello World, this is a demo sentence');
只需运行以下命令即可检查自动加载脚本是否有效。
$ php load.php Output: Hello World, this is a demo sentence
在本文中,我们学习了如何从官方存储库安装 Composer,如何创建 composer.json 文件以及如何在安装任何新依赖项时自动更新依赖项。