How to Fix Composer update memory limit in PHP or Laravel

This reminds me of when I was working on an outdated Laravel project and needed to upgrade the Laravel and related dependencies.  To upgrade the dependencies, I encountered this error: “Composer update memory limit”.

The memory limit error occurs during a composer update (or other Composer commands) because Composer can sometimes require a lot of memory to fix and update dependencies, especially for larger projects.

Composer update memory limit

How to fix the error?

Here are different ways to fix the issue.

Composer update memory limit in PHP or Laravel

Increase PHP Memory Limit Temporarily

You can run the Composer command with a prefixed memory limit increase:

php -d memory_limit=-1 composer update

The -d memory_limit=-1 sets the memory limit to unlimited for this command.

Increase Composer’s Memory Limit

Composer also has its memory limit setting, separate from PHP’s.

You can use the COMPOSER_MEMORY_LIMIT environment variable to increase Composer’s memory limit.

COMPOSER_MEMORY_LIMIT=-1 composer update

Increase PHP Memory Limit Globally

Follow the below steps:

Step 1: Locate your php.ini file. You can find its location by running:

php --ini

Step 2: Open the php.ini file

Open the php.ini file in a text editor and search for the memory_limit setting. Increase its value.

For example:

memory_limit = 512M

You can increase it to a higher value, like 1G for 1 gigabyte, if needed.

Step 3: Save the file

You can save the file and restart your web server to apply the changes.

Optimize Composer

Optimizing Composer can help:

Use the –prefer-dist option

composer update --prefer-dist

The above command tells Composer to fetch distributed versions of packages, which can sometimes reduce the memory footprint.

Clear the Composer cache

If there might be some corrupted files or issues with the cache, clearing it might help:

composer clear-cache

I hope these solutions will help you fix the error.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.