Laravel framework update issue
By Nethru Limited (www.nethru.com)
Laravel is one of the most popular PHP framework nowadays, and it surely is my first choice in doing projects using PHP. I am not writing about the advantages of using Laravel or teach you using it in this article here, instead, I am going to share with you an updating issue that I have solved recently.
One of the reason I choose Laravel is because it allows me to search and install different packages and add functionality to the Laravel framework (Packagist).
Recently, I found a useful package and try to add it to my Laravel project by editing the composer.json and use the composer update command to update the Laravel framework just like what I did before.
$ php composer.phar update
However, I got an error this time. Here is part of the error message I received.
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 71 bytes) in phar:///usr/local/bin/composer.phar/src/Composer/DependencyResolver/RuleWatchGraph.php on line 47
After spending some time searching in Google and testing by myself, I found that the reason is due to the memory limit in PHP. If I try to add many packages or add a very large package to Laravel, the update process may exceed the memory limit of PHP and the update process will be failed.
Solution 1 – Upgrade your PHP version to 5.4.19 or newer
Actually this cannot be called a “solution”, it may have some help but it cannot completely solving the issue. Since PHP optimized the memory usage in 5.4.19 release, upgrading the PHP version can let the Laravel update process has better usage of the memory. But there may still have a chance that the update process exceed the memory limit.
Solution 2 – Increase the memory limit of PHP
By default, PHP limits memory usage to 128M by the parameter
memory_limit. Edit this setting in PHP may solve the problem. There are three ways to change its value.
Way 1: Edit
php.ini and change the value of
memory_limit to what size you want (-1 means no limit) and then restart your Apache.
(#Caution: This changes will apply to all your PHP process, think carefully if you really want to do this)
Way 2: Find the
.htaccess in your root directory of the specified domain, if there isn’t, create one and put the following line in it.
php_value memory_limit 128M ; Change the 128M to your needs, -1 is no limit
(#Caution: This changes will apply to all your PHP process of a specified domain, same as way 1, think carefully if you really want to do this)
Way 3 (Best way): Change at run-time using the following command for the Laravel process.
$ php -d memory_limit=-1 composer.phar update
Solution 3 – Increase the Linux swap size
In most cases, “solution 2” can already solve the problem. But if your server has small size of memory and setting the
memory_limit value still cannot solve the issue, you may have to increate the swap size of your Linux server. Here is the steps.
1. Create a 1G swap file.
$ dd if=/dev/zero of=/extraswap bs=1M count=1024
2. Format the created swap file.
$ mkswap /extraswap
3. Setup correct file permission for security reasons
$ chown root:root /extraswap $ chmod 0600 /extraswap
4. Activate new swap space immediately
$ swapon /extraswap
5. Activate new swap space after system reboot, append the following line to
/extraswap swap swap defaults 0 0
To check the swap usage, use this command.
$ swapon -s
Nowadays, memory size of most server is already large enough, “solution 2” can solve the problem in most cases. My suggestion for solving this issue is trying “solution 2” first. If you still got the error, then use “solution 3”.