Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer: Allowed memory size error when installing package

When I run composer require yab/laravel-scout-mysql-driver this is the output that I get:

Using version ^2.40 for yab/laravel-scout-mysql-driver
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.3.2/libexec/composer.phar/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52

Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.3.2/libexec/composer.phar/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52

Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.

I'm running Composer 1.9.2.

Here are the last few lines of the output when I use -vvv:

Reading /Users/redacted/.composer/cache/repo/https---repo.packagist.org/provider-ircmaxell$password-compat.json from cache
Reading /Users/redacted/.composer/cache/repo/https---repo.packagist.org/provider-paragonie$constant-time-encoding.json from cache
Reading /Users/redacted/.composer/cache/repo/https---repo.packagist.org/provider-yab$laravel-scout-mysql-driver.json from cache
Reading /Users/redacted/.composer/cache/repo/https---repo.packagist.org/provider-symfony$class-loader.json from cache
Reading /Users/redacted/.composer/cache/repo/https---repo.packagist.org/provider-symfony$polyfill-apcu.json from cache
Reading /Users/redacted/.composer/cache/repo/https---repo.packagist.org/provider-symfony$polyfill-xml.json from cache
Reading /Users/redacted/.composer/cache/repo/https---repo.packagist.org/provider-gecko-packages$gecko-php-unit.json from cache
PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.3.2/libexec/composer.phar/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52

Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.3.2/libexec/composer.phar/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52

Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.

My memory limit is 128M. https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors doesn't seem to provide lot of insight other than to increase my memory limit but it seems like 128M should be sufficient?

php -d memory_limit=-1 /usr/local/bin/composer require yab/laravel-scout-mysql-driver did not help - I got "Allowed memory size of 1610612736 bytes exhausted" errors. Same thing with memory_limit=1024M.

I did php --ini and from that got /usr/local/etc/php/7.1/php.ini, which I edited to change the memory limit 1024MB and am getting the same thing: "Allowed memory size of 1610612736 bytes exhausted".

It's weird that the "allowed memory size" is the exact same in every instance. It's like nothing I'm doing is actually changing the memory size.

Any ideas?

like image 725
neubert Avatar asked Jan 30 '20 02:01

neubert


2 Answers

(may be a) DUPLICATE of Composer Update failed -- out of memory

I got the same issue with same versions (PHP 7.1, Composer 1.9.x) and exactly same memory limit (which after investigation is coded inside/defined in composer).

So, the first thing to do when you encouter this kind of error is to tell composer to not limit the memory with a variable, like this:

COMPOSER_MEMORY_LIMIT=-1 composer require "xxx/yyy"

This is the recommended way but sometime it's just not work.

In my project, it seems that the composer.lock get corrupted everytime I do a composer require xxx.

So to "fix" it, I do this:

  • remove your vendor folder (rm -fr vendor)
  • remove composer.lock (rm composer.lock)
  • make sure that your composer.json contains your new requirement (it should be there because of the previous composer require), if not, add it
  • do a fresh composer install

At this point, either you will clearly see a dependency issue, or everything will be installed.

I did this with success 2 times.

like image 183
Cédric Avatar answered Oct 07 '22 09:10

Cédric


Do not delete composer.lock as was recommended in previous answer for projects, which are dunning on the production. Moreover, ensure that it does exist, it helps both to save lots of resources and time on avoid recalculation of dependencies by composer + locking of libraries versions make project behavior more predictable.

"composer.lock" is a must have for any project and it's removal kinda similar to composer update for all. You might get in trouble because of the unlocking and, as a result, fetching newer versions of the libraries into the project and get breaking changes. In most cases in composer.json, libraries versions are not too strict (people usually put there major version, in best case minor version, and almost never version of the patch) so that removal of the composer.lock for a big project might lead to the huge issues and it will not really help, because composer will have to fetch all possible branches and version of all required libraries, which were defined in composer.json just to generate a composer.lock .

your solution with php -d memory_limit=-1 /usr/local/bin/composer require yab/laravel-scout-mysql-driver was a correct and in most cases it works. Thing is that seems to me that you have limited amount of available memory. In this case you can try to do one from the next things:

  1. in case if you are inside the docker container in OSX env, reconfigure your VM there (check Preferences of the docker and you can increase amount of memory and CPU, which can be allocated by docker - this is a very popular issue: people forget that in OSX there is a VM running to serve docker, so that they have by default limitations in CPU/MEM/disk allocation)
  2. in case if docker on the OSX is not the case and host has a real limitation with available memory, then ideally to use dev environment, which is has either more memory or add a swap for this purposes.
  3. From my observations, usually composer requires MOST of the memory exactly to recalculate all the dependencies, find out matching versions for the platform and take a hash from the remote repository and put all this info into the composer.lock. After composer.lock is generated, it does not need too much memory, so that install works perfectly with very limited amount of memory. So sometimes I used a workaround, like (which is quite terrible, and depends on luck, but sometimes worked):

    a. run composer require and wait until a new record appears in the composer.lock and then kill the process (to avoid revert of record in composer.lock, which is happens automatically on install failure). If you are lucky you will get it updated before memory limit is reached.

    b. then just run composer install and get finally library installed.

If none of the approaches worked, you can try to add manually record in the composer.lock, with proper hash. In this case you can avoid recalculation of all dependencies and intermediately jump into installation process. But this is just a quick win and later you will face again same issues (when next time you need to recaclculate lock file).

like image 4
Oleksii Chernomaz Avatar answered Oct 07 '22 10:10

Oleksii Chernomaz