ChatGPT解决这个技术问题 Extra ChatGPT

Composer update memory limit

I need to run composer update at my hosting so I log in with ssh and try to run the following command inside /www folder where I have Laravel and composer installation:

composer update

I get this error:

https://i.stack.imgur.com/FGC01.png

I'm in contact with my hosting provider, they told me to run the command:

php -d memory_limit=512M composer update

I ran this command but I got: "Could not open file: composer"

What to do? What is the solution here?

As @Sven said, in production composer install is enough. In your case, while using a shared hosting, i think you will not get composer update working, so the only way is buy a VPS hosting like Digital Ocean, Linode.

d
diamondsea

Set it to use as much memory as it wants with:

COMPOSER_MEMORY_LIMIT=-1 composer update

How can we set this value or any value globally? instead of having to do this every time via CLI?
@snh_nl you will need to set php memory limit. Example: chapterthree.com/blog/how-fix-composer-memory-issue
Define it globally in your /etc/profile (or whatever shell you use) with COMPOSER_MEMORY_LIMIT=-1; export COMPOSER_MEMORY_LIMIT. Or define it a shell alias? alias memcomposer='COMPOSER_MEMORY_LIMIT=-1 composer', then use memcomposer instead of composer
It still says Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 4096 bytes) in phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52
@Ozan Kurt you might have used up all the memory available to your virtual machine, or to your actual computer. If you're using a VM, try increasing its memory limit.
p
pfnuesel

When you run composer update, the OS will look into the configured paths and try to locate an executable file with that name.

When running php composer update, the composer string is treated as a parameter to PHP, which is not searched in any paths. You have to provide the full path in order to run it.

Running which composer will tell you where the OS finds the composer executable, and then you simply use the full path in the PHP command:

$>which composer
/usr/local/bin/composer

$>php -d memory_limit=512M /usr/local/bin/composer update
...

Note that 512MB might be too few. My perception is that it will happily take 1GB or more, depending on the number of dependencies you use and the variety of versions that you theoretically allow, i.e. if you allow Symfony ~2.3, then you make Composer deal with a lot more possible versions compared to using ~2.7.

Also note that running Composer on the production machine is not the best idea. You would have to have access to Github, maybe provide access credentials, have VCS tools installed, and you will easily break your site if any of the remote hosting servers is offline during your update. It is a better idea to use Composer on a deployment system that does all the preparation, and then moves all the files onto the production server.

Update

It's the year 2020 now, and the way Composer manages its memory has changed quite a bit. The most important thing is that Composer will increase the memory limit by itself if it encounters a limit set too low. This however immediately triggers the problem of running out of memory on machines that have too few memory installed. You can make Composer use less memory by setting the environment variable like COMPOSER_MEMORY_LIMIT=512M, but this will create problems if Composer would need more memory to correctly operate.

My main point remains true: Do not run Composer on machines that have too few memory installed. You potentially need 1.5 GB of free memory to be able to update everything.


I just wite: php -r "readfile('getcomposer.org/installer');" | php and after that everything works fine... also THANS FOR HELP
php -d memory_limit=-1 $(which composer) update is more convenient due to having no need to specify composer path.
This is great. I did not know you could boost memory for a single command. That is what this is doing right?
how in windows?
@Rax one more thing, if you are running this command in windows on mingw then you'll get an error that path must be translated so use this command php -d memory_limit=-1 $(which composer).phar update
R
Roman Grinev

The best solution for me is

COMPOSER_MEMORY_LIMIT=-1 composer require <package-name>

mentioned by @realtebo


D
Diego Favero

I am facing problems with composer because it consumes all the available memory, and then, the process get killed ( actualy, the output message is "Killed")

So, I was looking for a solution to limit composer memory usage.

I tried ( from @Sven answers )

$ php -d memory_limit=512M /usr/local/bin/composer update

But it didn't work because

"Composer internally increases the memory_limit to 1.5G."

-> Thats from composer oficial website.

Then I found a command that works :

$ COMPOSER_MEMORY_LIMIT=512M php composer.phar update

Althought, in my case 512mb is not enough !

Source : https://www.agileana.com/blog/composer-memory-limit-troubleshooting/


I had to use composer like so: COMPOSER_MEMORY_LIMIT=1024M composer require 'module'
Similar, I used COMPOSER_MEMORY_LIMIT=-1 composer require <package name>
E
ESP32

I had to combine COMPOSER_MEMORY_LIMIT and memory_limit in the command line:

On Windows:

set COMPOSER_MEMORY_LIMIT=99999999999&& php -d memory_limit=-1 composer.phar update

On Linux:

export COMPOSER_MEMORY_LIMIT=99999999999 && php -d memory_limit=-1 composer.phar update

This is the only one that's worked for me on Mac OS X El Capitan.
Just doing set COMPOSER_MEMORY_LIMIT=-1 and then my composer command worked for me on Windows.
A
Arun Panneerselvam

If there's enough memory composer would internally consume it and run without any problem. No need to specifically tell the composer to do it.

Have you tried to increase your swap memory, coz it worked for me. I increased the swap memory to 4096Mb (4GB) and now it all looks great to me.

first use "sudo free" to see available memory and swap memory. and configure swap as,

For Debian:

sudo fallocate -l 4G /swapfile
sudo dd if=/dev/zero of=/swapfile bs=4096k count=1048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

to make it permenant add this to /etc/fstab file, /swapfile swap swap defaults 0 0

For CentOS :

[root@myserver]:/# cd /var
[root@myserver]:/var# touch swap.img
[root@myserver]:/var# chmod 600 swap.img
[root@myserver]:/var# mkswap /var/swap.img

[root@myserver]:/var# dd if=/dev/zero of=/var/swap.img bs=4096k count=1000
[root@myserver]:/var# mkswap /var/swap.img 
[root@myserver]:/var# swapon /var/swap.img

you can increase your swap memory by changin bs= 1024k or 2048k or 8096k depending on your physical volume size. use 'swapon' and swapoff commands to see the difference.

check 'swappiness' (60 should do good,)

cat /proc/sys/vm/swappiness

V
Vladimir Salguero

You can change the memory_limit value in your php.ini

Try increasing the limit in your php.ini file

Use -1 for unlimited or define an explicit value like 2G

memory_limit = -1

Note: Composer internally increases the memory_limit to 1.5G.

Read the documentation getcomposer.org


T
Tawfeeq Amro

I made this on Windows 10 and worked with me:

php -d memory_limit=-1 C:/ProgramData/ComposerSetup/bin/composer.phar update

You can change xx Value that you want

 memory_limit=XX

M
MadJack

On MAC OS High Siera I ran the below:

MacBook-Pro:asiu jack$ php --ini

Returned:

Configuration File (php.ini) Path: /usr/local/etc/php/7.4
Loaded Configuration File:         /usr/local/etc/php/7.4/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.4/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.4/conf.d/ext- 
opcache.ini,
/usr/local/etc/php/7.4/conf.d/php-memory-limits.ini

All answers above are setting the loaded config which does update, but notice additional .ini files parsed has php-memory-limits.ini as a separate file. You have to update this file memeory limit as well. same way open in text editor and change to somehting like 2G. The output on memory limit failure should tell you how much memory it needs to run, just set it to higher than that or -1 for unlimited.


S
Snigdha Sambit Aryakumar

This error can occur especially when you are updating large libraries or libraries with a lot of dependencies. Composer can be quite memory hungry.

Be sure that your composer itself is updated to the latest version:

php composer.phar --self-update

You can increase the memory limit for composer temporarily by adding the composer memory limit environment variable:

COMPOSER_MEMORY_LIMIT=128MB php composer.phar update

Use the format “128M” for megabyte or “2G” for gigabyte. You can use the value “-1” to ignore the memory limit completely.

Another way would be to increase the PHP memory limit:

php -d memory_limit=512M composer.phar update ...

v
vinzee
COMPOSER_MEMORY_LIMIT=-1 composer update --ignore-platform-reqs

Thanks for contributing! If you find time please edit an explanation into the answer as it's recommended to explain entirely code-based answers on StackOverflow.
R
Rem

How large is your aws server? If it only has 1gb of ram, setting the memory limit of 2gb in php.ini won't help.

If you can't/don't want to also increase the server side to get more RAM available, you can enable SWAP as well.

See here for how to enable swap. It enables 4gb, although I typically only do 1GB myself.

Source: Got from laracast site


C
Connor Leech

I'm running Laravel 6 with Homestead and also ran into this problem. As suggested here in the other answers you can prefix COMPOSER_MEMORY_LIMIT=-1 to a single command and run the command normally. If you'd like to update your PHP config to always allow unlimited memory follow these steps.

vagrant up
vagrant ssh
php --version # 7.4
php --ini # Shows path to the php.ini file that's loaded
cd /etc/php/7.4/cli # your PHP version. Each PHP version has a folder
sudo vi php.ini

Add memory_limit=-1 to your php.ini file. If you're having trouble using Vim or making edits to the php.ini file check this answer about how to edit the php.ini file with Vim. The file should look something like this:

; Maximum amount of memory a script may consume
; http://php.net/memory-limit
memory_limit = -1

Note that this could eat up infinite amount of memory on your machine. Probably not a good idea for production lol. With Laravel Valet had to follow this article and update the memory value here:

sudo vi /usr/local/etc/php/7.4/conf.d/php-memory-limits.ini

Then restart the server with Valet:

valet restart

This answer was also helpful for changing the config with Laravel Valet on Mac so the changes take effect.


D
Digital Human
<C:\>set COMPOSER_MEMORY_LIMIT=-1
<C:\>composer install exhausted/packages

Please add some explanation to your answer such that others can learn from it
a
alvaro.canepa

For those who use Laravel Homestead

Write this on Homestead.yaml

variables:
    - key: COMPOSER_MEMORY_LIMIT
      value: -1

O
Obot Ernest

For Laravel

Step 1. Open your terminal

step 2. cd into your laravel directory

step 3. Type the command which composer, in your laravel directory and note the directory in which composer resides.

step 4. run the command php -d memory_limit=-1 /opt/cpanel/bin/composer update (you can also run the code if it works for you)

(change /opt/cpanel/bin/composer to the directory path returned in step 3 )

Problem solved


E
ESP32

In my case none of the answers helped. Finally it turned out, that changing to a 64 bit version of PHP (M$ Windows) fixed the problem immediately. I did not change any settings - it just worked.


can you explain how do you have changed php version from 32 bit to 64 bit?
@HaritsinhGohil Just download the 64 Bit Version from php.net/downloads.php, rename your 32 bit folder and copy the 64 bit version to a folder of the old versions name.
D
Dinush Chathurya

Go to your php ini file

set memory_limit = -1


K
Khalifa

In Windows 10 (Git bash), i had to use this

php -d memory_limit=-1 C:\\composer\\composer.phar install

A
Andrew

Its fixed in later composer versions. On Windows I had to uninstall composer then download+install latest version https://getcomposer.org/download/

Now running composer update works!


F
Fischer Tirado

First of all, try to update composer:

composer self-update

For some reason composer v1.0 doesn't works fine, in my case I had to update composer, after that I can execute:

composer update

N
Naser Nikzad

In my case it needed higher permissions along with this memory limit increase.

sudo COMPOSER_MEMORY_LIMIT=2G php /opt/bitnami/php/bin/composer.phar update

Running Composer with sudo is not a good idea, as it will mix up your permissions. This looks like you are running composer update on a server - this is also bad practise, as it will make it much more different to put the calculated dependencies under version control
Agree with @NicoHaase, you need to be so much careful using this! thanks.