image Rio das Ostras, Brazil, 06/2010

Addressing Composer & Laravel Forge Memory Issues

<p>Composer, to calculate and resolve all dependencies the packages in your application, needs to use memory, lots of it sometimes, and you may experience differents errors while running <code class="spancode">composer update</code>. Here are the errors people usually see while updating Composer packages:</p> <pre class="prettyprint"><code >Allowed memory size of X bytes exhausted </code></pre> <p>and</p> <pre class="prettyprint"><code >ErrorException: proc_open(): fork failed - Cannot allocate memory in phar </code></pre> <p>The issues <a href="">945</a>, <a href="">1898</a> and <a href="">2704</a> in Composer's repo are about those errors.</p> <p>If are using <a href="">Laravel Forge</a> and have a 512MB box, you may experience those errors, due to the lack of swap memory:</p> <pre class="prettyprint"><code >forge@albatroz:~$ free total used free shared buffers cached Mem: 2049988 279064 1770924 17072 15296 119308 -/+ buffers/cache: 144460 1905528 Swap: 0 0 0 </code></pre> <p>To fix this we have some options:</p> <h2>Use Composer Install instead of Update</h2> <p>Those memory issues are usually related to the <code class="spancode">composer update</code> command, because it has to check all package versionsm, and map all dependencies, to decide what should be installed. But if you execute the update command <strong>in your development environment</strong>, commit the ´composer.lock´ file and pull it on your Forge box, you don't have to run update, you can just</p> <pre class="prettyprint"><code >composer install </code></pre> <p>And you should not have any kind of memory problem, because Composer will just have to download and install the packages listed in your ´composer.lock´, this is a good practice and also a very fast operation.</p> <h2>Delete the vendor folder before updating</h2> <p>If you delete the vendor folder and run a clean <code class="spancode">composer update</code>:</p> <pre class="prettyprint"><code >rm -rf vendor composer update </code></pre> <p>Composer will have a lot less to check and calculate, using a lot less memory, and it will also ignore the ´composer.lock´ file, creating a new one at the end of the installation.</p> <h2>Run Composer with no memory limit</h2> <p><a href="">PHP's default memory limit is 128M</a>, but having a 512MB box, you probably will be able to give more memory to PHP and Composer, so try to run with no limit:</p> <pre class="prettyprint"><code >php -d memory_limit=-1 /usr/local/bin/composer update </code></pre> <p>If it fixes the problem for you, you can edit your <code class="spancode">/etc/php5/cli/php.ini</code> file and raise it</p> <pre class="prettyprint"><code >memory_limit=256M </code></pre> <p>Unfortunately this usually doesn't fix it for the 512MB Forge boxes, because, by default they come with</p> <pre class="prettyprint"><code >memory_limit=512M </code></pre> <h2>Create swap space on your box</h2> <p>You can easily create <a href="">swap space (virtual memory)</a> in your box, this will waste some diskspace, but will probably fix the problem. Here are the Bash commands you can use to do that:</p> <p>Create a 2GB file to be used as your swap file:</p> <pre class="prettyprint"><code >sudo dd if=/dev/zero of=/swapfile bs=1024 count=2048k </code></pre> <p>Make it a swap file:</p> <pre class="prettyprint"><code >sudo mkswap /swapfile </code></pre> <p>Turn the swap on on your box:</p> <pre class="prettyprint"><code >sudo swapon -a </code></pre> <p>Add the a swap entry to your <code class="spancode">fstab</code>:</p> <pre class="prettyprint"><code >echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab </code></pre> <p>Tell the kernel to only use swap when RAM usage is around 80 or 90 percent. It will increase performance:</p> <pre class="prettyprint"><code >echo 10 | sudo tee /proc/sys/vm/swappiness echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf </code></pre> <p>Protect the swap file:</p> <pre class="prettyprint"><code >sudo chmod 0600 /swapfile </code></pre> <p>Reboot your box, and you now should see it:</p> <pre class="prettyprint"><code >forge@albatroz:~$ free -h total used free shared buffers cached Mem: 2.0G 279M 1.7G 16M 26M 116M -/+ buffers/cache: 136M 1.8G Swap: 2.0G 0B 2.0G </code></pre>

comments powered by Disqus