How to solve Composer PHP memory exhausted issue in 30 seconds (2021)

Ralph J. Smit Laravel Software Engineer

Nothing is more irritating than software that should just work, whilst it doesn't. One of those highly irritating things are problems with composer. One of those problems is the error message 'Allowed memory size of {indecipherable number} bytes exhausted'. To fix this, you need to go and edit your PHP's configuration. But luckily, there's a way that's even faster and simpler.

The problem here is that Composer doesn't have enough memory. You might say, well, I've got a powerful computer. What's the problem? The problem is the PHP configuration contains a certain value for the memory that the computer is only allowed to use. This value is set in the php.ini file. To change that, you need to dive into your configuration files, restart PHP and just hope that it works.

If you're using a Mac and installed PHP via Brew, you could use an app like PHP Monitor to find out how much it is.

How to fix composer memory limit exhausted

In order to fix this, it's handy to allow unlimited memory usage. This sounds scary, but it really isn't. It's just a precaution in order to make sure that you're never hitting that limit again.

In order to allow maximum memory, instead of typing composer before every command, type this:

php -d memory_limit=-1$(which composer)

So the composer upgrade command would become this:

# Old
composer upgrade -W
# New
php -d memory_limit=-1$(which composer) upgrade -W

Or if you really need some powerful Composer troubleshooting, upgrade with extra verbose output, timestamps & allow up- or downgrading of dependencies.

php -d memory_limit=-1$(which composer) upgrade -vvv --profile -W
Fix the composer memory exhausted issue by changing the PHP memory limit to unlimited.

Allowing composer and PHP unlimited memory usage.

Creating an alias

As you see, it's very easy to fix the memory issue. But having to type this before every command is quite cumbersome. That's why we'll create an alias for that. An alias is a sort of shortcut in the terminal.

We'll define the alias as follows: every time we type composer, replace that with php -d memory_limit=-1$(which composer). So when you type composer upgrade, it means exactly the same as php -d memory_limit=-1$(which composer) upgrade. Only a lot shorter!

How to create an alias for zsh (macOS Catalina and newer)

To create an alias, we'll need to edit a file called .zshrc. This is a kind of configuration file and should be in your homefolder.

First, check whether the file exists:

open -e ~/.zshrc
# If you get an error, open up text editor and create
# an empty file called '.zshrc' in your home folder.

Next, add the following code at the bottom of your .zshrc file:

alias composer="php -d memory_limit=-1$(which composer)"

Save the file. The changes will now be implemented on any new terminal session. Stop the current session or run the code below, and then it will work for the current session too:

source ~/.zshrc

How to create an alias for bash (macOS Mojave and older)

We need to do something very similar for bash as for zsh. This time, we'll edit the .bashrc file. Open it (or create it, if needed), manually or via terminal:

open ~/.bash_rc

Now add the following line at the end of the file:

alias composer="php -d memory_limit=-1$(which composer)"

Here, too, the changes will only apply for a new session. So stop the current session or run the below code:

source ~/.bashrc


Now, anytime when you run composer, you should not come into any memory issues again. By using a command line alias, we created a very handy shortcut that doesn't cost you any extra time, but prevents just errors in the future.

Have you encountered an other issue with composer or have a question? I'm curious, leave a comment below👇

Published by Ralph J. Smit on in Guides . Last updated on 11 March 2022 .