Skip to content

Composer memory limit workaround

Some servers will have insufficient memory to support composer requirement resolution. In that case you'll see errors like so:

$ composer require drupal/whatever 

mmap() failed: [12] Cannot allocate memory

mmap() failed: [12] Cannot allocate memory
PHP Fatal error:  Out of memory (allocated 1220550656) (tried to allocate 12288 bytes) in /usr/share/php/Composer/DependencyResolver/GenericRule.php on line 36

Fatal error: Out of memory (allocated 1220550656) (tried to allocate 12288 bytes) in /usr/share/php/Composer/DependencyResolver/GenericRule.php on line 36

In such case, it might be possible that we would increase the RAM provisioned to the server, but often the RAM allocation is provided at a fixed price by our hosting infrastructure provider, and increasing RAM is not an easy option.

In these cases, we can use the following workaround to resolve composer dependencies on another machine, such as a local dev environment.

# 1. Create a new empty directory for this work:
mkdir /tmp/mycomposer

# 2. copy composer.json and composer.lock from the live site to this empty directory
cd /tmp/mycomposer
scp user@server:/path/to/drupal/root/composer.* .

# 3. Run composer install to fetch all packages
compsoer install

# 4. Run the relevant composer require/update/etc command 
composer require drupal/whatever

# 5. Observe that the composer command completes properly and without error
# At this point, composer.json and composer.lock have been modified by composer

# NOTE: There's no need to run a full Drupal install, because we are concerned only about composer packages,
# not database updates, etc. Therefore, we don't create a fully working Drupal site on the local environment,
# because it is not needed.

# 6. Copy the local composer.json and composer.lock files back  up to the live site.
scp composer.* user@server:/path/to/drupal/root/.

# 7. Log into the live site and run composer install with proper arguments to update packages per the 
# newly modified composer.json contents.
ssh user@server
cd /path/to/drupal/root
composer install --no-dev

# 8. Observe that composer completes properly and without error.