Running composer update on a small VPS prints "PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted" or the process gets killed. How do I get it to finish?
DEV3 Developer Forum
Composer fails with 'Allowed memory size exhausted'. How do I fix it?
Every post is reviewed first. Usually reviewed within 24 hours.
1 reply
Composer's dependency solver can use a lot of RAM, especially during update with many packages. Options, from simplest to most robust:
1. Lift the PHP limit for Composer only
COMPOSER_MEMORY_LIMIT=-1 composer update
# or
php -d memory_limit=-1 /usr/local/bin/composer update
If you see "Killed" with no PHP message, the Linux OOM killer ended the process because the machine itself is out of RAM; a PHP setting cannot fix that.
2. Add swap on small servers
sudo fallocate -l 2G /swapfile && sudo chmod 600 /swapfile
sudo mkswap /swapfile && sudo swapon /swapfile
Add it to /etc/fstab to keep it after reboot.
3. Do not run update on the server
Run composer update locally or in CI, commit composer.lock, and on the server run only:
composer install --no-dev --optimize-autoloader
install reads the lock file and needs little memory because it does no dependency solving.
4. Reduce what the solver has to consider
- Update a single package:
composer update vendor/package --with-dependencies. - Remove unused packages and
*constraints. - Use Composer 2 (
composer self-update --2), which is much faster and lighter than version 1. - Check the PHP binary used:
composer diagnose,composer --version.
5. Prefer dist over source
Cloning git repositories uses memory and time. composer install --prefer-dist downloads zip archives. Install the zip and unzip tools so it does not fall back to git.
Pitfalls
- Memory problems in
composer requirefor Laravel on shared hosting: do it on your machine and uploadvendor/. - Dev tools like PHPStan consume memory too but with a different setting (
--memory-limit=1G). - If Composer says the PHP version is too old for a package, check
php -vfor the CLI; it can differ from the web PHP version.