76

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?

5
  • 6
    I'm glad memory_limit exists because 144115188075867549 is exactly 128 petabytes if I did the calculation right... Oct 23, 2015 at 10:09
  • I've tried that settings myself and all went fine. Could you try wiping out your entire vendor directory and running the command again? Oct 23, 2015 at 10:18
  • right now i can't really access the VM since it's an office VM and i can't access it from outside of the office. i'll try your suggestion and update the result on monday probably. thanks :)
    – Aldibe
    Oct 24, 2015 at 2:45
  • 1
    yep, it works :) thanks a lot
    – Aldibe
    Oct 26, 2015 at 9:20
  • stackoverflow.com/questions/21815635/… follow this for get fast and quick solution.
    – pankaj
    Oct 29, 2020 at 10:24

28 Answers 28

104

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.

12
  • 13
    i use windows 10 and this command worked for me, php -d memory_limit=-1 "C:\ProgramData\ComposerSetup\bin\composer.phar" update Nov 2, 2018 at 15:21
  • this allowed me to get around this memory error and upgrade, thanks :)
    – iphigenie
    Jan 30, 2019 at 12:24
  • 2
    should be php -d memory_limit=-1 which composer update
    – Amorphous
    Feb 22, 2019 at 12:12
  • 2
    For me sadly it´s not working (win10, composer 1.9.0). It allocated 1.1 GB (!!!!) and exitted with "Out of memory" - despite there are at least 3 GB more free. To repeat: 1.1 GB (!) allocated by a dependency resolution tool....(!)
    – cljk
    Aug 13, 2019 at 6:05
  • @cljk, check php version. x84 or x64 ? Aug 23, 2019 at 12:18
62

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

4
  • 1
    This worked. But maybe you could expand your answer and explain why. Thanks anyway!! Jan 30, 2019 at 22:47
  • 6
    Thanks, in my case composer didn't succeed with 1.7 GB RAM ... what on earth is this tool doing? AFAIS it's a very poor adoption of a tool like npm for PHP ... Mar 21, 2019 at 15:57
  • 1
    Trying to install Symfony with PHP7.2 ... 1GB swap wasn't even sufficient to get symfony/flex installed as the first dependency to be installed ... npm is used in the same setup establishing a dev environment for building a client app, thus fetching tens of thousands of files from 1500+ packages w/o ever complaining about a lack of RAM ... composer/PHP renders more and more useless to me. Mar 21, 2019 at 16:07
  • 1
    I work on a windows machine with 16 GB RAM (memory_limit=-1) and struggling with the same problem. Cannt even try using a big swap. Perhaps I´ll have to boot a Linux-System just to run composer. What a mess
    – cljk
    Aug 13, 2019 at 6:01
33

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.

1
  • 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
    – Julix
    Oct 4, 2021 at 21:21
30

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

4
  • 3
    I guess your previous dependency set triggered a bug in Composer and made it enter an infinite loop trying to arrange the upgrade but it could handle direct installation from scratch just fine. I suppose you could file a ticket if you happened to keep your composer.lock file. Oct 26, 2015 at 12:39
  • 1
    Deleting the vendor folder and running composer install worked on Windows for me. I guess update comparisons take far more memory than a fresh install.
    – Nick W
    Oct 19, 2020 at 17:26
  • Don't forget to mention that clearing the composer cache is necessary lol
    – Vitor
    Oct 21, 2020 at 13:43
  • This looks the only way to go over this issue Dec 9, 2021 at 20:05
30

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

2
  • 1
    This was the correct answer for me for another issue - I manually edited the version number of a package in composer.json and it was causing the memory limit error.
    – damtypo
    Jun 14, 2020 at 23:40
  • 1
    I confirm. I had an old constraint "aws/aws-sdk-php": "~3.2" which was resolving to 3.204.0. So I updated this constraint to "aws/aws-sdk-php": "~3.204" and memory issue was gone. So I also reviewed constraints for other packages to avoid such a problem in future. I recommend to try this before playing around with memory_limit, because it can really bite you on cloud-based setups.
    – pstryk
    Dec 1, 2021 at 13:26
17

Katiak's answer worked but I had to modify it. You will need 4 GB of free space for this to work on a Linux machine. Make sure you sudo the commands if you're not root:

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

Composer takes alot of memory for certain repositories like Drupal.

Essentially, this creates 4 GB of Swap memory from the hard drive which the CPU can use to complete the composer command.

The original solution seems to have come from this Github thread but I could be mistaken:

https://github.com/composer/composer/issues/7348#issuecomment-414178276

To load the swap at boot add the following line in /etc/fstab

/var/swap.1 none swap sw 0 0

You may want to backup your fstab file just to be on the safe side.

To reclaim the swap space do this:

sudo swapoff -v /var/swap.1
sudo rm /var/swap.1

If you get a message like this after turning on the swap...

swapon: /var/swap.1: insecure permissions 0644, 0600 suggested.

...change the permission if appropriate

sudo chmod 600 /var/swap.1
13

Memory limit errors

Composer may sometimes fail on some commands with this message:

PHP Fatal error: Allowed memory size of XXXXXX bytes exhausted <...>

Or in my case :

Fatal error: Out of memory (allocated 1116733440) (tried to allocate 134217728 bytes) in phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/DependencyResolver/Pool.php on line 339

In this case, the PHP memory_limit should be increased.

Note: Composer internally increases the memory_limit to 1.5G.

To get the current memory_limit value, run:

php -r "echo ini_get('memory_limit').PHP_EOL;"

Try increasing the limit in your php.ini file (ex. /etc/php5/cli/php.ini for Debian-like systems):

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

Composer also respects a memory limit defined by the COMPOSER_MEMORY_LIMIT environment variable:

COMPOSER_MEMORY_LIMIT=-1 composer.phar <...>

Or, you can increase the limit with a command-line argument:

php -d memory_limit=-1 composer.phar <...>

This issue can also happen on cPanel instances, when the shell fork bomb protection is activated. For more information, see the documentation of the fork bomb feature on the cPanel site.


To get loaded php.ini files location try:

php --ini

Source : (Composer docs)

7

It's a memory problem, not a storage issue. You are reaching your PHP memory limit.

Edit /etc/php.ini and increase the memory limit ( memory_limit = 128M replace with memory_limit = 256M )

I would suggest you look for the reason composer is using so much memory , and find ways to cut your PHP memory usage :

  1. Upgrade to PHP56 if you haven't already
  2. Install Zend Opcache ( shares PHP memory between different instances )
  3. Uninstall any and all unused PECL extensions

This is the issue you are having : https://github.com/composer/composer/issues/1898

6
  • 2
    is it possible/normal for composer to request that much memory size? (allocate 144115188075867549 bytes)
    – Aldibe
    Oct 23, 2015 at 10:02
  • Look at the issue I linked to.. seems pretty common .. some had to increase memory consumption to 1GB... The advice there is to upgrade PHP , upgrade Composer, and request more accurate dependency versions ( 1.1.x instead of 1.x ) to reduce the amount of checks required )
    – DannyZB
    Oct 23, 2015 at 10:08
  • p.s. ... click the little V next to the answer if it solved your problem (:
    – DannyZB
    Oct 23, 2015 at 10:08
  • i guess i'll try to look into the second and third point :v and well, the problem is not yet solved...
    – Aldibe
    Oct 23, 2015 at 10:12
  • The biggest reduction in memory usage would probably be from removing unneeded composer dependencies and asking for more specific versions -- try that out. And try increasing memory max to 512m temporarily for now.
    – DannyZB
    Oct 23, 2015 at 10:17
7

I'm on a Windows machine and I tried all of the answers from this question, but none of them worked. For me, it FINALLY worked after I ran composer using the 64-bit version of PHP.

To run composer using a local copy of PHP x64 you can do the following:

  1. Download the zip file from here (I used the VC15 x64 Thread Safe version): https://windows.php.net/download
  2. Unzip the file
  3. Copy php.ini-development and rename to php.ini
  4. Uncomment the extension_dir = "ext" line and any other php extensions you will need (such as extension=gd2 or extension=openssl). If any other PHP extensions are needed for the update then it will tell you while running the command.

Working command:

"C:\path\to\php-7.2.23-Win32-VC15-x64\php.exe" -d memory_limit=-1 "C:/path/to/composer.phar" update

Source: https://ourcodeworld.com/articles/read/816/how-to-solve-composer-install-update-error-virtualalloc-failed-0x00000008

0
7

Try this one

COMPOSER_MEMORY_LIMIT=-1 composer require [package name]
3
  • My computer froze when I ran this... and then composer still failed with an OUT OF MEMORY error. Jan 3, 2021 at 15:17
  • Never happened to me, how much ram your PC have? Feb 9, 2021 at 8:41
  • 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! Feb 10, 2021 at 9:46
6

When using the software Laragon on Windows, the following line helped me requiring a new package without getting the memory error:

php -d memory_limit=-1 "C:\laragon\bin\composer\composer.phar" require <insert package author here>/<insert package name here>
1
  • 1
    Thanks a lot for this. And it can also be used for pretty much any other command.
    – Josh D.
    Sep 28, 2020 at 17:12
4

The solution that works both on local environment but also in a container (e.g. in a build stage of a pipeline) where composer might be installed in tricky way, is

COMPOSER_MEMORY_LIMIT=-1 composer install
3
php -d memory_limit=-1 /usr/local/bin/composer install

This command work for me.

1

In my case, I was facing this error because of running composer install inside vagrant box. Running it inside my host machine didn't cause the issue.

1

Nothing worked for me except putting memory_limit = -1 in php.ini file.

1

I set memory_limit=-1 in my php.ini file but didnt work but i went ahead to execute commands from link below and then run composer install and worked perfectly

https://tecadmin.net/enable-swap-on-ubuntu/

1

It worked for me!

Download the zip 64-bit version of your PHP from this link: https://windows.php.net/download/

enter image description here

Now extract it to C:\PHP-74\ and rename the php.ini-development to php.ini

php.ini

Now open this php.ini and uncomment the following by removing ; (semicolon).

extension_dir = "ext"
extension=bz2
extension=curl
extension=fileinfo
extension=gd2
extension=gettext
extension=mbstring
extension=exif      ; Must be after mbstring as it depends on it
extension=mysqli
extension=openssl
extension=pdo_sqlite

In my scenario these extensions are required you can compare your old php.ini file of xampp or wamp resolve this one. Now save the file and run the following command.

"C:\PHP-74\php.exe" -d memory_limit=-1 "C:\ProgramData\ComposerSetup\bin\composer.phar" update
1

Update composer to v2.

composer self-update --2

Remove vendor path and composer.lock file.

Set in php.ini memory_limit=-1.

Run

composer install
1

This one helped me, with showing memory usage:

php -d memory_limit=-1 /usr/local/bin/composer update --verbose --profile
0

You may also get resolved by just removing vendor directory and re-execute composer install command.

0

Try increase the memory_limit value in your active php.ini file.

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 512M

For example.

0

this solution will fix your problem.

Update your php.ini file then restart Apache or your server example: memory_limit=128M to memory_limit=1128M

It's work on me.

0
php -d memory_limit=-1 /usr/local/bin/composer update --no-scripts

memory_limit=-1 is for unlimited memory for this process when you add php before the composer for unlimited memory then you have to give composer complete path where its installed on server (for ubntu) its /usr/local/bin/composer Reamining part is just compoer commands update/install

1
  • 1
    Could you maybe elaborate on your answer a bit more, maybe by explaining the code?
    – Twenty
    Feb 9, 2020 at 17:11
0

Increasing the memory_limit value on my php.ini did not solve my problem. What I did was:

  1. Delete composer.lock,
  2. run composer update,

And that did the trick!

0

If you are still facing issues after updating your memory limit then the best thing to try is to run:

composer update --lock

This will update your lock file so that composer can run from it if you have changed your composer.json file.

You can now optimally run:

composer update

or

composer install
0

If on Windows, using XAMP, I recommend to Update your Composer version to latest. Uninstall, download latest - https://getcomposer.org/download/ and install.

Could also try update PHP version to latest.

0

If you are running into composer memory problems, now your best option is probably to use composer v2, if you can.

Composer v2 has many performance improvements and as a result, it takes much less memory.

I was always having to use COMPOSER_MEMORY_LIMIT=-1 composer update but after the v2 update, I no longer need to use this.

Check out the release announcement https://blog.packagist.com/composer-2-0-is-now-available/ or download the latest version https://getcomposer.org/download/

-7

sudo php -d memory_limit=-1 bin/magento setup:di:compile

1
  • 3
    Could you please explain how your suggestion solves the original problem. Code only answers should generally be avoided.
    – Nigel Ren
    Oct 17, 2017 at 18:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.