380

I am trying to add HWIOAuthBundle to my project by running the below command.

composer require hwi/oauth-bundle php-http/guzzle6-adapter php-http/httplug-bundle

HWIOAuthBundle github: https://github.com/hwi/HWIOAuthBundle

When I try to run composer require I am getting the out of memory error.

Using version ^0.6.0@dev for hwi/oauth-bundle Using version ^1.2@dev for php-http/guzzle6-adapter Using version ^1.10@dev for php-http/httplug-bundle ./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 67108864 bytes) in phar:///usr/local/Cellar/composer/1.4.2/libexec/composer.phar/src/Composer/DependencyResolver/Solver.php on line 220

Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 67108864 bytes) in phar:///usr/local/Cellar/composer/1.4.2/libexec/composer.phar/src/Composer/DependencyResolver/Solver.php on line 220

I tried setting the memory_limit to 2G in my php.ini file but did not work. I found my php.ini by running php -i | grep php.ini

Brian Chen
  • 4,001
  • 3
  • 9
  • 15
  • 4
    Have you done a composer self-update lately? Not sure if the 1.4.2 in your error message indicates version 1.4.2 but the latest version of composer is 1.6.2. And how much physical memory do you have? Is it a vm or cloud server? – Cerad Mar 10 '18 at 19:03
  • @Cerad Yes - I did a composer self-update before and my composer version is 1.6.2 – Brian Chen Mar 10 '18 at 22:02
  • @Cerad this is on my local machine and I definitely should have enough memory – Brian Chen Mar 10 '18 at 22:03
  • When experiencing this issue on projects where Composer ran fine previously, I'd advise running `composer self-update --rollback`. I started running into memory exhaustion with v.1.9.3, which stopped when I rolled back to 1.8.6. – karolus Nov 15 '19 at 03:42
  • Does no-one else think that it's pretty crazy that running `composer require` can use more than 1.5G of memory? When I first saw this I thought it has to be a bug in composer. Setting the memory limit to -1 did work for me... but how on earth is 1.5G not enough? Can composer really need so much? – Daniel Howard Aug 03 '20 at 10:55
  • See this [memory-limit-errors](https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors) – Arash Younesi Oct 20 '20 at 21:14
  • https://stackoverflow.com/questions/21815635/composer-running-out-of-memory-on-every-project-mac-os-x/64589121#64589121 follow this for get fast and quick solution. – pankaj Oct 29 '20 at 10:26
  • In my case, I was missing a swap partition. More info: https://github.com/composer/composer/issues/7348#issuecomment-414178276 – М.Б. Apr 28 '21 at 12:20

24 Answers24

588

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

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

php -d memory_limit=-1 composer.phar require hwi/oauth-bundle php-http/guzzle6-adapter php-http/httplug-bundle

To get loaded php.ini files location try:

php --ini

Another quick solution:

php composer.phar COMPOSER_MEMORY_LIMIT=-1 require hwi/oauth-bundle php-http/guzzle6-adapter php-http/httplug-bundle

Or just:

COMPOSER_MEMORY_LIMIT=-1 composer require hwi/oauth-bundle php-http/guzzle6-adapter php-http/httplug-bundle
Adam
  • 6,447
  • 1
  • 12
  • 23
  • 77
    COMPOSER_MEMORY_LIMIT=-1 composer update works for me also. – Kishan Patel Jun 07 '20 at 04:08
  • 1
    Thanks for the command `php --ini` ! in php 7.4 you have a different php ini file (php-cli.ini) for clid – Vincent Decaux Jun 11 '20 at 10:27
  • 9
    using ```php -d memory_limit=-1 composer.phar install``` worked for me. Thanks! – Tomasz Kuter Jun 21 '20 at 15:28
  • 1
    is memory_limit= -1 a good solution ? it seems like it removes any memory consumption limit and can use up all of it. – kamote ulalo Jun 23 '20 at 05:53
  • @kamoteulalo `php -d` affects only the running command, btw memory_limit helps prevent poorly written scripts for eating up all available memory on a server, so if your code is well written so there's no fear to set memory_limit to unlimited. but still, it's not recommended. – Adam Aug 22 '20 at 20:52
  • 12
    On Windows, use `SET COMPOSER_MEMORY_LIMIT=-1` – Vincent Decaux Oct 06 '20 at 13:53
  • For me, inside a vagrant machine and using composer as part of another script, it works by prepending `COMPOSER_MEMORY_LIMIT=-1` to the command. – daco Feb 08 '21 at 09:29
  • When using php and composer inside a dev docker container, this is what worked for me `docker-compose exec -e COMPOSER_MEMORY_LIMIT=-1 composer require hwi/oauth-bundle php-http/guzzle6-adapter php-http/httplug-bundle` – Nacho Mezzadra Mar 15 '21 at 15:04
427

In my case I was trying to require this package when I got this error.

You can run like this, and you don't have to update the PHP INI file:

COMPOSER_MEMORY_LIMIT=-1 composer require huddledigital/zendesk-laravel
odubah
  • 4,309
  • 1
  • 13
  • 10
  • 1
    Also helped. My PHP limit was 128MB, but the error message said that PHP composer limit is 1,5GB (which means it's configured elsewhere for this particular case). So modification of php.ini didn't help, but this answer did. – Tom Jul 25 '20 at 08:12
  • Thank you, putting the constant `COMPOSER_MEMORY_LIMIT=-1` before the `composer require xyz` made it work (the accepted answer did not). – hPNJ7MHTyg Jul 13 '21 at 07:36
  • you saved my day, in my case, i am instalingl `COMPOSER_MEMORY_LIMIT=-1 composer require guzzlehttp/guzzle` – Yohanim Aug 19 '21 at 14:31
103

Another solution from the manual:

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

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

Or in my case

export COMPOSER_MEMORY_LIMIT=-1
composer <...>
Thomas Vangelooven
  • 1,679
  • 2
  • 12
  • 18
  • 15
    On mac I edited `~/.bash_profile` to add `alias composer="COMPOSER_MEMORY_LIMIT=-1 composer"` and that solved it for me. Be sure to source the file after the change or restart your terminal – nicholas.alipaz Sep 23 '19 at 23:43
  • perfect solution (no need to change any php configuration) – scibuff Feb 26 '21 at 15:58
88

Same problem, none of anything related to "memory_limit" worked, but..

composer self-update --2

..solved my problem. (upgrade: 1.10.17 -> 2.0.4)

Milla Sense
  • 931
  • 5
  • 4
61

On Windows 10;

Goto C:\ProgramData\ComposerSetup\bin

Edit: composer.bat and add memory_limit=-1 in the last line as shown below.

@echo OFF
:: in case DelayedExpansion is on and a path contains ! 
setlocal DISABLEDELAYEDEXPANSION
php -d memory_limit=-1 "%~dp0composer.phar" %*

Problem solved ;)

TIGER
  • 2,864
  • 5
  • 35
  • 45
Digital Human
  • 1,599
  • 1
  • 16
  • 26
  • 4
    the best one for me, almost all of them didn't work for me. – krachleur May 27 '20 at 19:23
  • 1
    I also had to update *composer* file without .bat extension (adding same -d memory_limit=-1) to last line. – Maulik Jun 18 '20 at 22:17
  • 2
    In windows, the location of `composer.bat` need not be `C:\ProgramData\ComposerSetup\bin` always, but I guess that is the default location. However, if someone want to find location of `composer.bat` just need to run the `where` command: like `where composer`. That will show exact location. – rineez Sep 17 '20 at 18:47
48

Since none of the previous answers included set it took me a bit to figure out how to do it in Windows without altering the php.ini, but here's what worked for me:

set COMPOSER_MEMORY_LIMIT=-1
composer require hwi/oauth-bundle php-http/guzzle6-adapter php-http/httplug-bundle
Arno van Oordt
  • 2,912
  • 5
  • 33
  • 63
38

I have bypassed the problem in a Homestead Laravel (vagrant) virtual machine running the composer commands preceded by COMPOSER_MEMORY_LIMIT=-1:

Examples

To update Composer:

COMPOSER_MEMORY_LIMIT=-1 composer update

To install a package:

COMPOSER_MEMORY_LIMIT=-1 composer require spatie/laravel-translatable
Davide Casiraghi
  • 15,591
  • 9
  • 34
  • 56
30

For this error in macOS Catalina and macOS Big Sur use this line:

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

I used this line to update on Symfony 5. This command also worked with laravel 7.

juanitourquiza
  • 2,097
  • 1
  • 30
  • 52
  • 1
    It worked for me! php -d memory_limit=-1 composer.phar require laravel-frontend-presets/tailwindcss --dev – Pham Quang Apr 15 '21 at 07:58
  • How do I fixed this permanently ? I need to redo it every time creating new project. Any advise ? – Nizzam Nov 10 '21 at 09:40
29

Just set the memory_limit specifying the full route of your composer.phar file and update, in my case with the command:

php -d memory_limit=-1 C:/wamp64/composer.phar update
Acapulco
  • 3,373
  • 8
  • 38
  • 51
Aitor Fernandez
  • 291
  • 3
  • 2
27

Sometimes the problem is in the composer memory limit. In my case, I tried increasing the php memory limit but still got the error. You can use COMPOSER_MEMORY_LIMIT=-1 to get around that. Use it as a prefix:

COMPOSER_MEMORY_LIMIT=-1 composer require the/library

You have to prefix it again in the future.

Hope this helps.

meow2x
  • 2,056
  • 22
  • 27
21

It was recently identified that Composer consumes high CPU + memory on packages that have a lot of historical tags. See composer/composer#7577

A workaround to this problem is using symfony/flex or https://github.com/rubenrua/symfony-clean-tags-composer-plugin

composer global require rubenrua/symfony-clean-tags-composer-plugin
Ruben Gonzalez
  • 219
  • 2
  • 3
  • 2
    Just need to have the plugin installed globally? Nothing else to be done? Could you please explain what this plugin actually do? Does is reduce memory usage by composer in such cases? – rineez Sep 17 '20 at 18:50
20

Just in case you get a composer error with:

Could not open input file: composer

run:

php -d memory_limit=-1 /usr/local/bin/composer require ...
Marcus Christiansen
  • 3,017
  • 7
  • 49
  • 86
14

what about windows?

i use windows 10 and this command worked for me,

php -d memory_limit=-1 "C:\ProgramData\ComposerSetup\bin\composer.phar" update
Mohamed Hany
  • 451
  • 4
  • 10
13

Composer 2.0 preview is available now: https://github.com/composer/composer/releases Fixed issue for me. You can set up a preview with composer self-update --preview

EDIT: Composer 2 with memory tuning released

Anatoly Sokolov
  • 188
  • 1
  • 8
8

in windows by xampp i just changed:

;memory_limit=512M 

in php.ini to:

memory_limit =-1

then restart the Apache by xampp

this is the result:

; Maximum amount of memory a script may consume
; http://php.net/memory-limit
memory_limit =-1
;memory_limit=512M
saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58
7

You can use a specific php Version when running Composer

If, like me, for some reason, you are using PHP 32 bits even though your computer is 64 bits, this will always limit the amount of memory allocated to Composer. I solved my problem this way:

  • Install a 64 bits php version somewhere on your computer (let's say in C:/php64)
  • In composer (using cygwin in my case), run:

COMPOSER_MEMORY_LIMIT=-1 C:/php64/php.exe ../composer.phar update

Roubi
  • 1,989
  • 1
  • 27
  • 36
  • This suggestion worked for me! Thanks. A note is that you also need to update the system environment variable to use the new 64bit version globally. – Ojchris May 13 '20 at 08:14
6

On Mac php 7.4

run

php --ini

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

If Additional .ini files parsed: memory_limit needs to be changed in

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

As Jose Seie writes, set memory to

memory_limit = -1 or memory_limit = 1G
5

Just want to share my situation on this matter.

Problem context:

  1. Running composer in a vagrant box.
  2. Was getting this message after try to run composer require "laravel-doctrine/orm:~1.4.13":

Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/bin/composer/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.

  1. Have tried setting php.ini memory limit to -1. (still not working).

Solution:

  1. Apparently my composer.json and composer.lock has some issues. Ran $ composer validate, and the result was: "The lock file is not up to date with the latest changes in composer.json, it is recommended that you run composer update."

  2. So I ran $ composer update, and all dependencies are resolved. Imho, when the dependencies has some issues, maybe the tree building is out of sync, hence the out of memory issue.

Hope this helps anyone out there.

Steven Yip
  • 51
  • 1
  • 2
4

For Macbook: run command sudo nano ~/.bash_profile to edit bash_profile then add alias composer="COMPOSER_MEMORY_LIMIT=-1 composer" in that file, then save and exit.

Hope this will solve the problem; Happy coding!

3

I condensed or packaged up the useful and accepted answer here into reusable (zsh) aliases/functions, for quicker and easier-to-remember reuse:

# composer high-memory
composermem() {
  php -r "echo ini_get('memory_limit').PHP_EOL;"
}
alias composerbig='COMPOSER_MEMORY_LIMIT=-1 composer $1'

(php composer.phar is already aliased to composer on the system).

emjayess
  • 126
  • 6
2

In my case, my composer version was 1.x which was back-dated. After upgrading the version to 2.x it worked as intended.

sh6210
  • 4,190
  • 1
  • 37
  • 27
0

Make sure to not require a package before making sure the vendor folder exists.

Check if you have done composer install before. You may be just cloned the repository to your machine. So, you have to install the old packages before requiring a new one. Or you may want to include this option --profile to your composer command to see the timing and memory usage information.

Saud Alfadhli
  • 846
  • 1
  • 8
  • 11
-2

I am using php7.2 and the size is set to memory_limit = 512M by default. to increase the size, you need to locate php.ini.

In Wamp, it is located in C:\wamp64\bin\php\php7.2.23\php.ini. Please watch out for the PHP version as it is the name folder, in my case I have php7.2.23

In Laragon, it is located in C:\laragon\bin\php\php-7.2.23-Win32-VC15-x64\php.ini as was in my case.

If you don't want to allow unlimited space, you can set whatever size that is convenient to you, in my case I did memory_limit = 2G

if you want to allow unlimited space, you can change to memory_limit = -1

Chetam Okafor
  • 493
  • 4
  • 7
-2

Running composer dump-autoload solves it for me.

Abdellah Ramadan
  • 367
  • 4
  • 15