一、composer 安装
提示:运行 Composer 需要 PHP 5.3.2+ 以上版本
1. bin 文件安装
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
2.脚本自动安装
php -r "readfile('https://getcomposer.org/installer');" > composer-setup.php
php -r "if (hash('SHA384', file_get_contents('composer-setup.php')) === 'fd26ce67e3b237fffd5e5544b45b0d92c41a4afe3e3f778e942e43ce6be197b9cdc7c251dcde6e2a52297ea269370680') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); }"
php composer-setup.php
php -r "unlink('composer-setup.php');"
3.软件更新
composer selfupdate
二、修改镜像源
composerconfig -g repo.packagistcomposerhttps://packagist.phpcomposer.com
常见镜像地址:
https://packagist.org 国外的
http://packagist.phpcomposer.com 中国全量镜像
三、安装依赖
1.通过composer.json 安装
创建 composer.json 文件,填写相应的包名和版本号,例如
{
"require": {
"monolog/monolog": "1.0.*"
}
}
然后 执行composer install,安装包就会安装在当前目录下了
2.命令安装
composer require monolog/monolog
指定版本:
composer require "monolog/monolog:1.2.*"
3.全局更新
composer install
composer update
注意:使用composer install或者composer update命令将会更新所有的扩展包,项目中使用需谨慎!!!
4.指定更新
命令行更新
composer update monolog/monolog
手动更新composer.json
composer dump-autoload
5.删除包
composer remove monolog/monolog
四、碰到的一些问题
1.执行composer install遇到错误:Your requirements could not be resolved to an installable set of packages. 这是因为不匹配composer.json要求的版本。
检查并更新composer.json
2.如果PHP 7版本太高,不符合composer.json需要的版本,但是在PHP 7下应该也是可以运行的,composer可以设置忽略版本匹配
composerinstall --ignore-platform-reqs
or
composerupdate --ignore-platform-reqs
- 发出如下警告
Cannot create cache directory /home/fly/.composer/cache/repo/https---repo.packagist.org/, or directory is not writable. Proceeding without cache
Cannot create cache directory /home/fly/.composer/cache/files/, or directory is not writable. Proceeding without cache
则修改 /home/fly/.composer/cache/files/ 文件夹权限
sudo chmod -R 777 /home/fly/.composer/cache/files/