Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

composer require dies with 'Allowed memory size exhausted'

ErrorPHP 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.

  • Applies to Composer 2.8, PHP 8.3, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 -m
  2. 2Match what you see, then jump to its fix

    1. 1composer --version prints 1.xAn old Composer 1 is installedFix
    2. 2Allowed memory size of 1610612736 bytes exhaustedThe job needs more than the 1.5G Composer allows itselfFix
    3. 3proc_open(): fork failed - Cannot allocate memoryThe server has no free RAM and no swapFix
    4. 4The same memory error on a cPanel account, even though the plan has plenty of RAMcPanel shell fork-bomb protection is activeFix

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 --version

2Fix

Update Composer, then repeat the command.

composer self-update

3Verify

The version line starts with 2 and the command now finishes.

composer --version
composer require vendor/package

Confirmed by getcomposer.org

Did this fix it?

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/package

3Verify

The run finishes and composer.lock changes.

git diff --stat composer.json composer.lock

Confirmed by getcomposer.org

Did this fix it?

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 --show

2Fix

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.1

3Verify

swapon lists the file, then run the Composer command again.

swapon --show

Confirmed by getcomposer.org

Did this fix it?

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 --version

2Fix

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 install

3Verify

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

Did this fix it?

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

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 fix

Back 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.