[Fix] Composer "Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory'" error on Amazon EC2

Red Lina

If you're using Amazon EC2 and running the micro instance you probably hit the following error when trying to install packages using Composers:

PHP Fatal error:  Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory'

The reason for this error to happen is because your instance is running out of memory so PHP cannot fork processes that required by Composer. In this article I'll show you how to fix this.

Clear Memory

SSH into your instance and check to see how much memory is available:

$ free -m

Next, run the following commands one by one to flush Memory cache, Buffer cache and Swap space:

$ sudo sync; echo 1 > /proc/sys/vm/drop_caches
$ sudo sync; echo 2 > /proc/sys/vm/drop_caches
$ sudo sync; echo 3 > /proc/sys/vm/drop_caches

Then check the free memory on your instance again to make sure you have fair amount of memory to run Composer:

$ free -m

If there's still not enough memory for Composer to run then go to the next step.

Restart Programs Eating Up Memory

Run the following command to find out which programs are eating up memory:

$ top

After running this command, terminal will show you list of programs and number of memory there're using (in the %MEM column). Also you'll see the process ID of the programs in the first column (the PID column).

Remember the PIDs of three programs on top of the list and hit Ctrl + C or Command + C if you're on Mac to go back to terminal. The use the following command to restart them or stop them if they're not in use any more:

$ kill -9 PID_of_Program

The check the amount of memory free and run the Composer again.

If the error still occurs then you should either consider restarting your instance or upgrading to other type of EC2 instance.

Reply