Introduction

You're running Composer to install or update PHP dependencies, but the command fails with a memory exhaustion error. Composer runs out of the allowed PHP memory while resolving dependencies, downloading packages, or generating the autoloader. The process terminates mid-operation, leaving your dependencies partially installed.

Composer is a PHP application itself, so it's subject to PHP's memory_limit configuration. When your project has many dependencies or complex version constraints, Composer needs significant memory to resolve the dependency graph.

Symptoms

Composer memory errors appear in several formats:

```bash # Direct memory exhausted error $ composer install Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/bin/composer/src/Composer/DependencyResolver/RuleSetGenerator.php on line 128

# With debug info $ composer update PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /usr/share/php/Composer/DependencyResolver/Solver.php on line 0

# Check version shows memory limit $ composer -V Composer version 2.6.5 2023-10-06 10:11:52 PHP version 8.2.12 Memory limit: 128MB

# During require $ composer require laravel/framework Using version ^10.0 for laravel/framework ./composer.json has been updated Running composer update laravel/framework Loading composer repositories with package information Updating dependencies Fatal error: Allowed memory size of... bytes exhausted

# On shared hosting $ php composer.phar install Fatal error: Allowed memory size of 67108864 bytes exhausted

# When generating autoloader $ composer dump-autoload -o Generating optimized autoload files Fatal error: Allowed memory size of... bytes exhausted

# With profile flag showing memory usage $ composer --profile install [6.8MiB/104.3MiB] Memory usage: 104.3MiB (peak: 156.2MiB), time: 45.2s Fatal error: Allowed memory size of 128MiB exhausted

# Composer diagnosis shows issue $ composer diagnose Checking platform settings: OK Checking http connectivity: OK Checking composer.json: OK Checking composer.lock: OK Checking pubkeys: FAIL Missing pubkey for tags verification Checking memory limit: FAIL Memory limit below 512MB (current: 128MB) ```

Additional symptoms: - Composer works for small projects but fails for large ones - composer update fails but composer install works - Process hangs then dies - Works locally but fails on CI/CD - Only specific packages trigger memory issues - First install works, update fails later

Common Causes

  1. 1.PHP memory_limit Too Low: The default PHP memory_limit (often 128MB) isn't enough for Composer to resolve complex dependency graphs with many packages. Large frameworks like Laravel or Symfony with many dependencies can require 512MB+.
  2. 2.Complex Dependency Resolution: Your project has many packages with intricate version constraints, aliases, or conflicts. Composer's SAT solver needs more memory to find a valid solution.
  3. 3.Large Number of Dependencies: Projects with hundreds of dependencies require more memory to load and process all package metadata simultaneously.
  4. 4.Memory Leaks in Older Composer: Older versions of Composer (1.x) had memory leaks that caused excessive memory usage during long operations.
  5. 5.CI/CD Environment Constraints: Continuous integration servers often have strict memory limits that differ from your local development environment.
  6. 6.Shared Hosting Restrictions: On shared hosting, PHP memory_limit is often set low (64-128MB) with no ability to change it via php.ini.
  7. 7.Plugin Memory Overhead: Custom Composer plugins add memory overhead during install/update operations.
  8. 8.Huge composer.lock File: Very large lock files with thousands of packages require more memory to parse and validate.

Step-by-Step Fix

  1. 1.Check logs for specific error messages
  2. 2.Verify configuration settings
  3. 3.Test network connectivity
  4. 4.Review recent changes
  5. 5.Apply corrective action
  6. 6.Verify the fix

Step 1: Check Current Memory Limit

Determine what memory limit PHP and Composer are using.

```bash # Check PHP CLI memory limit php -i | grep memory_limit php -r "echo ini_get('memory_limit') . PHP_EOL;"

# Check Composer's memory limit composer -V

# Run Composer diagnosis composer diagnose

# Check available system memory free -h

# Check PHP configuration file location php --ini

# Check all PHP memory-related settings php -i | grep -i memory

# Check for multiple PHP versions which php ls -la /usr/bin/php* update-alternatives --display php 2>/dev/null || true

# Check if Composer is using the same PHP which composer head -1 $(which composer) 2>/dev/null || true

# Test current memory limit php -r " \$limit = ini_get('memory_limit'); \$bytes = return_bytes(\$limit); echo \"Memory limit: \$limit (\" . number_format(\$bytes) . \" bytes)\\n\"; function return_bytes(\$val) { \$val = trim(\$val); \$last = strtolower(\$val[strlen(\$val)-1]); \$val = (int)\$val; switch(\$last) { case 'g': \$val *= 1024; case 'm': \$val *= 1024; case 'k': \$val *= 1024; } return \$val; } " ```

Step 2: Increase Memory Limit via Command Line

The quickest fix - set memory limit for the current Composer command.

```bash # Option 1: Use COMPOSER_MEMORY_LIMIT environment variable COMPOSER_MEMORY_LIMIT=-1 composer install

# -1 means unlimited memory

# Or set a specific limit: COMPOSER_MEMORY_LIMIT=2G composer install

# Option 2: Use php -d flag php -d memory_limit=-1 /usr/local/bin/composer install

# Or with specific limit: php -d memory_limit=2G /usr/local/bin/composer install

# If using composer.phar: php -d memory_limit=-1 composer.phar install

# For global Composer: COMPOSER_MEMORY_LIMIT=-1 composer global update

# Create an alias for convenience: echo 'alias composer="COMPOSER_MEMORY_LIMIT=-1 composer"' >> ~/.bashrc source ~/.bashrc

# Or for PHP memory: echo 'alias composer="php -d memory_limit=-1 /usr/local/bin/composer"' >> ~/.bashrc source ~/.bashrc

# Test the fix: COMPOSER_MEMORY_LIMIT=-1 composer --profile install # Check memory usage in output ```

Step 3: Change PHP Configuration Permanently

Update PHP configuration to increase memory limit permanently.

```bash # Find PHP configuration file php --ini

# Output shows: # Configuration File (php.ini) Path: /etc/php/8.2/cli # Loaded Configuration File: /etc/php/8.2/cli/php.ini

# Edit the CLI php.ini: sudo nano /etc/php/8.2/cli/php.ini

# Find memory_limit line: # memory_limit = 128M

# Change to: # memory_limit = 2G # Or for unlimited: # memory_limit = -1

# For Ubuntu/Debian with multiple PHP versions: sudo nano /etc/php/8.1/cli/php.ini sudo nano /etc/php/8.2/cli/php.ini

# For CentOS/RHEL: sudo nano /etc/php.ini

# Verify the change: php -i | grep memory_limit

# If separate web and CLI configs, edit CLI: ls /etc/php/*/cli/conf.d/ # Create custom ini: echo "memory_limit = 2G" | sudo tee /etc/php/8.2/cli/conf.d/99-memory.ini

# Reload PHP (if needed): sudo systemctl restart php8.2-fpm

# Test: composer -V # Should show new memory limit ```

Step 4: Configure Composer Environment Variables

Set Composer-specific environment variables in your shell profile.

```bash # Add to ~/.bashrc or ~/.bash_profile: nano ~/.bashrc

# Add these lines: export COMPOSER_MEMORY_LIMIT=-1 # Or specific limit: # export COMPOSER_MEMORY_LIMIT=2G

# Also useful Composer settings: export COMPOSER_PROCESS_TIMEOUT=600 export COMPOSER_NO_INTERACTION=1 export COMPOSER_DISABLE_XDEBUG_WARN=1

# Apply changes: source ~/.bashrc

# Or for zsh: nano ~/.zshrc # Add same lines source ~/.zshrc

# Create a .env file for project-specific settings: nano .env

# Add: COMPOSER_MEMORY_LIMIT=2G

# Load before running: export $(cat .env | xargs) && composer install

# For CI/CD (GitHub Actions): # In workflow YAML: ```

yaml
env:
  COMPOSER_MEMORY_LIMIT: -1
bash
# For GitLab CI:
# In .gitlab-ci.yml:
yaml
variables:
  COMPOSER_MEMORY_LIMIT: "-1"
bash
# For Jenkins pipeline:
# In Jenkinsfile:
environment {
    COMPOSER_MEMORY_LIMIT = '-1'
}

Step 5: Optimize Composer Operations

Reduce memory usage by optimizing how Composer runs.

```bash # Use install instead of update when possible composer install

# install uses lock file - less memory needed # update resolves all dependencies - more memory needed

# Use --no-dev for production composer install --no-dev

# Skip scripts if not needed composer install --no-scripts

# Use prefer-dist (downloads zip, less memory than git clone) composer install --prefer-dist

# Clear Composer cache before large operations composer clear-cache

# Remove vendor and reinstall fresh: rm -rf vendor/ composer install

# Use --prefer-stable for less resolution: composer require package/name --prefer-stable

# Update single package instead of all: composer update vendor/package

# Check for memory-hungry plugins: composer show | grep composer-plugin

# Disable plugins temporarily: composer install --no-plugins

# Use Composer 2.x (better memory management): composer --version # If 1.x, upgrade: composer self-update --2

# Optimize autoloader separately: composer install --no-autoloader composer dump-autoload --optimize

# Use parallel downloads (Composer 2.x): composer install --no-ansi --no-progress 2>/dev/null

# Profile memory usage: composer --profile update ```

Step 6: Handle Large Projects Efficiently

Special techniques for projects with many dependencies.

```bash # Split installation into stages: # First, install without dev dependencies: composer install --no-dev --no-scripts

# Then separately run scripts: composer run-script post-install-cmd

# Install packages in batches: composer require vendor/package1 vendor/package2 composer require vendor/package3 vendor/package4

# Use require one by one for very large projects: for pkg in $(cat packages.txt); do COMPOSER_MEMORY_LIMIT=-1 composer require $pkg done

# Reduce dependencies: composer why-not php 8.2 composer outdated

# Remove unused packages: composer remove --unused

# Analyze what's using memory: composer show --tree

# Check for circular dependencies: composer check-platform-reqs

# Use a lock file from another machine: # Copy composer.lock from machine where it worked scp other-machine:project/composer.lock ./ composer install

# Disable Xdebug (major memory consumer): php -m | grep xdebug # If present, disable: sudo phpdismod xdebug # Or use: php -n -d memory_limit=-1 /usr/local/bin/composer install # -n disables all extensions

# Check autoload size: composer dump-autoload --optimize du -sh vendor/composer/ ```

Step 7: Add Swap Memory for Low-RAM Servers

Create swap space when physical RAM is insufficient.

```bash # Check current swap: free -h swapon --show

# If no swap or insufficient, add swap: # Create 2GB swap file: sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile

# Verify: free -h swapon --show

# Make permanent: echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# Optimize swap usage: sudo sysctl vm.swappiness=10 echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

# Now run Composer: COMPOSER_MEMORY_LIMIT=-1 composer install

# For Docker containers, add swap in docker-compose: ```

yaml
services:
  app:
    # ...
    deploy:
      resources:
        limits:
          memory: 1G
        reservations:
          memory: 512M
bash
# Or run Docker with more memory:
docker run --memory=2g --memory-swap=4g your-image

Step 8: Fix Shared Hosting Memory Issues

Handle Composer on restrictive shared hosting.

```bash # On shared hosting, you often can't change php.ini # Try .user.ini in project root: echo "memory_limit = 512M" > .user.ini

# Or .htaccess (if Apache): echo "php_value memory_limit 512M" >> .htaccess

# Use a custom php.ini: cp /etc/php.ini ~/php.ini echo "memory_limit = 512M" >> ~/php.ini php -c ~/php.ini composer.phar install

# Use CGI instead of CLI if available: # Create wrapper script: cat > composer.sh << 'EOF' #!/bin/bash php -d memory_limit=512M composer.phar "$@" EOF chmod +x composer.sh ./composer.sh install

# Download Composer locally: curl -sS https://getcomposer.org/installer | php -d memory_limit=512M php -d memory_limit=512M composer.phar install

# If all else fails, run Composer locally and upload: # On local machine: composer install # Upload vendor/ folder to server

# Or use Composer on your dev machine: composer update git add composer.lock git commit -m "Update dependencies" git push # On server: git pull composer install --no-dev --no-scripts ```

Step 9: Upgrade Composer and PHP

Use newer versions with better memory management.

```bash # Check current versions: composer --version php --version

# Upgrade Composer to latest: composer self-update # Or specific version: composer self-update 2.6.5

# If self-update fails: php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php --install-dir=/usr/local/bin --filename=composer php -r "unlink('composer-setup.php');"

# Upgrade PHP (Ubuntu/Debian): sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt install php8.2-cli

# Switch PHP version: sudo update-alternatives --config php

# Check Composer works with new PHP: composer diagnose

# Verify memory: php -i | grep memory_limit

# Composer 2.x improvements: # - Better memory efficiency # - Parallel downloads # - Smarter dependency resolution

# Check if using Composer 2: composer --version | grep "Composer version 2" ```

Step 10: Create Monitoring and Prevention

Set up checks to prevent memory issues.

```bash # Create pre-install check script: cat > scripts/check-composer-memory.sh << 'EOF' #!/bin/bash # Check if system can run Composer

MIN_MEMORY_MB=512 PHP_MEMORY=$(php -r "echo ini_get('memory_limit');" | sed 's/[^0-9]//g') SYS_MEMORY=$(free -m | awk '/^Mem:/{print $2}') AVAILABLE_MEMORY=$(free -m | awk '/^Mem:/{print $7}')

echo "PHP memory_limit: ${PHP_MEMORY}MB" echo "System memory: ${SYS_MEMORY}MB" echo "Available memory: ${AVAILABLE_MEMORY}MB"

if [ "$PHP_MEMORY" != "-1" ] && [ "$PHP_MEMORY" -lt "$MIN_MEMORY_MB" ]; then echo "WARNING: PHP memory_limit is too low for large projects" echo "Consider running: COMPOSER_MEMORY_LIMIT=-1 composer install" fi

if [ "$AVAILABLE_MEMORY" -lt 256 ]; then echo "WARNING: Low available memory" echo "Consider adding swap or closing other applications" fi

# Check for Xdebug if php -m | grep -q xdebug; then echo "WARNING: Xdebug is enabled, this will increase memory usage" echo "Consider disabling: phpdismod xdebug" fi

# Check Composer version COMPOSER_MAJOR=$(composer --version 2>/dev/null | grep -oP 'version \K[0-9]+' | head -1) if [ "$COMPOSER_MAJOR" == "1" ]; then echo "WARNING: Composer 1.x has worse memory management" echo "Consider upgrading: composer self-update --2" fi EOF

chmod +x scripts/check-composer-memory.sh

# Run before install: scripts/check-composer-memory.sh

# Add to composer.json scripts: cat > composer.json << 'EOF' { "scripts": { "pre-install-cmd": ["scripts/check-composer-memory.sh"], "check-memory": ["scripts/check-composer-memory.sh"] } } EOF

# Run: composer check-memory

# Add to CI/CD pipeline: # .github/workflows/ci.yml ```

```yaml - name: Check memory run: scripts/check-composer-memory.sh

  • name: Install dependencies
  • env:
  • COMPOSER_MEMORY_LIMIT: -1
  • run: composer install --prefer-dist --no-progress
  • `

```bash # Create Makefile for consistent commands: cat > Makefile << 'EOF' .PHONY: install update test

install: COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-scripts composer dump-autoload --optimize

update: COMPOSER_MEMORY_LIMIT=-1 composer update --prefer-dist

test: php -d memory_limit=512M vendor/bin/phpunit EOF

# Use: make install ```

Checklist for Fixing Composer Memory Issues

StepActionCommandStatus
1Check current memory limit`php -i \grep memory_limit`
2Increase via command lineCOMPOSER_MEMORY_LIMIT=-1 composer install
3Change PHP config permanentlyEdit /etc/php/*/cli/php.ini
4Set environment variablesAdd to ~/.bashrc
5Optimize Composer operationsUse --no-dev, --prefer-dist
6Handle large projectsSplit installation, clear cache
7Add swap memoryfallocate, mkswap, swapon
8Fix shared hosting issuesUse .user.ini, .htaccess
9Upgrade Composer and PHPcomposer self-update --2
10Create monitoring scriptsAdd check scripts

Verification

After fixing Composer memory issues:

```bash # 1. Memory limit is sufficient php -i | grep memory_limit # Should show >= 512M or -1

# 2. Composer diagnosis passes composer diagnose # All checks should be OK

# 3. Install succeeds COMPOSER_MEMORY_LIMIT=-1 composer install # Should complete without errors

# 4. Profile shows reasonable memory composer --profile install # Memory usage should not exceed limit

# 5. Large update works composer update # Should not exhaust memory

# 6. Xdebug disabled php -m | grep xdebug # Should not show xdebug

# 7. Using Composer 2.x composer --version # Should show version 2.x

# 8. Swap available (if added) free -h # Should show swap space

# 9. Environment variable set echo $COMPOSER_MEMORY_LIMIT # Should show -1 or value

# 10. CI/CD pipeline works # Run your CI pipeline ```

  • [Fix PHP Composer Dependency Conflict](/articles/fix-php-composer-dependency-conflict) - Version conflicts
  • [Fix PHP Memory Limit Exhausted](/articles/fix-php-memory-limit-exhausted) - General PHP memory
  • [Fix PHP-FPM Pool Exhausted](/articles/fix-php-fpm-pool-exhausted) - PHP-FPM memory
  • [Fix Symfony Cache Corrupted](/articles/fix-symfony-cache-corrupted) - Symfony issues
  • [Fix Laravel Migration Locked](/articles/fix-laravel-migration-locked) - Laravel database
  • [Fix WordPress Fatal Error](/articles/fix-wordpress-fatal-error-uncaught) - WordPress errors
  • [Fix Magento 2 Indexer Stuck](/articles/fix-magento-2-indexer-stuck) - Magento issues
  • [Technical troubleshooting: Fix Composer Autoload Classmap Not Updating After ](composer-autoload-classmap-not-updating-after-new-class)
  • [Technical troubleshooting: Fix Php Composer Platform Php Version Mismatch Fix](php-composer-platform-php-version-mismatch-fix)
  • [Technical troubleshooting: Fix composer platform requirement php version mism](composer-platform-requirement-php-version-mismatch-fix)
  • [Fix Curl SSL Certificate Problem Unable To Get Local Issuer Issue in PHP](curl-ssl-certificate-problem-unable-to-get-local-issuer)
  • [Fix Drupal Module Update Failed Breaking Site](fix-drupal-module-update-failed)

<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Fix PHP Composer Out of Memory Error", "description": "Learn how to fix PHP Composer out of memory errors with memory_limit configuration, environment variables, and swap memory setup. Step-by-step guide.", "url": "https://www.fixwikihub.com/fix-php-composer-out-of-memory", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2025-12-14T14:34:59.663Z", "dateModified": "2025-12-14T14:34:59.663Z" } </script>