composer

composer 使用简洁

Composer 中文文档

安装 Composer注意,php 需要 5.6 及以上版本才能正常安装 composer)

# 1.1 通过官方下载 composer
➜  ~ curl -sS https://getcomposer.org/installer | php
➜  ~ sudo mv composer.phar /usr/local/bin/composer

# 1.2 或者通过国内镜像下载 composer
➜  ~ sudo wget https://dl.laravel-china.org/composer.phar -O /usr/local/bin/composer

# 2. 给 composer 执行权限
➜  ~ chmod a+x /usr/local/bin/composer
➜  ~ composer -v

# 3.1 全局配置 composer 中国镜像
➜  ~ composer config -g repo.packagist composer https://packagist.phpcomposer.com
# 3.2 全局配置 composer 优帆远扬镜像
➜  ~ composer config -g repo.packagist composer https://packagist.laravel-china.org

# 4. 在项目目录下安装依赖
➜  $ composer install -vvv

# 5. 更新 composer 需要以 root 
➜  ~ sudo su
➜  ~ composer selfupdate

如果你没有添加 GitHub 密钥的话,composer update 会提示你登录:

Could not fetch https://api.github.com/repos/jquery/jquery-dist, please create a GitHub OAuth token to go over the API rate limit
Head to https://github.com/settings/tokens/to retrieve a token.
It will be stored in /home/vagrant/.config/composer/auth.json for future use by Composer.
Token (hidden):
Token stored successfully.

/home/vagrant/.config/composer/auth.json

解决办法就是:进入 https://github.com/settings/tokens 点击 「Generate new token」 新建一个 Token,选择 gist Create gists,然后就会得到一个 Token,然后复制粘贴这个值就 OK 了。

配置文件在 /home/vagrant/.config/composer

composer 执行太慢时,可以在命令末尾添加 -vvv 参数输出详细日志,例如 :

composer install -vvv  
composer update -vvv  
composer -vvv create-project laravel/laravel blog  
composer -vvv require psr/log  

Q:为什么基于 Yii2 创建的项目安装依赖包时即便设置了中国镜像依然很慢?
A:yii2 通过 fxp/composer-asset-plugin 插件来安前端开发所用到依赖包,此插件会在安装完 php 依赖包之后再安装前端开发所依赖的 npm 或 bower 包, 所以导致再次前往 Github 下载资源,从而绕开了我们中国镜像。

composer require foo/bar  
composer require --dev foo/bar

composer require "foo/bar:1.0.0"  
composer require --dev "foo/bar:1.0.0"  

常见问题

Failed to decode response: zlib_decode(): data error

根据提示,关闭 IPv6

为生产环境作准备

# --no-dev 跳过 require-dev 字段中列出的包
composer install --no-dev  
composer update --no-dev 

# 优化一下自动加载
composer dump-autoload --optimize  

如果不小心在生产环境执行了 composer install 把开发依赖也安装了,可以再次执行 composer update --no-dev 就可以移除开发依赖。

使用 composer

安装包

composer require --prefer-dist 包名  

创建项目

Yii2
composer global require "fxp/composer-asset-plugin:^1.3.1"

composer create-project --prefer-dist yiisoft/yii2-app-basic basic  
Laravel
composer create-project laravel/laravel learnlaravel5  

部署应用时,生产环境请使用 --no-dev 选项

composer install --no-dev  
composer update --no-dev  

proc_open(): fork failed - Cannot allocate memory

free -m  

如果 swap 为 0 ,则创建 swap

Failed to decode response: zlib_decode(): data error

方法 1:根据提示,关闭 IPv6

方法 2

composer clear-cache

# 如果全是 ok,就更新下
composer diagnose  
composer selfupdate  

然后重新运行安装命令

方法2.修改 php 配置

在etc/php5目录下有三个文件夹:cgi cli fpm。我们这里修改 cli 目录下的配置文件

  • cli 是命令行运行php命令的配置
  • fpm(FastCGI 进程管理器)
# 查看配置
php -i  

The php.ini used by your command-line PHP is: /etc/php5/cli/php.ini

zlib.output_compression = On  
zlib.output_compression_level = 5  
# 如果全是 ok,就更新下
composer diagnose  
composer selfupdate  

王浩冰

继续阅读此作者的更多文章