Runtimes Advanced

Composer Memory Limit Exhausted on WSL2 Ubuntu: PHP CLI Troubleshooting Guide

Resolve 'Composer install memory limit exhausted' errors on Windows WSL2 Ubuntu. Learn to increase PHP CLI memory and optimize Composer for robust package management.

👨‍💻
Senior Systems Architect • Verified in Staging Labs

Resolve 'Composer install memory limit exhausted' errors on Windows WSL2 Ubuntu. Learn to increase PHP CLI memory and optimize Composer for robust package management.

When working with modern PHP applications, especially frameworks like Laravel or Symfony, Composer is an indispensable tool for dependency management. However, users running Composer within a Windows Subsystem for Linux 2 (WSL2) Ubuntu environment often encounter the frustrating "memory limit exhausted" error during composer install or composer update. This guide delves into the technical reasons behind this issue and provides a highly effective, step-by-step resolution strategy.

Symptom & Error Signature

The most common symptom is Composer failing to complete its operation, typically during the dependency resolution or package installation phase. You will observe an error message similar to the following in your terminal:

Loading composer repositories with package information
Installing dependencies from lock file (including require-dev)
Verifying lock file contents with composer.json (a2b3c4d5 vs e6f7g8h9)
Package operations: 150 installs, 0 updates, 0 removals
- Installing vendor/package (1.0.0): Extracting archive...
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in phar:///usr/local/bin/composer/src/Composer/DependencyResolver/Solver.php on line X

Or, more generically:

Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y bytes) in Unknown on line 0

This error explicitly points to PHP's memory_limit being too low for the Composer process to complete its task.

Root Cause Analysis

The "Allowed memory size exhausted" error signifies that the PHP CLI process, which Composer utilizes, has attempted to allocate more memory than permitted by its memory_limit configuration. Several factors contribute to this:

  1. Default PHP memory_limit: By default, PHP's memory_limit is often set to a conservative value (e.g., 128M or 256M) in php.ini. While sufficient for many web applications, Composer's dependency resolution algorithm, especially for projects with a large number of packages and complex dependency graphs, can consume significantly more memory. It needs to parse composer.json files, resolve transitive dependencies, and often cache package metadata, all of which are memory-intensive operations.

  2. Complexity of composer.json: Projects with many direct and indirect dependencies, or those using packages with complex version constraints, demand more memory from Composer. The larger and more intricate your vendor directory would be, the higher the memory consumption during resolution.

  3. Distinction between PHP CLI and FPM php.ini: It's crucial to understand that Composer runs as a PHP CLI (Command Line Interface) process. The php.ini file governing the CLI environment is separate from the php.ini used by PHP-FPM (FastCGI Process Manager) for web server execution (e.g., Nginx or Apache). Users often mistakenly modify the FPM configuration, which has no effect on Composer's memory limit.

  4. WSL2 Resource Allocation (Indirect): While WSL2 itself is a full Linux kernel environment, the underlying Windows host system manages its resource allocation. If your WSL2 instance is configured with a low maximum memory limit via .wslconfig, or if the Windows host is generally low on available RAM, this can exacerbate memory issues for any process, including Composer. However, for the specific "PHP Fatal error: Allowed memory size…" error, the primary bottleneck is almost always the PHP memory_limit configuration within the WSL2 Ubuntu environment.

Step-by-Step Resolution

The solution primarily involves increasing the PHP CLI's memory_limit. We'll also cover methods to override this setting directly for Composer and general Composer optimization.

1. Locate and Edit the PHP CLI php.ini Configuration

The first step is to identify the correct php.ini file used by your PHP CLI.

  1. Find the php.ini path: Open your WSL2 Ubuntu terminal and run:

    php -i | grep 'Loaded Configuration File'
    

    This command will output the full path to the php.ini file currently being used by the PHP CLI. It typically looks like /etc/php/{PHP_VERSION}/cli/php.ini, for example, /etc/php/8.1/cli/php.ini.

  2. Edit the php.ini file: Use your preferred text editor (e.g., nano or vim) to open this file. Remember to use sudo as it requires root privileges. Replace {PHP_VERSION} with the version identified in the previous step (e.g., 8.1).

    sudo nano /etc/php/{PHP_VERSION}/cli/php.ini
    

    Ensure you are editing the cli version of php.ini, not the fpm or apache2 version. Modifying the incorrect file will not resolve the Composer memory error.

  3. Adjust the memory_limit directive: Inside the php.ini file, locate the memory_limit directive. If it's commented out (starts with ;), uncomment it. Change its value to a significantly higher amount, or set it to -1 for unlimited memory (for CLI contexts this is generally safe for development environments).

    ; Maximum amount of memory a script may consume (128M)
    ; http://php.net/memory-limit
    memory_limit = 2G # Or -1 for unlimited in development CLI
    

    Setting it to 2G (2 Gigabytes) is usually more than sufficient for most Composer operations. Using -1 is convenient for development but should be avoided in production PHP-FPM environments. For CLI operations, the risk is minimal as the script terminates after execution.

  4. Save the file and exit: If using nano, press Ctrl+X, then Y to confirm saving, and Enter.

  5. Verify the change (no service restart needed for CLI): Unlike PHP-FPM or Apache, changes to the PHP CLI php.ini take effect immediately for new shell sessions or commands. You do not need to restart any services. Verify the new limit:

    php -r "echo ini_get('memory_limit');"
    

    This should now output 2G or -1.

2. Composer-Specific Memory Limit Override (Temporary or Scripted)

You can temporarily override the memory limit for a single Composer command without altering php.ini. This is useful for one-off operations or when you prefer not to change the global CLI setting.

  1. Using php -d: This is the most reliable method. It passes the memory_limit directive directly to the PHP interpreter running Composer.

    php -d memory_limit=-1 $(which composer) install
    # Or, if 'composer' is not in your PATH or you prefer the direct path:
    # php -d memory_limit=2G /usr/local/bin/composer install
    

    $(which composer) dynamically finds the path to your Composer executable. If it's not found, you might need to use the full path, commonly /usr/local/bin/composer.

  2. Using COMPOSER_MEMORY_LIMIT environment variable (Less Reliable/Common): Composer also supports a COMPOSER_MEMORY_LIMIT environment variable. However, this method is sometimes less effective or consistent across different Composer versions or PHP setups than php -d.

    COMPOSER_MEMORY_LIMIT=-1 composer install
    

3. Optimize Composer Operations

Beyond increasing memory, optimizing Composer itself can reduce its memory footprint and execution time.

  1. Install without development dependencies: For production builds or CI/CD pipelines, you often don't need development dependencies, which can significantly reduce the dependency graph size.

    composer install --no-dev
    
  2. Optimize the autoloader: After installing, optimize the autoloader for better performance. This might not directly reduce memory during installation, but it's a good practice.

    composer dump-autoload --optimize --no-dev
    
  3. Clear Composer cache: A corrupted or very large Composer cache can sometimes lead to unexpected issues. Clearing it can help.

    composer clear-cache
    
  4. Use --profile for diagnostics: If you're still facing issues, composer --profile install can provide insights into Composer's memory usage and execution time, helping you pinpoint bottlenecks.

    composer --profile install
    

4. Adjust WSL2 VM Memory Allocation (If System-Wide Resource Constraint)

If, after increasing the PHP memory_limit, you find your entire WSL2 instance becoming unresponsive or encountering other memory-related issues, it might indicate that the WSL2 VM itself is running out of allocated RAM from the Windows host. You can increase the memory allocated to your WSL2 VM.

  1. Create or edit .wslconfig: On your Windows machine, open your user profile directory (%UserProfile%), typically C:Users<YourUsername>. Create a file named .wslconfig (if it doesn't exist) or edit it.

    # .wslconfig
    [wsl2]
    memory=4GB # Adjust to a suitable value, e.g., 4GB, 8GB
    processors=4 # Number of virtual processors
    

    This file must be located in your Windows user profile directory (C:Users<YourUsername>). After making changes, you must shut down and restart the WSL2 environment for them to take effect.

  2. Shut down and restart WSL2: Open a Windows Command Prompt or PowerShell and run:

    wsl --shutdown
    

    Then, reopen your WSL2 Ubuntu terminal, and it will start with the new memory allocation.

By systematically applying these steps, you will effectively resolve the "Composer install memory limit exhausted" error on your Windows WSL2 Ubuntu environment, allowing for smooth and efficient PHP dependency management.

👨‍💻

Johnathon Wheeler

Senior Systems Architect & DevOps Engineer • Austin, TX

Connect on LinkedIn

Johnathon has over 16 years of hands-on experience designing, debugging, and scaling Linux web hosting stacks, container clusters, and high-availability database architectures. Every guide on ButItWorkedLocal is independently tested against Debian 12, Ubuntu 24.04/22.04 LTS, Rocky Linux, and Docker environments to guarantee reproducibility in production.

🛡️

Our Production Verification Guarantee

Encountering a bug not covered here or running a non-standard kernel configuration? Our solutions are continually refined against real production incidents. Submit an environment trace for our editorial team to replicate.