昨天部署 Laravel 项目一直卡在 composer Updating dependencies (including require-dev)

解决方案是切换国内阿里云的 Composer 全量镜像, 官网地址:https://mirrors.aliyun.com/composer/

a). 配置只在当前项目生效

composer config repo.packagist composer https://mirrors.aliyun.com/composer/

# 取消当前项目配置
composer config --unset repos.packagist

a). 配置全局生效

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

# 取消全局配置
composer config -g --unset repos.packagist

全局生效和当前项目生效选一种就好了。

接下来我直接遇到 php 内存不足的错误

PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 32 bytes) in phar:///usr/local/bin/composer/src/Composer/DependencyResolver/Rule.php on line 62

使用命令查看 $ php -i |grep memory 发现 php.ini 配置的内存只有 124M,这也太惨了吧,先给它配 1G(1024M)试试吧

memory_limit => 124M => 124M

执行命令 $ php -i | grep php.ini 找到 php.ini 位置

Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini

用 vim 打开文件修改配置,$ sudo vim /etc/php5/cli/php.ini 按: 进入命令行模式 输入 /memory_limit ,找到 memory_limit 修改配置为 memory_limit = 1024M

最后重新 $ composer install 就好了

解决这个之后我他喵又遇到 PHP Composer install “cannot allocate memory” error (using Laravel) 问题

这个说起来就有点坑了,我遇到之后删掉了 composer.lock 文件准备重新 composer install 发现还严重一些,最好是不要删 composer.lock 文件,删掉之后重新 install 需要的内存会大很多。因为我是小 Ubuntu 服务器可能带不动全新的 composer install

Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing thujohn/rss (dev-master df80a7d)
    Downloading: 100%         
PHP Fatal error:  Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php:975
Stack trace:
#0 [internal function]: Composer\Util\ErrorHandler::handle(2, 'proc_open(): fo...', 'phar:///usr/loc...', 975, Array)
#1 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(975): proc_open('stty -a | grep ...', Array, NULL, NULL, NULL, Array)
#2 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(853): Symfony\Component\Console\Application->getSttyColumns()
#3 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(818): Symfony\Component\Console\Application->getTerminalDimensions()
#4 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(752): Symfony\Component\Console\Application->getTerminalWidth()
#5 phar:///usr/local/bin/com in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php on line 975

Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php:975
Stack trace:
#0 [internal function]: Composer\Util\ErrorHandler::handle(2, 'proc_open(): fo...', 'phar:///usr/loc...', 975, Array)
#1 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(975): proc_open('stty -a | grep ...', Array, NULL, NULL, NULL, Array)
#2 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(853): Symfony\Component\Console\Application->getSttyColumns()
#3 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(818): Symfony\Component\Console\Application->getTerminalDimensions()
#4 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(752): Symfony\Component\Console\Application->getTerminalWidth()
#5 phar:///usr/local/bin/com in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php on line 975

解决方案是分配多一点内存,Ubuntu 解决方案如下,其他 Linux 也大致差不多吧

sudo su
# 切换到 root 用户

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

执行之后重新 composer install 应该就没问题了。