安裝 - *nix
下載 Composer 的可執(zhí)行文件
curl -sS https://getcomposer.org/installer | php
注意: 如果上述方法由于某些原因失敗了蚁袭,你還可以通過 php >下載安裝器:
php -r "readfile('https://getcomposer.org/installer');" | php
這將檢查一些 PHP 的設置,然后下載 composer.phar 到你的工作目錄中石咬。這是 Composer 的二進制文件揩悄。這是一個 PHAR 包(PHP 的歸檔),這是 PHP 的歸檔格式可以幫助用戶在命令行中執(zhí)行一些操作鬼悠。
局部安裝
你可以通過 --install-dir 選項指定 Composer 的安裝目錄(它可以是一個絕對或相對路徑):
php composer-setup.php --install-dir=bin --filename=composer
全局安裝
你可以將此文件放在任何地方删性。如果你把它放在系統(tǒng)的 PATH 目錄中亏娜,你就能在全局訪問它。 在類Unix系統(tǒng)中蹬挺,你甚至可以在使用時不加 php 前綴维贺。
你可以執(zhí)行這些命令讓 composer 在你的系統(tǒng)中進行全局調用:
mv composer.phar /usr/local/bin/composer
現(xiàn)在只需要運行 composer
命令就可以使用 Composer 而不需要輸入 php composer.phar
。
[elastic@iZm5ebjvditkx9x8pm3dw4Z bin]$ composer
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 1.5.1 2017-08-09 16:07:22
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
--no-plugins Whether to disable plugins.
-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 Shows the short information about Composer.
archive Creates 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 Sets config options.
create-project Creates new project from a package into given directory.
depends Shows which packages cause the given package to be installed.
diagnose Diagnoses the system to identify common errors.
dump-autoload Dumps the autoloader.
dumpautoload Dumps the autoloader.
exec Executes a vendored binary/script.
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 Shows 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 Shows information about licenses of dependencies.
list Lists commands
outdated Shows a list of installed packages that have updates available, including their latest version.
prohibits Shows which packages prevent the given package from being installed.
remove Removes a package from the require or require-dev.
require Adds required packages to your composer.json and installs them.
run-script Runs the scripts defined in composer.json.
search Searches for packages.
self-update Updates composer.phar to the latest version.
selfupdate Updates composer.phar to the latest version.
show Shows information about packages.
status Shows a list of locally modified packages.
suggests Shows package suggestions.
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.
why Shows which packages cause the given package to be installed.
why-not Shows which packages prevent the given package from being installed.
[elastic@iZm5ebjvditkx9x8pm3dw4Z bin]$
修改鏡像源地址:
為了提升下載安裝包的速度,建議將composer的鏡像源修改為國內(nèi)的源:
官方推薦有兩種使用方法,使用任意一種均可扑庞。
方法一: 修改 composer 的全局配置文件(推薦方式)
打開命令行窗口(windows用戶)或控制臺(Linux贰拿、Mac 用戶)并執(zhí)行如下命令:
composer config -g repo.packagist composer https://packagist.phpcomposer.com
方法二: 修改當前項目的 composer.json 配置文件:
打開命令行窗口(windows用戶)或控制臺(Linux、Mac 用戶),進入你的項目的根目錄(也就是 composer.json 文件所在目錄),執(zhí)行如下命令:
composer config repo.packagist composer https://packagist.phpcomposer.com
上述命令將會在當前項目中的 composer.json 文件的末尾自動添加鏡像的配置信息(你也可以自己手工添加):
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
以 laravel 項目的 composer.json 配置文件為例,執(zhí)行上述命令后如下所示(注意最后幾行):
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*"
},
"config": {
"preferred-install": "dist"
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
}
OK肢簿,一切搞定!試一下 composer install 來體驗飛一般的速度吧蜻拨!
鏡像原理:
一般情況下池充,安裝包的數(shù)據(jù)(主要是 zip 文件)一般是從 github.com 上下載的,安裝包的元數(shù)據(jù)是從 packagist.org 上下載的官觅。
然而纵菌,由于眾所周知的原因,國外的網(wǎng)站連接速度很慢休涤,并且隨時可能被“墻”甚至“不存在”咱圆。
“Packagist 中國全量鏡像”所做的就是緩存所有安裝包和元數(shù)據(jù)到國內(nèi)的機房并通過國內(nèi)的 CDN 進行加速,這樣就不必再去向國外的網(wǎng)站發(fā)起請求功氨,從而達到加速 composer install 以及 composer update 的過程序苏,并且更加快速、穩(wěn)定捷凄。因此忱详,即使 packagist.org、github.com 發(fā)生故障(主要是連接速度太慢和被墻)跺涤,你仍然可以下載匈睁、更新安裝包。
修改composer地址參考文檔: Packagist / Composer中國全量鏡像
附:全局安裝英文原文:
Globally#
You can place the Composer PHAR anywhere you wish. If you put it in a directory that is part of your PATH
, you can access it globally. On unixy systems you can even make it executable and invoke it without directly using the php
interpreter.
After running the installer following the Download page instructions you can run this to move composer.phar to a directory that is in your path:
mv composer.phar /usr/local/bin/composer
Note:
If the above fails due to permissions, you may need to run it again with sudo.
Note:
On some versions of OSX the/usr
directory does not exist by default. If you receive the error "/usr/local/bin/composer: No such file or directory" then you must create the directory manually before proceeding:mkdir -p /usr/local/bin
.
Note:
For information on changing your PATH, please read the Wikipedia article and/or use Google.
Now just run composer
in order to run Composer instead of php composer.phar
.