How do I set the memory_limit for PHP CLI when using xampp
The error Allowed memory size of 262144 bytes exhausted is one of the most frequent stumbling blocks when working with command-line tools on local development environments like XAMPP. The default configuration often restricts PHP CLI to just 256 KB, which is sufficient for a simple script but disastrous for anything involving Composer, database migrations, or large file processing.
You likely already know that editing the php.ini file associated with your XAMPP installation is the standard approach for setting memory limits in web contexts. However, applying changes to that file does not automatically apply to CLI processes unless specific conditions are met. If you have modified php.ini and still see the exhaustion error when running scripts directly, it usually means the CLI environment isn't picking up your global configuration, or the path you are editing points to a different instance of PHP than the one executing your command.
When you run composer install or composer update, the tool executes via the system's PHP binary, not necessarily through the Apache module context where your web server reads your config. To resolve this without relying on the verbose workaround of passing flags every single time, you need to align the CLI environment with your desired memory settings.
The most reliable method to set a persistent limit for CLI operations is using the php.ini file specifically designated for CLI usage, often named php-cli.ini. In many XAMPP distributions, this file exists alongside the main web php.ini but might be commented out or missing entirely. If you create a new php-cli.ini in your XAMPP installation directory (e.g., xampp/php/php-cli.ini) and set the variable there:
memory_limit = 2048M
You must ensure this file is loaded before any CLI script runs. The syntax to force this file to load can be added to the main php.ini or simply by ensuring the CLI binary reads the specific file you configure. Alternatively, if you prefer not to manage multiple config files, you can modify the main php.ini but ensure the CLI is pointing to it. A common pitfall in XAMPP is that the Apache server runs with one PHP instance and the command line with another, potentially using a system-wide PHP version installed via Homebrew or macOS packages rather than the bundled XAMPP PHP.
To verify exactly which configuration file your CLI is reading, run the following command:
php -i | grep memory_limit
php --ini
The output of php --ini will show the "Loaded Configuration File" path. If you cannot find a dedicated CLI config there, creating one and adding it to the main php.ini via the zend.enable_static=Off or similar directives is sometimes necessary depending on the PHP version (7.x vs 8.x).
For Composer specifically, which is the tool causing frustration in your current workflow, you can either set the environment variable for your session or permanently adjust the global config. Setting the environment variable provides an immediate fix without touching files:
export MEMORY_LIMIT=2048M # Note: This sets a shell var, not the PHP ini value directly
# Correct approach via PHP flag for Composer specifically:
composer -d memory_limit=2048M install
However, the cleanest long-term solution remains configuring the php.ini correctly so that composer runs without flags. If you are using a shared hosting environment or a specific XAMPP version where modifying php.ini is restricted, wrapping your Composer commands in a script that sets the limit dynamically via -d memory_limit=2048M remains the most robust workaround until you can adjust the core configuration.
Understanding the distinction between the web server's PHP context and the CLI context is crucial here. Just because Apache starts successfully with 256M allocated doesn't mean your terminal commands respect that same limit. By ensuring your XAMPP installation uses a dedicated php-cli.ini or correctly pointing the main config to both contexts, you eliminate the need for manual flag passing every time you run a build process.