Troubleshooting Composer Out of Memory Errors

Anyone who has used Composer for more than a hot second has likely run into the dreaded “out of memory” error. There are a few ways to fix this, including a permanent fix. Let me show you how!

The Problem

The error looks something like this:

PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.9.3/bin/composer/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52
Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.9.3/bin/composer/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52
Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.

This just happened to me while running the following command:

$ composer create-project acquia/lightning-project MY_PROJECT

Remember that Composer is using PHP to execute, so in this case the problem is that my computer most likely doesn’t have enough memory to allocate to the PHP process.

Checking PHP Memory Limit

All you have to do to check your PHP Memory Limit is:

1. figure out which php.ini file your computer is currently using (if you followed my guide on setting up a new Macbook and/or are using Homebrew, you likely aren’t using the stock one that shipped with your computer). Run this: $ php --ini

Configuration File (php.ini) Path: /usr/local/etc/php/7.3
Loaded Configuration File:         /usr/local/etc/php/7.3/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.3/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.3/conf.d/ext-opcache.ini

You’re interested in the “loaded” configuration file (so for me, it’s in /usr/local/etc/php/7.3/php.ini).

2. Using your preferred editing method, open up that php.ini file and find the memory_limit setting. This is what I found:

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 128MB

That’s not NEARLY enough!

Upping Your PHP Memory Limit

1. Using the same steps as above, we’ll want to edit the php.ini file.

Update the default memory_limit. I upped mine to 2048MB (2GB)

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 2048MB

Note: you may have to up it even higher or set it to -1 (for unlimited, which is what the Composer docs suggest).

2. Restart your apache server and terminal so the change will take effect.

sudo apachectl restart


3. Run your composer command again.


A Temporary Fix

You can also fix this on a “case by case” basis if you so desire (or your machine is locked down and you can’t change your php.ini file without an administrator’s assistance) by running commands like this:

$ COMPOSER_MEMORY_LIMIT=-1 composer <command>

This will ensure that there is no PHP memory limit imposed on the current command being run. It’s not a long term fix though, so if you don’t up the memory limit permanently you will likely have to do this over and over again!

Related Content