Composer: Easy memory limit troubleshooting

I needed to implement a PoC really quickly to integrate a Symfony project with Salesforce Apex REST API. For it, I needed the Guzzle PHP HTTP client to make the requests to this API. The easiest way to add this package as a dependency of a Symfony project is using Composer dependency manager:

$ php composer.phar require guzzlehttp/guzzle

However, every time I ran this command I got the following error:

Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes)...

It clearly indicates the memory_limit value I have set to PHP isn’t enough to complete the installation of the package.

My initial thought to fix this issue was to modify the memory_limit setting found in my PHP main configuration file, but it will not only apply for the PHP CLI but to all the PHP process that I’m running (i.e. Local web server and Symonfy commands.) This is a side effect I was not willing to accept.

I started to research about this case and found a very straightforward solution to it and it will only apply to the Composer command you apply it to. The solution is to use the COMPOSER_MEMORY_LIMIT environment variable, setting its value to -1. It can be added to the current Terminal session with

$ export COMPOSER_MEMORY_LIMIT=-1

And it will be used by all the composer commands you use in that session. Or it can be used on-demand by those Composer commands you know they need more than the memory_limit setting value you have set for your PHP installation with

$ COMPOSER_MEMORY_LIMIT=-1 php composer.phar [command]

By using any of these two you will ensure PHP process won’t consume more memory than the assigned one in the memory_limit parameter.

I hope this simple (yet powerful) trick helps you.


Tagged: ,