Composer stops with 'fork failed - Cannot allocate memory' on a small server
proc_open(): fork failed - Cannot allocate memoryShort answer
This error means the operating system refused to start a child process because the server is out of memory, not because PHP's memory_limit is too low. Check free -m. If Swap is 0, add a swap file. Also run Composer 2, which uses far less memory than Composer 1.
Fix it fast
1Find out which cause you have
Look at total memory and the Swap line. A Swap total of 0 on a small VPS is the classic setup for this error.
free -m2Match what you see, then jump to its fix
What does the error look like?
proc_open(): fork failed - Cannot allocate memoryPHP Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory'
Cause 1 of 3
The server has no swap space
1Check
Composer's own troubleshooting guide says this error usually means the VPS ran out of memory and has no swap.
free -m2Fix
Create a swap file and switch it on. These are the commands from the Composer guide (1 GB).
sudo dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo mkswap /var/swap.1
sudo chmod 0600 /var/swap.1
sudo swapon /var/swap.13Verify
Swap now shows a total, and Composer finishes.
free -m
composer updateConfirmed by getcomposer.org
Cause 2 of 3
An old Composer 1 is installed
1Check
Composer 1 used much more memory than Composer 2. The troubleshooting guide asks you to use Composer 2, ideally 2.2.0 or later.
composer --version2Fix
Update Composer to the current 2.x release.
sudo composer self-update --23Verify
The version is 2.x and the same command runs.
composer --versionConfirmed by getcomposer.org
Cause 3 of 3
Shell fork bomb protection on shared hosting (cPanel)
1Check
The Composer guide lists cPanel's Shell Fork Bomb Protection as a trigger for memory-style failures. Look for it in the account's shell settings.
# cPanel / WHM: Security Center > Shell Fork Bomb Protection2Fix
Ask the host to relax the shell limit for your account, or run Composer on your own machine or in CI and upload the vendor folder.
composer install --no-dev --optimize-autoloader3Verify
Run the install again over SSH.
composer installConfirmed by getcomposer.org
How do I stop it coming back?
- Give every VPS with 1 GB of RAM or less a swap file before the first deploy.
- Build vendor in CI or on a build machine and ship it, instead of running Composer on a tiny production server.
- Keep Composer itself up to date.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
composer require dies with 'Allowed memory size exhausted'
PHP Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory'
Composer already lifts PHP's memory_limit to 1.5G on its own, so hitting this error means the job needs more than that, or the machine itself has too little memory.
4 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
CodeIgniter 4 cannot write its cache files
CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/
CodeIgniter 4's file cache needs a cache directory that the web server can really write to.
2 causes, each with a fixBack end
CodeIgniter 3 loads the model but the property is undefined
The model name you are loading is the name of a resource that is already being used: user
CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.
2 causes, each with a fix