Memory problems when running php commands or composer

If you have a server with low memory resources you are probably familiar with the error “Fatal error: Allowed memory size of XXX bytes exhausted…” when you attempt to run a php cli command or composer.

Running php commands

Of course you can circumvent this by changing your php configuration files (either the main or local php.ini). However this is not always practical because you will need to restart the web server.

So, the alternative is to use the -d option on the command line directly where you can set php options on the fly 🙂

For the memory limitation you can run

$ php -d memory_limit=<new limit> <command to run>

Where <new limit> is the actual memory limit you want to use for the command you are about to run and <command to run> is that command. The memory limit can be something like 1024M or 2G or you can even remove the limitation by using -1.

Here is the code for the setup:upgrade command without memory limitations:

$ php -d memory_limit=-1 setup:upgrade

Useful PHP command line options

Another useful php option is the -v that shows you the php version you are running on the command line by default.

$ php -v
PHP 7.3.20-1+0~20200710.65+debian9~1.gbpc9cbeb (cli) (built: Jul 10 2020 07:22:47) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.20, Copyright (c) 1998-2018 Zend Technologies
    with the ionCube PHP Loader (enabled) + Intrusion Protection from (unconfigured) v10.3.2, Copyright (c) 2002-2018, by ionCube Ltd.
    with Zend OPcache v7.3.20-1+0~20200710.65+debian9~1.gbpc9cbeb, Copyright (c) 1999-2018, by Zend Technologies

And the -i where you list the equivalent to the phpinfo() function right on the command line

$ php -i
PHP Version => 7.3.20-1+0~20200710.65+debian9~1.gbpc9cbeb

System => Linux 4.9.0-13-amd64 #1 SMP Debian 4.9.228-1 (2020-07-05) x86_64
Build Date => Jul 10 2020 07:22:47
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php/7.3/cli
Loaded Configuration File => /etc/php/7.3/cli/php.ini
Scan this dir for additional .ini files => /etc/php/7.3/cli/conf.d
Additional .ini files parsed => /etc/php/7.3/cli/conf.d/05-ioncube.ini,

Running composer

If you have composer installed on your server and you are running a composer command directly you will not be able to use the above strategy.

The way to circumvent this is by downloading composer and run it using the php command. How?

Install composer in your current directory by running the following commands (taken from

$ php -r "copy('', 'composer-setup.php');"
$ php -r "if (hash_file('sha384', 'composer-setup.php') === '795f976fe0ebd8b75f26a6dd68f78fd3453ce79f32ecb33e7fd087d39bfeb978342fb73ac986cd4f54edd0dc902601dc') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"

The use php composer.phar instead of composer. Because you are actually running composer via php you can use the -d option to change the memory limitation 🙂


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

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *