如何在 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) ...

安裝 Composer

我們將使用 Composer 官方文件中提供的安裝說明。

Composer 將安裝在 /usr/local/bin 目錄下。

使用以下命令在 /tmp 資料夾中下載 Composer。

$ sudo php -r "copy('https://getcomposer.org/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 檔案以及如何在安裝任何新依賴項時自動更新依賴項。

更新於:2020年1月23日

367 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告