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.
Recommended PHP Memory Limit for Magento 2
For optimal performance:
- Production environment: 2G (2048M)
- Development environment: 3G (3072M) or higher for large stores
Update in php.ini (Global Setting)
Locate the PHP configuration file
Run this command to find the location of php.ini:
Look for Loaded Configuration File.
Edit the php.ini file
Open the file in a text editor:
Find and Update memory_limit
Locate the line:
Update it to:
Save and Exit
Save the file and exit (Ctrl + X, then Y, then Enter).
Restart the Web Server
Apply the changes by restarting the web server:
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:
- Use the -d flag with PHP:
- For long-running commands:
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.
- Edit the .htaccess File: Open the file located in the Magento root directory.
- Add the Following Line:
- 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().
- Edit the Script: Add the following line at the top of your PHP script:
- Run the Script.
Verify the New Memory Limit
To confirm the memory limit is updated:
- Check in CLI: Run this command:
- Check in Browser: Create a file called phpinfo.php in the Magento root directory with this content:
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 |
|
| 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:
To ensure Magento 2 runs smoothly:
- Set the PHP memory_limit to at least 2G in php.ini or .htaccess.
- Use -d memory_limit=3G for CLI commands when needed.
- Verify the new memory limit using phpinfo() or php -r "echo ini_get('memory_limit');".


