Composer Update failed -- out of memory

120,970

Solution 1

Solved by deleting the whole vendor folder, and then doing the composer update again, and it works... somehow. I don't even understand :v

Solution 2

Check the Composer's troubleshooting wiki, especially the memory limit errors section.

For instance, by running the composer like this:

php -d memory_limit=-1 `which composer` update

I get no error anymore. So it is probably an insufficient memory issue that can be solved inline, without altering your default PHP configuration.

What the command above does is that it sets the PHP CLI memory limit to "unlimited" (ie. -1) and then it runs the inline composer update command.

Please note that instead of `which composer` you should probably use the real path of your composer.phar PHP script. The which composer written inline (like in my example above) will be inline solved to your composer.phar full path (you may use whatever form you like).

Note: in case both the physical and virtual memory is exceeded the above solution might fail as well. If that is the case then the obvious solution would be to increase your system's virtual memory then try again.

Solution 3

The only thing that solve my problem was doing this:

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

REF: COMPOSER TROUBLESHOOTING

Solution 4

From my experience, memory errors from composer usually means it is spending too much memory looking for the right combinations of packages to install, especially the version constraints are not specific enough. For example, ^5.2.4 matches 5.3 to 5.3.29, 5.4 to 5.4.45, etc. For each specific version and permutation, composer has to get the package's dependencies to check if all the constraints are met. This is usually when the memory consumption gets huge.

Once the versions have been figured out, the installation phase uses much less memory. The resolved versions for each package are also stored in a composer.lock file so that the specific permutation installed can be replicated in other environments. And this is the potential solution to your issue: run composer update in your dev machine (which should have enough memory), deploy the updated composer.lock, and run composer install on the server.

Composer install will always reference the existing composer.lock for the versions to install for each package, and thus should seldom run into memory issues.

For a reference on how to express version constraints in composer.json, check out https://getcomposer.org/doc/articles/versions.md

Solution 5

Rather than permanently setting your memory limit to an increased number (or unlimited), I use this;

# Running an update
COMPOSER_MEMORY_LIMIT=-1 composer update
COMPOSER_MEMORY_LIMIT=-1 composer require PACKAGE/NAME

That temporarily set's the composer memory limit env variable to unlimited.

Share:
120,970
Aldibe
Author by

Aldibe

Updated on July 05, 2022

Comments

  • Aldibe
    Aldibe almost 2 years

    I got this error when running composer.phar update on my VM:

    PHP Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 144115188075867549 bytes) in phar:///bin/composer.phar/src/Composer/Util/RemoteFilesystem.php on line 179

    The composer.json, if needed:

    {
            "description" : "The CodeIgniter framework",
            "name" : "codeigniter/framework",
            "license": "MIT",
            "require": {
                    "php": ">=5.2.4",
                    "videlalvaro/php-amqplib": "2.5.*"
            },
            "require-dev": {
                    "mikey179/vfsStream": "1.1.*",
                    "videlalvaro/php-amqplib": "2.5.*"
            }
    }
    

    The VM just recently recovered from a bad disk sector problem, and the guy running the VM said that the VM has been moved to a new disk. There are only Java, PHP, httpd, postgre, rabbitmq and the website itself in my VM, and it already ran perfectly for about 4 months before this happened. I'm using PHP 5.6.11. Can anyone help please?

  • Twenty
    Twenty about 4 years
    Could you maybe elaborate on your answer a bit more, maybe by explaining the code?
  • Josh D.
    Josh D. over 3 years
    Thanks a lot for this. And it can also be used for pretty much any other command.
  • Eliezer Berlin
    Eliezer Berlin over 3 years
    My computer froze when I ran this... and then composer still failed with an OUT OF MEMORY error.
  • Mahbubur Rahman Mishal
    Mahbubur Rahman Mishal about 3 years
    Never happened to me, how much ram your PC have?
  • Eliezer Berlin
    Eliezer Berlin about 3 years
    I had 8GB of RAM, so it wasn't that. One of my co-workers eventually solved it, but I'm not sure how. I think he solved it by deleting composer-lock.json. Thank you, though!
  • Julix
    Julix over 2 years
    I like this answer a lot since it doesn't require changing anything permanently - downside of course is that if you encounter the same need in the future you might run into trouble again and have to go googling for this answer again :D