Running out of memory on Laravel 4 update

PECL out of memory with Composer and LaravelLaravel is great framework and I quite enjoy how it’s structured and how it works. In version 4 it was migrated from bundles to using Composer for dependency injection which is great and fits well in my daily routine on building apps also follows standards promoted by the PHP community in general.

In my application I was planning to add MongoDB support as first I’d like to improve the performance and second my data structure is perfect for this purpose, furthermore I want to improve my NoSQL skillset.

In this article I’ll describe the steps that I followed on installing the MongoDB provider, the problems that I’ve faced and final solutions and small tricks I used to make all work.

Updating Laravel and installing MongoDB

Installing MongoDB and the PHP driver

I’ll start first with MongoDB as that was the first step I did. So I am running a VPN with 1GB of RAM on Ubuntu distribution. That’s probably not important for now but it will provide some insights on the hurdles I had later on. Installing MongoDB on Ubuntu is straight forward you just need to run following commands:

Import MongoDB GP key needed to validate the package :

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

Create the repository file so APT knows where to look for the package:

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

Update the package repositories and install:

sudo apt-get update
sudo apt-get install mongodb-10gen

This should be enough and now you should have mongo running on your machine. You might need to start it, just run;

sudo service mongodb start

If you need to change any of the configurations of the service you can do it in /etc/mongodb.conf that’s where you can enable authentication as well.

The next step is to install the PHP driver so we can use it for connecting to the database. This is done through PECL and it has just a simple command:

sudo pecl install mongo

After that completes add “extension=mongo.so” to your php.ini file and restart apache.

So far all should be working if you type mongo in the console you should be connected to the MongoDB shell where you can list all databases and collections simply type show dbs and you should see current databases. To test the PHP driver you can run PHP in the console with php -a and than instantiate new class of type MongoClient(). If any of the steps above is not working you should resolve the issues before continuing forward.

Updating Laravel dependencies

Laravel can be updated through Composer all you have to do is run composer -vvv update in the root of your project. You might spot the verbose parameter I use it as at first I run that without it and the execution seemed as it has hung up though it turn out there are loads of 3rd party libraries to be updated and that is hendy so you can get some feedback on what’s going on. After a while I got an exception with message like: “ErrorException: proc_open(): fork failed – Cannot allocate memory in phar …

OK we’ve run out of memory. For Apache I have configured 128MB of memory which is fine as I have nothing big happening there. I found other people having same issue so I followed the top of MDNS Solution. That didn’t work for me as for CLI the php.ini for memory_limit was saying -1 which meant there is no limit. As I said above the server has 1GB of RAM and I don’t run anything apart of this application and a blog which means the update script of composer took more than 600/700MB of memory during installation.

Next solution was to either stop processes to gain some free memory or upgrade the server which would cost me some money. Though I found a solution the server didn’t have a swap file so all I had to do is add a swap file.

Adding Swap file on Ubuntu

First check if you have a SWAP file already added by running:

sudo swapon -s

You should see an empty list otherwise if you have record there that means you already have SWAP file set up but bare in mind this file still could be small to fit all data in the memory of that SWAP file. Next step it to create the file on the system with “dd” command:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k

After that create the swap file area for Linux and activate the file

sudo mkswap /swapfile
sudo swapon /swapfile

Next open the fstab file and add new line for our SWAP file.

sudo nano /etc/fstab/swapfile none swap sw 0 0

Thats all you’re ready to run the composer update command again and should work just fine.

Installing MongoDB provider for Laravel

There are different extensions out there but I picked this one on GitHub it has installation notes in the README file and it’s quite straight forward. Add to composer.json following line:

"require": {
    "jenssegers/mongodb": "*"
}

After that run composer update and add the provider class to the app configurations, configure the DB connection and that should be all.

Helpful links

It's only fair to share...Buffer this pageShare on Facebook0Tweet about this on TwitterShare on Google+0Share on LinkedIn0Share on Reddit0Pin on Pinterest0Email this to someone
About

Just a guy with strong interest in PHP and Web technologies

Posted in BASH, Laravel, MongoDB, PHP, Unix Tagged with: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*