If you get error below after running composer commands then you'll have to increase vagrant memory, assign more CPU cores and additionally allocate a bit of swap space just in case as shown below.


Error


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 973

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

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

Current memory status


By default vagrant gives us 512MB RAM and no swap space.


vagrant@symfony:~$ free -m
total used free shared buffers cached
Mem: 489 415 74 0 14 280
-/+ buffers/cache: 121 368
Swap: 0 0 0

Vagrantfile


We're now allocating 2GB RAM, 1GB swap space and 2 CPU cores.


# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
....
....

config.vm.provider :virtualbox do |virtualbox_config|
...
virtualbox_config.memory = 2048
virtualbox_config.cpus = 2
end

config.vm.provision "shell", inline: "/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024", run: "always"
config.vm.provision "shell", inline: "/sbin/mkswap /var/swap.1", run: "always"
config.vm.provision "shell", inline: "/sbin/swapon /var/swap.1", run: "always"
end

New memory status


vagrant@symfony:~$ free -m
total used free shared buffers cached
Mem: 2001 1358 643 5 11 1143
-/+ buffers/cache: 203 1798
Swap: 1023 0 1023