composer require dies with 'Allowed memory size exhausted'
PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes)Short answer
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. Upgrade to Composer 2, run the command with COMPOSER_MEMORY_LIMIT=-1, and on a small VPS add swap space.
Fix it fast
1Find out which cause you have
Read the number in the error and check which Composer you run. 1610612736 bytes is exactly 1.5G, the limit Composer sets for itself, so raising php.ini alone changes nothing.
composer --version php -r "echo ini_get('memory_limit').PHP_EOL;" free -m2Match what you see, then jump to its fix
- 1
composer --version prints 1.xAn old Composer 1 is installedFix - 2
Allowed memory size of 1610612736 bytes exhaustedThe job needs more than the 1.5G Composer allows itselfFix - 3
proc_open(): fork failed - Cannot allocate memoryThe server has no free RAM and no swapFix - 4
The same memory error on a cPanel account, even though the plan has plenty of RAMcPanel shell fork-bomb protection is activeFix
- 1
What does the error look like?
PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes)PHP Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory'
Cause 1 of 4
An old Composer 1 is installed
1Check
Composer 1 used far more memory than Composer 2 for the same dependency tree. The official troubleshooting page asks for Composer 2, and 2.2.0 or newer if possible.
composer --version2Fix
Update Composer, then repeat the command.
composer self-update3Verify
The version line starts with 2 and the command now finishes.
composer --version
composer require vendor/packageConfirmed by getcomposer.org
Cause 2 of 4
The job needs more than the 1.5G Composer allows itself
1Check
Composer raises memory_limit to 1.5G internally. A big dependency tree (a framework plus many packages) can still need more.
php -r "echo ini_get('memory_limit').PHP_EOL;"2Fix
Lift the limit for this one run with the environment variable. The php -d form does the same. Both affect Composer itself, not scripts it starts as child processes.
COMPOSER_MEMORY_LIMIT=-1 composer require vendor/package
# or
php -d memory_limit=-1 $(which composer) require vendor/package3Verify
The run finishes and composer.lock changes.
git diff --stat composer.json composer.lockConfirmed by getcomposer.org
Cause 3 of 4
The server has no free RAM and no swap
1Check
The official page links this error to a VPS that runs out of memory and has no swap space.
free -m
swapon --show2Fix
Create and enable a swap file, using the commands from the Composer troubleshooting page. 1024 is the size in MB; pick what your disk allows.
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo /sbin/mkswap /var/swap.1
sudo /bin/chmod 0600 /var/swap.1
sudo /sbin/swapon /var/swap.13Verify
swapon lists the file, then run the Composer command again.
swapon --showConfirmed by getcomposer.org
Cause 4 of 4
cPanel shell fork-bomb protection is active
1Check
The Composer docs name cPanel's shell fork bomb protection as another trigger for this error.
composer --version2Fix
Ask your host to relax that protection for your account, or run Composer on your own machine or in CI and upload the vendor folder.
# on a machine you control
composer install3Verify
The install completes there, and the app starts on the host with the uploaded vendor folder.
php -r "require 'vendor/autoload.php'; echo 'ok';"Confirmed by getcomposer.org
How do I stop it coming back?
- Keep Composer current with composer self-update.
- Commit composer.lock and deploy with composer install, which does not solve versions again.
- Build vendor on a CI runner or your laptop, not on a small production server.
- Add a swap file to every small VPS that runs Composer.
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
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted
The script used more memory than memory_limit allows.
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