194

I just can't solve this one.

I'm on Linode 1G RAM basic plan. Trying to install a package via Composer and it's not letting me. My memory limit is set to "-1" on PHP.ini

Is there anything else I can do to get this installed?

Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing thujohn/rss (dev-master df80a7d)
    Downloading: 100%         
PHP Fatal error:  Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php:975
Stack trace:
#0 [internal function]: Composer\Util\ErrorHandler::handle(2, 'proc_open(): fo...', 'phar:///usr/loc...', 975, Array)
#1 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(975): proc_open('stty -a | grep ...', Array, NULL, NULL, NULL, Array)
#2 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(853): Symfony\Component\Console\Application->getSttyColumns()
#3 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(818): Symfony\Component\Console\Application->getTerminalDimensions()
#4 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(752): Symfony\Component\Console\Application->getTerminalWidth()
#5 phar:///usr/local/bin/com in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php on line 975

Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php:975
Stack trace:
#0 [internal function]: Composer\Util\ErrorHandler::handle(2, 'proc_open(): fo...', 'phar:///usr/loc...', 975, Array)
#1 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(975): proc_open('stty -a | grep ...', Array, NULL, NULL, NULL, Array)
#2 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(853): Symfony\Component\Console\Application->getSttyColumns()
#3 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(818): Symfony\Component\Console\Application->getTerminalDimensions()
#4 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(752): Symfony\Component\Console\Application->getTerminalWidth()
#5 phar:///usr/local/bin/com in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php on line 975

28 Answers 28

449

Looks like you runs out of swap memory, try this

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

as mentioned by @BlackBurn027 on comments below, this solution was described in here

16
  • 32
    You sir, are a genius - its not a php issue and removing files will no help. I have to follow your instructions (with sudo) on my VPS. This is the only one useful answer here for VPS owners.
    – Croll
    Dec 29, 2015 at 10:27
  • 5
    getcomposer.org/doc/articles/… as mentioned by the source Nov 23, 2016 at 11:44
  • 1
    This worked for me but I'm confused what those commands are actually doing, can someone explain them? Feb 27, 2017 at 15:24
  • 4
    sometimes 1024 is not enough... Use 2048 instead
    – psylosss
    Jan 8, 2018 at 14:36
  • 2
    I will be forever in your dept, If you ever came to Iran let me know, I will invite you to a lunch (btw don't come to iran it's dangerous) Feb 19, 2020 at 14:49
127

As composer troubleshooting guide here This could be happening because the VPS runs out of memory and has no Swap space enabled.

free -m

To enable the swap you can use for example:

sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo /sbin/mkswap /var/swap.1
sudo /sbin/swapon /var/swap.1

Or if above not worked then you can try create a swap file

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
6
  • thanks alot without upgrading php5 to php7 this worked
    – Shadab K
    May 7, 2018 at 6:13
  • It gave the error again but running composer update after that worked fine. Dec 2, 2019 at 14:16
  • And you can check the [official document][2] getcomposer.org/doc/articles/… Jul 10, 2020 at 5:19
  • Thanks a lot. This works for me. (Digital Ocean $10 Server)
    – WHY
    Sep 29, 2020 at 3:13
  • Setting the swap is the best solution. If you just up the memory with php -dmemory_limit then you will still run out of memory if your machine doesn't have enough, there is no way around that apart from enabling swap.
    – Oliver P
    Oct 13, 2020 at 13:52
113

A bit old but just in case someone new is looking for a solution, updating your PHP version can fix the issue.

Also you should be committing your composer.lock file and doing a composer install on a production environment which is less resource intensive.

More details here: https://github.com/composer/composer/issues/1898#issuecomment-23453850

6
  • 14
    BRILLIANT! That worked. I didn't upgrade my PHP, but committing composer.lock file and updating installed all things properly. Thank you.
    – ericbae
    Oct 5, 2013 at 6:02
  • 7
    Late to the party, but I simply switched off Apache and MySQL. There is a reason I am using a 512MB RAM VPS, don't want to spend monies.
    – Kumar
    Mar 20, 2017 at 14:13
  • 14
    Updating PHP is not a solution. I have 7.0.21, and still get this error.
    – cj5
    Aug 14, 2017 at 17:30
  • @Kumar's solution worked for me. Super easy and like most things, obvious in hindsight :) Feb 4, 2020 at 18:53
  • Thanks a lot. Saved me a bunch of time.
    – JGCW
    May 8, 2020 at 13:33
40

I have faced the same issue. I am on a AWS Free Microinstance which has less memory. I always try one of the below options and it always works (Before all this please check if you have the latest version of composer installed)

sudo php -dmemory_limit=750M composer.phar update

or remove the contents of the vendor folder and try composer update.

sudo rm -rf vendor
sudo php -dmemory_limit=750M composer.phar update --no-scripts --prefer-dist
sudo php artisan --dump-autoload

The second option tries to update all the components, if there is no update, it picks up the package from the cache else picks up from the dist

Note: Please change the memory limit as per your choice.

or

Create a swap partition and try. Swap partition is the portion of the hard drive that linux uses as virtual memory when it runs out of physical memory. It's similar to the windows swap file only instead of using an actual file, linux uses a partition on the hard drive instead.

Hope this helps

5
  • 1
    I dropped the memory limit down to 500M and composer installed what I needed.
    – devNoise
    Dec 9, 2013 at 19:47
  • you can also use an actual swap file instead of a partition. see cyberciti.biz/faq/linux-add-a-swap-file-howto
    – Reese
    Dec 17, 2013 at 18:56
  • 2
    Had same problem on my Digital Ocean account even at 250M I also had to stop Apache and MySQL first before I could run it Jun 14, 2014 at 19:42
  • tristanbailey your solution worked, thanks! Before I stopped mysql and apache I couldn't update, even by setting the php memory limit. Dec 6, 2015 at 1:03
  • When I run sudo php -dmemory_limit=750M composer.phar update I got this error Could not open input file: composer.phar please help Apr 7, 2020 at 6:54
16

Easy, type this commands:

rm -rf vendor/

rm -rf composer.lock

php composer install --prefer-dist

Should work for low memory machines

5
  • Worked for me. I have written a comprehensive answer over here - stackoverflow.com/questions/26850332/…
    – halkujabra
    Nov 10, 2014 at 18:48
  • 1
    It's not working. And this deleted all my vendor folder. It's wrong! Nov 15, 2016 at 13:03
  • What is the problem delete the vendor? just run install/update again...
    – insign
    Nov 16, 2016 at 18:06
  • 1
    Deleting vendor isn't entirely wrong, though it does break the site and keeps it in the broken state if the install still doesn't work. However, deleting the lock file on production machines is not advised. You should be committing your lock file and only installing, not updating. In fact, by deleting the lock file, you're forcing Composer to install from the json file, which is where the memory-intensive processes come from (determining which version of what to install).
    – Shauna
    Aug 11, 2017 at 16:44
  • It's ok to follow this if you are not in a production environment, since it will break the project until composer is done with the installation
    – Bizarro
    Dec 4, 2019 at 19:18
13

Here are the steps to fix the problem: (instant fast SWAP file allocation method used)

Server SWAP Setup (Ubuntu 16.04 SWAP to Fix Out of Memory Errors)

Check if you have swap already, memory and disk size:

    sudo swapon -s
    free -m
    df -h

Make swap file: (change 1G to 4G if you want 4GB SWAP memory)

    sudo fallocate -l 1G /swapfile 

Check swap file:

    ls -lh /swapfile

Assign Swap File:

    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
    sudo swapon /swapfile

Check if swap OK, memory and disk size:

    sudo swapon -s
    free -m
    df -h

Attach Swap File on System Restart:

    sudo nano /etc/fstab
        /swapfile   none    swap    sw    0   0

Adjust Swap File Settings:

    cat /proc/sys/vm/swappiness
    cat /proc/sys/vm/vfs_cache_pressure

    sudo sysctl vm.swappiness=10
    sudo sysctl vm.vfs_cache_pressure=50

    sudo nano /etc/sysctl.conf

SWAP File Priority: (0-100% => 0: Don't put to swap, 100: Put on SWAP and free the RAM)

        vm.swappiness=10

Remove inode from cache: (100: system removes inode information from the cache too quickly)

        vm.vfs_cache_pressure = 50
1
  • I don't know what I have to do on the last step sudo nano /etc/sysctl.conf, So, I skip it then restart Nginx and then do composer install (previously I've removed the vendor directory) and it's working. Thanks!
    – arhakim
    Oct 24, 2019 at 12:07
10

I had a same issue on vagrant. I fixed it by allcate more memory.

 config.vm.provider :virtualbox do |vb|
      vb.customize ["modifyvm", :id, "--memory", "1024"]
 end
1
  • 1
    Solved this issue for me too. Note that in vagrant 2.x instead of vb.customize ... you can do vb.memory = 1024.
    – acobster
    Oct 21, 2016 at 17:55
7

Here is the workaround that I found that works for me every time:

df -h 
dd if=/dev/zero of=/swapfile bs=1M count=1024
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
mkswap /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo 'echo "/swapfile  none  swap  defaults  0  0" >> /etc/fstab' | sudo sh

free -m

confirm u see your swap there:
total used free shared buffers cached
Mem: 494 335 158 0 19 62
-/+ buffers/cache: 254 240
Swap: 1023 3 1020

watch free -m
4
  • 6
    Wowwow, posting the commands without explaining what they do is probably dangerous! Although I can definitively say it would work on most systems. Basically you are creating allocating Swap space so that the HD can be used as RAM. Here is a comprehensive guide on how to do that explaining each command: digitalocean.com/community/tutorials/… Jul 7, 2015 at 16:50
  • Thank you for the clarification of the commands, it is nice to know what the script actually do before you execute it. But in the same way it is not nice how you are promoting digital ocean and your tutorial/blog post.
    – Goran
    Oct 17, 2015 at 15:11
  • The tutorial is great, showing step by step with explanations!
    – Tarik
    Aug 11, 2017 at 14:18
  • Thank you! You instructions help me to install Drupal Presto for 1GB RAM server. I have change 1024 to 2048.
    – Nikit
    Sep 2, 2019 at 0:49
7

Try that:

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024

/sbin/mkswap /var/swap.1

/sbin/swapon /var/swap.1
1
  • 2
    Please add some explanation to your answer such that others can learn from it - especially: what have you changed comparing to the other answers that use the same approach? Is there any need to duplicate their answer?
    – Nico Haase
    Jan 13, 2020 at 8:30
7

I tried by just deleting the vendor folder and composer.lock file and then i run the command composer clear-cache and then composer install. So it working without any error.

0
5

you can use the following to check your free (swap) memory

free -m

total used free shared buffers cached

Mem: 2048 357 1690 0 0 237
-/+ buffers/cache: 119 1928
Swap: 0 0 0

To enable the swap you can use for example:

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1
1
  • I used free -m and noticed that I had no memory, not even in swap .. then did sudo reboot and it worked Dec 8, 2019 at 0:26
4

This seems to be a recurring issue with 1GB and smaller server instances. Apart from trying to shutdown processes and tweak swap settings, you could install on a local machine and upload.

4
  • 1
    yep. Funny thing is if I delete the whole project, do a fresh git pull and then do composer install, it seems to work. Strange.
    – ericbae
    Aug 8, 2013 at 1:57
  • Frustrating, I am deploying a few L4 projects to 1GB Linode instances and am now worried about hitting the memory ceiling.
    – Makita
    Aug 8, 2013 at 2:01
  • An old link but may be of some use: github.com/composer/composer/issues/1104
    – Makita
    Aug 8, 2013 at 2:07
  • 2
    The accepted answer is still the best way to go. You should commit composer.lock and then run an install instead of update on the production server.
    – Makita
    Jan 28, 2014 at 3:08
3

Sometimes by self-updating composer it solves the problem

php composer.phar self-update

Cheers

2
  • This should be the first thing to try to fix the issue. I had problem with 1.3.3, self-update to 1.4.0 fixed the issue. Tip: you could update your answer to cover also cover the use-case of composer self-update, if someone not-so-familiar with composer is to read this reply.
    – Niklaus
    Mar 10, 2017 at 10:05
  • get permission denied when rename occurs
    – Andrew
    Aug 24, 2018 at 18:40
3

I increased the PHP memory_limit from the default 128M to 512M and restart the server. That solved the problem.

3
  • 3
    reboot shouldn't be needed as PHP doesn't runs as a service.
    – HelpNeeder
    Apr 27, 2017 at 23:22
  • 1
    No but Apache does and thus it should be restarted in order to take effect...
    – Dediqated
    Jun 23, 2020 at 10:04
  • 1
    it is needed, as apache loads the php modules
    – Aris
    Jun 23, 2020 at 10:50
2

I get into this situation most of the times so normally i used to follow the step of setting the swap memory.

But now i found a simple alternate trick which worked for me.

Run composer update --no-dev Other than composer update

2

I solved the same problem in Vagrant. I increased the value of memory_limit and delete composer cache: sudo rm -R ~/.composer and finally vagrant reload.

2

Try this:

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

This work for me on Centos 6

1
  • this worked for me on AWS Opsworks EC2 Instance Ubuntu 14.04! Thanks! Nov 9, 2016 at 23:35
1

Make a swap and run composer command again. I hope it'll work for you.

 $ dd if=/dev/zero of=/swapfile bs=1024 count=512k
 $ mkswap /swapfile
 $ swapon /swapfile
 $ echo "/swapfile       none    swap    sw      0       0 " >> /etc/fstab
 $ echo 0 > /proc/sys/vm/swappiness
 $ chown root:root /swapfile
 $ chmod 0600 /swapfile
1

In my case I tried everything that was listed above. I was using Laravel and Vagrant with 4GB of memory and a swap, with memory limit set to -1. I deleted the vendor/ and tried other PHP-versions. Finally, I managed it to work by running

vagrant halt
vagrant up

And then composer install worked again as usual.

0

have same problem with php composer.phar update on my 512mb hosting.

solved with php composer.phar install

0

I had the same problem using composer in wsl2.

Microsoft WSL team introduced a file called .wslconfig for tweaking WSL2 settings.

You basically have to create that file at %UserProfile%.wslconfig and set the settings below.

[wsl2]
memory=6GB  # Any size you feel like
swap=30GB
localhostForwarding=true

Restart your computer, and from now on, you won't have any problem with high memory consumption.

Hopefully, it helps!

0

In my case, when Laravel ver 7,8 and composer ver 1, this error came out. I updated the version of Composer to 2 and it was resolved.

composer self-update

Or

composer self-update --2

Back to previous version:

composer self-update --rollback
-1

I had a similar issue on the cheapest server (512MB RAM) hosted with DigitalOcean, and I was also running Jenkins CI on the same server. After I stopped the Jenkins instance the composer install command worked (well, to a point, it failed with the mcrypt extension missing besides already being installed!).

Maybe if you have another app running on the server, maybe its worth trying to stop it and re-running the command.

-1

edit php.ini file and increase memory_limit value.

memory_limit=1G

will solve this issue.

-1
composer update

Loading composer repositories with package information
Updating dependencies (including require-dev)

> mmap() failed: [12] Cannot allocate memory

Update memory on Server and require '4G' Change 4GB Ram [try to change server type or add more ram]

2 Files We need to edit


on command

# cd /var/www/html
# nano .htaccess

and edit "memory_limit 756M” to 4G


Php ini on php 7.0

# cd ~
# php –-ini
# sudo nano /etc/php-7.0.ini

memory_limit = 128M to 4G

#AWS #AMAZONLINUX #MAGENTO2 #PHP7.0

1
  • 2
    Please add some explanation to your answer. Changing anything in .htaccess does not affect composer after all, as this is not run through a webserver
    – Nico Haase
    Jan 13, 2020 at 8:30
-1

I use it for 500MB RAM and it works for me.

php -d memory_limit=-1 /usr/local/bin/composer install
-1

This worked for me:

According to composer's documentation, Composer 1 used much more memory and upgrading to the latest version will give you much better and faster results.

https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors

3
  • Already suggested years ago. stackoverflow.com/a/32637848
    – miken32
    Nov 12, 2021 at 23:29
  • @miken32 sorry, I can't find this answer on this question and that's why I posted it to help anyone encountering same issue Nov 15, 2021 at 11:33
  • also @miken32 the command here stackoverflow.com/a/32637848 doesn't update to composer 2 as suggested in the composer article link posted above Dec 6, 2021 at 9:44
-2

Please disable js bundling and increase memory. That should fix it. I fixed mine by disabling js bundling.

Thanks

1
  • 1
    Please share more details on your answer - why should disabling "js bundling" help when an error occurs on downloading a package (which happens way before any scripts are run)
    – Nico Haase
    Jan 13, 2020 at 8:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.