Magento 2 is a resource-intensive application that requires a higher PHP memory limit to run smoothly. Here is how you can update the memory limit for Magento 2.

Update in php.ini (Global Setting)

1

Locate the PHP configuration file

Run this command to find the location of php.ini:

php --ini

Look for Loaded Configuration File.

2

Edit the php.ini file

Open the file in a text editor:

sudo nano /etc/php/7.x/apache2/php.ini # For Apache
sudo nano /etc/php/7.x/fpm/php.ini # For Nginx with PHP-FPM
3

Find and Update memory_limit

Locate the line:

memory_limit = 128M

Update it to:

memory_limit = 2G
4

Save and Exit

Save the file and exit (Ctrl + X, then Y, then Enter).

5

Restart the Web Server

Apply the changes by restarting the web server:

sudo systemctl restart apache2 # For Apache
sudo systemctl restart php7.x-fpm # For Nginx with PHP-FPM

Update for Magento CLI Commands

When running Magento CLI commands like setup:upgrade or indexer:reindex, you may need a higher memory limit. You can specify it dynamically:

  1. Use the -d flag with PHP:
  2. php -d memory_limit=2G bin/magento setup:upgrade
  3. For long-running commands:
  4. php -d memory_limit=3G bin/magento indexer:reindex

Update via .htaccess (For Shared Hosting)

If you are on shared hosting and cannot access php.ini, add the memory limit in Magento .htaccess file.

  1. Edit the .htaccess File: Open the file located in the Magento root directory.
  2. Add the Following Line:
  3. php_value memory_limit 2G
  4. Save the File.

Update Memory Limit Temporarily in PHP Script

If you want to set the memory limit for a specific script, you can use ini_set().

  1. Edit the Script: Add the following line at the top of your PHP script:
  2. ini_set('memory_limit', '2G');
  3. Run the Script.

Verify the New Memory Limit

To confirm the memory limit is updated:

  1. Check in CLI: Run this command:
  2. php -r "echo ini_get('memory_limit');"
  3. Check in Browser: Create a file called phpinfo.php in the Magento root directory with this content:
  4. <?php
    phpinfo();
    ?>

    Open it in your browser (http://yourdomain.com/phpinfo.php) and check the memory_limit value.

Common Issues and Solutions

Issue Solution
Error: Allowed Memory Size Exhausted Increase the memory limit in php.ini or use -d memory_limit=2G for CLI commands.
Changes Not Reflected
  • Ensure you are editing the correct php.ini file (check php --ini).
  • Restart the web server after making changes.
Shared Hosting Limitations If you are on shared hosting and cannot edit php.ini, use .htaccess to update the memory limit.

Example for Magento CLI Commands

Run Magento commands with a higher memory limit:

php -d memory_limit=3G bin/magento setup:upgrade
php -d memory_limit=3G bin/magento setup:di:compile
php -d memory_limit=3G bin/magento indexer:reindex

To ensure Magento 2 runs smoothly:

  1. Set the PHP memory_limit to at least 2G in php.ini or .htaccess.
  2. Use -d memory_limit=3G for CLI commands when needed.
  3. Verify the new memory limit using phpinfo() or php -r "echo ini_get('memory_limit');".