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?
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.
Set it to use as much memory as it wants with:
COMPOSER_MEMORY_LIMIT=-1 composer update
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.
php -d memory_limit=-1 $(which composer) update
is more convenient due to having no need to specify composer path.
php -d memory_limit=-1 $(which composer).phar update
The best solution for me is
COMPOSER_MEMORY_LIMIT=-1 composer require <package-name>
mentioned by @realtebo
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/
COMPOSER_MEMORY_LIMIT=-1 composer require <package name>
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
set COMPOSER_MEMORY_LIMIT=-1
and then my composer command worked for me on Windows.
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
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
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
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.
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 ...
COMPOSER_MEMORY_LIMIT=-1 composer update --ignore-platform-reqs
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
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.
<C:\>set COMPOSER_MEMORY_LIMIT=-1
<C:\>composer install exhausted/packages
For those who use Laravel Homestead
Write this on Homestead.yaml
variables:
- key: COMPOSER_MEMORY_LIMIT
value: -1
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
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.
Go to your php ini file
set memory_limit = -1
In Windows 10 (Git bash), i had to use this
php -d memory_limit=-1 C:\\composer\\composer.phar install
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!
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
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
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
Success story sharing
COMPOSER_MEMORY_LIMIT=-1; export COMPOSER_MEMORY_LIMIT
. Or define it a shell alias?alias memcomposer='COMPOSER_MEMORY_LIMIT=-1 composer'
, then usememcomposer
instead ofcomposer
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