Cloud9でcomposerを使ってLaravel生成、Cannot allocate memory対処

侍エンジニア塾でPHP Laravelアプリの学習をしています。

塾の中ではAWS Cloud9に開発環境を作ってもらって、その中で学習を進めているのですが自分でも開発環境を作れるようにしておかないといけないな、と思った次第です。

Cloud9の環境

Cloud9はPHPやMySQLやcomposerが既にインストールされている。

composer create-projectでCannot allocate memory

以下のコマンドでLaravelプロジェクトの生成を行おうとしたら

composer create-project laravel/laravel laravel-test --prefer-dist

途中でエラーが出てしまって。laravel-testディレクトリは生成されたけど、完了してなさそう。

% composer create-project laravel/laravel laravel-test --prefer-dist
Installing laravel/laravel (v5.4.30)
  - Installing laravel/laravel (v5.4.30): Loading from cache
Created project in laravel-test
> php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 61 installs, 0 updates, 0 removals
  - Installing doctrine/inflector (v1.1.0): Loading from cache
    proc_open(): fork failed - Cannot allocate memory
    The archive may contain identical file names with different capitalization (which fails on case insensitive filesystems)
    Unzip with unzip command failed, falling back to ZipArchive class
The following exception is caused by a lack of memory or swap, or not having swap configured
Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details

PHP Warning:  proc_open(): fork failed - Cannot allocate memory in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 952

Warning: proc_open(): fork failed - Cannot allocate memory in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 952


  [ErrorException]
  proc_open(): fork failed - Cannot allocate memory

create-project [-s|--stability STABILITY] [--prefer-source] [--prefer-dist] [--repository REPOSITORY] [--repository-url REPOSITORY-URL] [--dev] [--no-dev] [--no-custom-installers] [--no-scripts] [--no-progress] [--no-secure-http] [--keep-vcs] [--remove-vcs] [--no-install] [--ignore-platform-reqs] [--] [] [] []
[/code]


もちろんLaravelのインストールにも失敗

% composer global require "laravel/installer=~1.1"
Changed current directory to /home/ec2-user/.composer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 13 installs, 0 updates, 0 removals
  - Installing symfony/process (v3.4.20): Downloading (100%)
    proc_open(): fork failed - Cannot allocate memory
    The archive may contain identical file names with different capitalization (which fails on case insensitive filesystems)
    Unzip with unzip command failed, falling back to ZipArchive class
The following exception is caused by a lack of memory or swap, or not having swap configured
Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details

The following exception is caused by a lack of memory or swap, or not having swap configured
Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details

PHP Warning:  proc_open(): fork failed - Cannot allocate memory in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 952

Warning: proc_open(): fork failed - Cannot allocate memory in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 952

  [ErrorException]
  proc_open(): fork failed - Cannot allocate memory

require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--update-with-all-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] []...

[/code]

Amazon Cloud9のプランはt2.microというのを使っていて、これがメモリ1GBしかないからメモリ領域を確保できずにエラーになっているのだと思います。

これを解決するためにスワップ領域というのを作ってあげてうまくいった。



Cloud9でスワップファイルを作る

sudoをつけないと該当ディレクトにPermission deniedになってしまったので、sudoを付けてコマンドを入力。
$sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
$sudo /sbin/mkswap /var/swap.1
$sudo /sbin/swapon /var/swap.1

そのうえでもう1度

composer create-project laravel/laravel laravel-test --prefer-dist

これでプロジェクトを生成できた。