keepcalculator

06 Numbers and Powers

Fix Composer Install Out of Memory on a VPS

Card No. 06·30340 · Updated · Standard formula, shown below

When running composer install or composer update on a VPS with limited RAM, you may hit a fatal error like Allowed memory size of 1610612736 bytes exhausted. This guide walks through the most effective fixes, from quick environment tweaks to permanent configuration changes, so you can install dependencies without upgrading your server plan.

What You’ll Need

  • SSH access to your VPS (root or a user with sudo privileges).
  • Your project’s composer.json and composer.lock files.
  • Basic familiarity with the command line.

Step-by-Step

  1. Check your current PHP memory limit.
    Run php -i | grep memory_limit or php -r 'echo ini_get('memory_limit');'. The default is often 128M or 256M. Composer recommends at least 512M for large projects.
  2. Increase the memory limit just for the Composer command.
    Use the COMPOSER_MEMORY_LIMIT environment variable. This is the quickest fix and doesn’t change any system files:
    COMPOSER_MEMORY_LIMIT=-1 composer install
    The -1 means “no limit�. Alternatively, set a specific value like 2G.
  3. Use the php -d flag to override memory_limit.
    If the environment variable doesn’t work (e.g., on Windows or certain shells), run:
    php -d memory_limit=-1 /usr/local/bin/composer install
    Adjust the path to your composer binary if needed.
  4. Edit your project’s composer.json to request more memory.
    Add a config section (if not present) with:

    "config": {
        "memory-limit": "-1"
    }

    This sets the memory limit for every Composer command run in this project.

  5. Increase the PHP memory limit globally (if you have root).
    Edit your php.ini file. Find it with php --ini. Look for memory_limit and change it to something like 512M or 1G. Restart PHP-FPM or Apache after saving.
  6. Run Composer with --no-dev to reduce memory usage.
    If you’re in production, skip development dependencies:
    composer install --no-dev --optimize-autoloader
    This often halves memory usage.
  7. Disable Xdebug (if installed).
    Xdebug significantly increases memory consumption. Temporarily disable it by renaming its ini file or using php -n to run without any extensions:
    php -n /usr/local/bin/composer install
    If that works, you’ll need to disable Xdebug permanently in your php.ini (comment out the zend_extension line).
  8. Use a swap file as a last resort.
    If your VPS has very little RAM, adding swap space can prevent OOM kills. As root:
    fallocate -l 1G /swapfile
    chmod 600 /swapfile
    mkswap /swapfile
    swapon /swapfile
    Add it to /etc/fstab to make it permanent.

Common Mistakes

  • Editing the wrong php.ini. There are often multiple PHP versions or CLI vs. FPM ini files. Always verify with php --ini.
  • Setting memory_limit to -1 in production. While fine for a one-off command, leaving it unlimited globally can cause memory leaks to crash your server.
  • Running Composer as root when not needed. This can cause file permission issues later. Use a user with appropriate permissions.
  • Forgetting to restart PHP-FPM after changing php.ini. Changes won’t take effect until the service restarts.

Tips

  • Use composer diagnose to check for common issues before debugging memory.
  • Keep your composer.lock file in version control. It ensures everyone installs the exact same dependency versions, which can also reduce memory usage because Composer doesn’t need to resolve versions.
  • Consider using composer install --prefer-dist to download packages as archives instead of cloning from Git. This uses less memory.
  • If you frequently hit memory limits, consider upgrading your VPS plan or using a Composer proxy like Packagist’s own CDN.

Troubleshooting

Still getting “Out of memory� after setting -1?
Check if you’re using a PHP version with a 32-bit integer limit. On 32-bit PHP, memory_limit cannot exceed 2GB. Use a 64-bit PHP build.

The command works with php -d but not with COMPOSER_MEMORY_LIMIT?
Some shells or CI systems don’t pass environment variables correctly. Use the php -d approach as a fallback.

Composer crashes with “Killed� and no error message?
This usually means the Linux OOM killer terminated the process. Check dmesg | tail -20 for confirmation. Add swap or increase RAM.

Memory limit is already high but Composer still fails?
The issue might be a specific package that requires more memory. Try composer install --no-scripts to skip post-install scripts that may be memory-hungry.

After changing php.ini, Composer still uses the old limit?
Make sure you’re editing the CLI php.ini, not the FPM one. Run php --ini to see which file is loaded.

Frequently Asked Questions

What is the recommended memory limit for Composer?

For most projects, 512M is enough. Large projects with many packages may need 1G or more. Setting it to -1 is safe for one-off commands.

Can I set the memory limit in composer.json permanently?

Yes, add "config": {"memory-limit": "-1"} to your composer.json. This applies to anyone running Composer in that project.

Why does Composer use so much memory?

Composer loads all package metadata and resolves dependencies in memory. Large dependency trees or packages with many versions can quickly consume hundreds of MB.

Is it safe to use COMPOSER_MEMORY_LIMIT=-1 on production?

For a one-off install, yes. But leaving it unlimited for all commands can cause memory exhaustion if a script has a bug. Prefer a finite value like 2G.

Will adding swap help Composer run faster?

No, swap is much slower than RAM. It only prevents crashes. If you need swap frequently, consider upgrading your VPS’s RAM.

Does --no-dev significantly reduce memory usage?

Yes, skipping development packages can reduce the number of packages by half or more, which directly lowers memory usage.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →