How to increase the memory_limit for PHP 7.x and 8.x on MacOS

# php

While setting up up of my projects, I ran into the following error by running a simple composer install [...] command.

PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted [...]

Composer has a helpful section about this topic on the Troubleshooting page. But not as helpful when working with a Mac.

So let’s start fixing the memory limit.

Check your current memory_limit

First up, we’re gonna check the currently set memory_limit for PHP. To do that we’ll just execute the following command.

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

In my case, the memory_limit is currently set to 128M .

Change the memory_limit

Now that we know that we want to change the memory_limit, we need to locate the corresponding php.ini file. You do that with the following command.

php --ini

Now from this command there are different outcomes.

When PHP is installed via Homebrew, your result should look something like this:

Configuration File (php.ini) Path: /opt/homebrew/etc/php/8.3
Loaded Configuration File:         /opt/homebrew/etc/php/8.3/php.ini
Scan for additional .ini files in: /opt/homebrew/etc/php/8.3/conf.d
Additional .ini files parsed:      /opt/homebrew/etc/php/8.3/conf.d/ext-opcache.ini

From there, you can just go ahead and open /opt/homebrew/etc/php/8.3/php.ini , search for the memory_limit and change it from the current value (in my case 128M) to -1 .

When PHP is installed a different way, your output might look like something like this. Note the line on the bottom adding a additional .ini file loaded.

Configuration File (php.ini) Path: /usr/local/etc/php/8.3
Loaded Configuration File:         /usr/local/etc/php/8.3/php.ini
Scan for additional .ini files in: /usr/local/etc/php/8.3/conf.d

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

When this is your result, you’ll just have go change the memory_limit in the /usr/local/etc/php/8.3/php.ini file and if it also appears in the /usr/local/etc/php/7.2/conf.d/php-memory-limits.ini file, you’ll have to change it here too.

Note: If you change your memory_limitto -1 , your memory_limit is unlimited. If you want to give it a specific amount, you can just change the value to something like 4G for example.

The final touch

The last step is to restart the php service:

brew services restart php

And you are done. Congrats!