Give More Memory to Composer on VPS

When update Drupal with composer on VPS with RAM less than 2Gb you may catch errors:

COMPOSER_MEMORY_LIMIT=-1; php -d memory_limit=-1 /usr/local/bin/composer update "drupal/core"
Loading composer repositories with package information
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
Info from https://repo.packagist.org: #StandWithUkraine
Updating dependencies (including require-dev)

mmap() failed: [12] Cannot allocate memory

mmap() failed: [12] Cannot allocate memory

mmap() failed: [12] Cannot allocate memory
PHP Fatal error:  Out of memory (allocated 1744838656) (tried to allocate 67108864 bytes) in phar:///usr/local/bin/composer/src/Composer/DependencyResolver/Solver.php on line 223

Fatal error: Out of memory (allocated 1744838656) (tried to allocate 67108864 bytes) in phar:///usr/local/bin/composer/src/Composer/DependencyResolver/Solver.php on line 223

 

As you can see I already used some advices like add `COMPOSER_MEMORY_LIMIT=-1` (1) and add `php -d memory_limit=-1` (2) when run composer. But that does not work on cheap VPS. You need swap! Sure, if your hoster allows to use swap.

#make swap for composer use more memory:
#https://www.howtogeek.com/455981/how-to-create-a-swap-file-on-linux/
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
mkswap /swapfile
chmod 600 /swapfile
swapon /swapfile
gedit /etc/fstab
  /swapfile    none    swap    sw    0    0

#when need more memory
sudo dd if=/dev/zero /of=/swapfile2 bs=1024 count=104857
mkswap /swapfile2
chmod 600 /swapfile2
swapon /swapfile2

#so composer needs about 2 Gb RAM
# free -m
              total        used        free      shared  buff/cache   available
Mem:           1837        1622         163           6          51         113
Swap:          1023         483         540
 

I created /swapfile size 1 GB, and composer finished successfully. It used about 2 Gb Ram. When need more, create another /swapfile2. You can add it to /etc/fstab for use automatically after reboot.

 

Links

  1. Allow composer memory with no limit https://www.drupal.org/forum/support/installing-drupal/2020-06-23/compo…
  2. Allow php process use memory with no limit https://www.oakleys.org.uk/blog/2018/03/overcoming_memory_issues_instal…
  3. Create swap https://www.howtogeek.com/455981/how-to-create-a-swap-file-on-linux/
Section
Category