PHP Composer update "cannot allocate memory" error (using Laravel 4)
Solution 1
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 2
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 3
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 4
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
Easy, type this commands:
rm -rf vendor/
rm -rf composer.lock
php composer install --prefer-dist
Should work for low memory machines
Related videos on Youtube
Comments
-
ericbae almost 2 years
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
-
ericbae over 10 yearsyep. Funny thing is if I delete the whole project, do a fresh git pull and then do composer install, it seems to work. Strange.
-
Makita over 10 yearsFrustrating, I am deploying a few L4 projects to 1GB Linode instances and am now worried about hitting the memory ceiling.
-
Makita over 10 yearsAn old link but may be of some use: github.com/composer/composer/issues/1104
-
ericbae over 10 yearsBRILLIANT! That worked. I didn't upgrade my PHP, but committing composer.lock file and updating installed all things properly. Thank you.
-
devNoise over 10 yearsI dropped the memory limit down to 500M and composer installed what I needed.
-
Reese over 10 yearsyou can also use an actual swap file instead of a partition. see cyberciti.biz/faq/linux-add-a-swap-file-howto
-
Makita over 10 yearsThe 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.
-
tristanbailey almost 10 yearsHad same problem on my Digital Ocean account even at 250M I also had to stop Apache and MySQL first before I could run it
-
halkujabra over 9 yearsWorked for me. I have written a comprehensive answer over here - stackoverflow.com/questions/26850332/…
-
adelriosantiago almost 9 yearsWowwow, 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/…
-
Goran over 8 yearsThank 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.
-
Elias Kouskoumvekakis over 8 yearstristanbailey your solution worked, thanks! Before I stopped mysql and apache I couldn't update, even by setting the php memory limit.
-
Croll over 8 yearsYou 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.
-
Jed over 7 yearsThis worked for me too and I didn't have to update my PHP. Thanks!
-
Piusha over 7 yearsThis worked for me. With this solution I increased memory_limit from the default 128M to 512M
-
acobster over 7 yearsSolved this issue for me too. Note that in vagrant 2.x instead of
vb.customize ...
you can dovb.memory = 1024
. -
Paul Preibisch over 7 yearsthis worked for me on AWS Opsworks EC2 Instance Ubuntu 14.04! Thanks!
-
Vladimir Kovalchuk over 7 yearsIt's not working. And this deleted all my vendor folder. It's wrong!
-
insign over 7 yearsWhat is the problem delete the vendor? just run install/update again...
-
BlackBurn027 over 7 yearsgetcomposer.org/doc/articles/… as mentioned by the source
-
Anunay over 7 yearsthat works for me without reinstalling the PHP. Thanks.
-
Joseph Astrahan about 7 yearsThis worked for me but I'm confused what those commands are actually doing, can someone explain them?
-
Niklaus about 7 yearsThis 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. -
Kumar about 7 yearsLate 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.
-
HelpNeeder about 7 yearsreboot shouldn't be needed as PHP doesn't runs as a service.
-
Tarik over 6 yearsThe tutorial is great, showing step by step with explanations!
-
Shauna over 6 yearsDeleting 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).
-
cj5 over 6 yearsUpdating PHP is not a solution. I have 7.0.21, and still get this error.
-
psylosss over 6 yearssometimes 1024 is not enough... Use 2048 instead
-
Himanshu Upadhyay over 6 yearsI had the same issue with my AWS EC2 with Ubuntu 16.04 with PHP 7.0.1 and your answer solved my problem. Kudos!!!
-
Shadab K almost 6 yearsthanks alot without upgrading php5 to php7 this worked
-
coder almost 6 yearsThis happened to me on ec2 as well. T2 micro, AWS-Linux AMI
-
Andrew over 5 yearsget permission denied when rename occurs
-
preyz over 5 yearsI received a memory error while running “composer update” on as Raspberry Pi 3b+ and solved this by creating a swap like described above. Thanks a lot for this :-)
-
Jaber Al Nahian about 5 yearsBut this solution goes away if I reboot Ubuntu 18. I am on AWS EC2 T3 Mini
-
LuizEduardoMPF about 5 yearsI'd already tried to switch off Apache and Mysql, but it didn't work. With this suggestion, it worked great!!! Thank you so much, it saved me.
-
Mihail Minkov about 5 yearsExcellent! From what I can see it basically creates/resizes the swap file right?
-
Mohammed Omer almost 5 years@JaberAlNahian you can make a permanent swap please check here digitalocean.com/community/tutorials/…
-
Nikit over 4 yearsThank you! You instructions help me to install Drupal Presto for 1GB RAM server. I have change 1024 to 2048.
-
arhakim over 4 yearsI 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 docomposer install
(previously I've removed the vendor directory) and it's working. Thanks! -
Amir Hajiha over 4 yearsIt gave the error again but running composer update after that worked fine.
-
Bizarro over 4 yearsIt'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
-
Eleazar Resendez over 4 yearsI used
free -m
and noticed that I had no memory, not even in swap .. then didsudo reboot
and it worked -
Nico Haase over 4 yearsPlease 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 over 4 yearsPlease add some explanation to your answer. Changing anything in
.htaccess
does not affectcomposer
after all, as this is not run through a webserver -
Nico Haase over 4 yearsPlease 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?
-
James Bridgewater about 4 years@Kumar's solution worked for me. Super easy and like most things, obvious in hindsight :)
-
Hossein Shahdoost about 4 yearsI 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)
-
Varun Naharia about 4 yearsWhen I run
sudo php -dmemory_limit=750M composer.phar update
I got this errorCould not open input file: composer.phar
please help -
JGCW almost 4 yearsThanks a lot. Saved me a bunch of time.
-
Dediqated almost 4 yearsNo but Apache does and thus it should be restarted in order to take effect...
-
Aris almost 4 yearsit is needed, as apache loads the php modules
-
Thilina Dharmasena almost 4 yearsAnd you can check the [official document][2] getcomposer.org/doc/articles/…
-
Yohanim almost 4 yearsthis isn't php problem. you're really genius.
-
temirbek over 3 yearshow about docker container? should I do above as usual or should I first go to container bash?
-
WHY over 3 yearsThanks a lot. This works for me. (Digital Ocean $10 Server)
-
Oliver P over 3 yearsSetting 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. -
Sašo Kovačič over 2 yearsAs I know swap files are not good for SSD disks. It is better to increase memory_limit with the update command: run php -dmemory_limit=750M composer update --no-scripts --prefer-dist
-
miken32 over 2 yearsAlready suggested years ago. stackoverflow.com/a/32637848
-
Evidoski over 2 years@miken32 sorry, I can't find this answer on this question and that's why I posted it to help anyone encountering same issue
-
Evidoski over 2 yearsalso @miken32 the command here stackoverflow.com/a/32637848 doesn't update to composer 2 as suggested in the composer article link posted above