PHP fatal error when installing or updating dependencies with Composer

When installing or updating dependencies with Composer it may be possible that you see a PHP fatal error like:

This problem has been documented in Composer on issue https://github.com/composer/composer/issues/1898

This is common when the memory limit setting of PHP is not enough for Composer needs, something that can happen on Shared Hosting and smaller VPSs such as VPS100, VPS150, or VPS200.

If the memory limit setting is too low and there is available memory, Composer can be executed with different settings. For example, to use up to 256 MB:

Note: If Composer is running out of memory when being run from Capifony, try to add the following configuration to your deploy.rb file:

A better solution is to calculate the dependency tree locally. To install all the required packages, Composer needs to calculate the right versions of your project dependencies, as well as the ones that those packages depend on. This is a very expensive process but it is done only the first time or when we execute composer update. The results of this calculations are saved in a file called composer.lock, so when running composer install, this file will be used and the memory usage will decrease dramatically.

This will bypass the need to recalculate all the dependencies and generate the composer.lock file again. This also has the benefit of guaranteeing that you are installing the same version of the project dependencies as what you have on your development computer.

An alternative solution would be to use the Composer as a service tool, which accepts a composer.json file, makes all the calculations and gives you a zip file with all the actual required packages as well as the composer.lock file.