PHP Composer update "cannot allocate memory" error (using Laravel 4)

PhpLaravelLaravel 4Composer Php

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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

Solution 5 - Php

Easy, type this commands:

rm -rf vendor/

rm -rf composer.lock

php composer install --prefer-dist

Should work for low memory machines

Solution 6 - Php

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

Solution 7 - Php

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

Solution 8 - Php

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

Solution 9 - Php

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.

Solution 10 - Php

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

Solution 11 - Php

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

Solution 12 - Php

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.

Solution 13 - Php

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

Solution 14 - Php

Sometimes by self-updating composer it solves the problem

php composer.phar self-update

Cheers

Solution 15 - Php

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

Solution 16 - Php

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.

Solution 17 - Php

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

Solution 18 - Php

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

Solution 19 - Php

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.

Solution 20 - Php

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

solved with php composer.phar install

Solution 21 - Php

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!

Solution 22 - Php

I had a similar issue on the cheapest server (512MB RAM) hosted with [DigitalOcean][1], 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]: https://www.digitalocean.com/?refcode=a2984a839658 "DigitalOcean"

Solution 23 - Php

edit php.ini file and increase memory_limit value.

> memory_limit=1G

will solve this issue.

Solution 24 - Php

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

Solution 25 - Php

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

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

Solution 26 - Php

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

Solution 27 - Php

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

Thanks

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionericbaeView Question on Stackoverflow
Solution 1 - PhpAditya Kresna PermanaView Answer on Stackoverflow
Solution 2 - PhpUmair HamidView Answer on Stackoverflow
Solution 3 - Phpfl3x7View Answer on Stackoverflow
Solution 4 - PhpAbishekView Answer on Stackoverflow
Solution 5 - PhpinsignView Answer on Stackoverflow
Solution 6 - PhpTarikView Answer on Stackoverflow
Solution 7 - PhpYann叶View Answer on Stackoverflow
Solution 8 - PhpGoranView Answer on Stackoverflow
Solution 9 - PhpManjunath AView Answer on Stackoverflow
Solution 10 - PhpAyo 'LanaView Answer on Stackoverflow
Solution 11 - PhpRishi RanjanView Answer on Stackoverflow
Solution 12 - PhpMakitaView Answer on Stackoverflow
Solution 13 - PhpArisView Answer on Stackoverflow
Solution 14 - PhpTitogeloView Answer on Stackoverflow
Solution 15 - PhpBastin RobinView Answer on Stackoverflow
Solution 16 - PhpoussakaView Answer on Stackoverflow
Solution 17 - PhpAdonias VasquezView Answer on Stackoverflow
Solution 18 - Phpnixon1333View Answer on Stackoverflow
Solution 19 - PhpPHZ.fi-PharazonView Answer on Stackoverflow
Solution 20 - PhpIvan ProskuryakovView Answer on Stackoverflow
Solution 21 - PhpNicolas FaccioloView Answer on Stackoverflow
Solution 22 - PhpChrisView Answer on Stackoverflow
Solution 23 - PhpAmjithView Answer on Stackoverflow
Solution 24 - PhpSUNNETmediaView Answer on Stackoverflow
Solution 25 - PhpToirView Answer on Stackoverflow
Solution 26 - PhpEvidoskiView Answer on Stackoverflow
Solution 27 - Phpmani mannView Answer on Stackoverflow