Era Host hosting
EraHost – Free Domain, Cheap Hosting!
Client Area
Support 24/7
Menu

Modify PHP Memory Limit for Magento via Command-Line Settings

3 min read
02.01.2026

Magento often requires a higher PHP memory limit for resource-intensive tasks like setup:upgrade, indexer:reindex, or di:compile. Instead of permanently modifying the global php.ini, you can dynamically set the memory limit for specific Magento CLI commands.

Magento CLI PHP Memory Limit
Magento CLI memory — `-d memory_limit=-1` per command is the standard.

For closely related Magento PHP-memory topics, see How to Update PHP Memory Limit for Magento 2 and Magento CLI PHP Memory Limit.

Temporary Memory Limit via Command Line

To override the memory limit only for a single command, use the -d option with the php command:

php -d memory_limit=2G bin/magento <command>

Examples:

  1. Upgrade the Database:
  2. php -d memory_limit=2G bin/magento setup:upgrade
  3. Recompile Dependency Injection:
  4. php -d memory_limit=3G bin/magento setup:di:compile
  5. Reindex Data:
  6. php -d memory_limit=2G bin/magento indexer:reindex
  7. Run the Magento Cron Job:
  8. php -d memory_limit=2G bin/magento cron:run

Verify the Memory Limit in Magento CLI

To confirm the memory limit applied during a command, you can add a simple PHP command:

php -d memory_limit=2G -r "echo ini_get('memory_limit');"

Expected output: 2G

Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

Use Environment Variables (For Recurring Tasks)

If you frequently run Magento CLI commands that require higher memory, you can set an environment variable to avoid specifying -d memory_limit every time:

1. Set an Environment Variable Temporarily:

export PHP_MEMORY_LIMIT=2G

Then run Magento commands like this:

php bin/magento setup:upgrade

2. Set an Environment Variable Permanently:

Add this to your .bashrc or .zshrc file:

export PHP_MEMORY_LIMIT=2G

Apply the changes:

source ~/.bashrc

Combine with Other PHP Options

Magento CLI commands may also require other PHP configuration overrides. You can combine multiple -d options in a single command:

php -d memory_limit=3G -d max_execution_time=1800 bin/magento setup:upgrade

When to Use Command-Line Memory Limit

Best for One-Time or Debugging Tasks:

  • Useful for temporary operations, such as upgrades or reindexing.

Avoids Permanent Changes:

  • Prevents system-wide changes to php.ini.
  • Production Environment: 2G (2048M)
  • Development Environment: 3G (3072M) or higher for large catalogs.

Troubleshooting Common Issues

Memory Exhaustion Errors

Error: PHP Fatal error: Allowed memory size of X bytes exhausted.

Solution: Increase the memory limit using:

php -d memory_limit=3G bin/magento <command>

Command Fails Without Error

Solution: Check your PHP configuration:

php -r "phpinfo();"

Ensure the correct memory_limit is applied.

Global Configuration Takes Precedence

If the -d option does not work, ensure your system does not override settings from /etc/php-cli.ini or similar files.

To dynamically adjust PHP memory limits for Magento CLI commands:

  1. Use php -d memory_limit=2G bin/magento <command>.
  2. Verify the applied memory limit with php -r "echo ini_get('memory_limit');".
  3. Set environment variables for recurring commands.
Frequently asked questions
For CLI deployment commands: fine — they're one-shot, won't run away. For long-running CLI workers (queue consumers): no — a runaway worker will eat all RAM before OOM killer fires. For workers, cap at reasonable: `php -d memory_limit=4G bin/magento queue:consumers:start`.
Script wrapper if you always run the same commands. Wrap `bin/magento` with: `php -d memory_limit=-1 /var/www/magento/bin/magento "$@"`. Per-command if running ad-hoc — easier to think about memory per command than globally. Pick based on team workflow.
Less common but works: `PHP_INI_SCAN_DIR=/etc/php/8.1/cli/conf.d.magento php bin/magento ...` causes PHP to load .ini files from that directory in addition to default. Put a magento.ini there with `memory_limit = -1`. Avoids per-command flags; affects only when env var is set.
Each user has its own php.ini location resolution. CLI php.ini is global; the user running CLI gets that. If web SAPI uses 2G and CLI uses 1G (distro default), Magento setup commands hit OOM at 1G even though web SAPI has 2G. Set CLI to -1 or 4G+ explicitly.
Related articles
Change memory_limit via ini_set() in a PHP Script (Sibling Reference)
Set PHP memory_limit = -1 via Command Line (For CLI Scripts)
Fixing MySQL Error #1227: Access Denied for Enabling Event Scheduler