Start up with Laravel 4 – Installation and Configuration Mac/Linux

Lately i thought of diving into Laravel which in recent days has gained quite a popularity among the developers community. I will cover the basic installation steps of getting Laravel setup to work efficiently on the machine along with the Homestead environment. The steps as per the Laravel website seems to be very easy but developer faces a lot of issues while getting it to work properly. So lets begin.

Introduction

Laravel is a PHP based framework used for developing great applications over web. It consists of various features similar to other frameworks but are very well organized compared to other competitors in the market.

Some of the features that it consists of are:

  • Restful Routing
  • Eloquent ORM
  • Blade templating engine
  • Built on top of various Symphony components.
  • Composer Support
  • Great community to help on various issues faced by developers over a period of time.

there are many more including the above ones…

Before we begin lets see what are the pr-requisites required for installing the framework successfully.

Pr-requisites

To install laravel, you need to have the following items already installed on your local machine:

  • PHP 5.3+ – Latest the better
  • MySQL
  • Apache/Nginx
  • Ruby – required for the composer to work appropriately.

Installation

To install laravel we will use Composer on our local machine to automate the process and make it easier.

What is Composer ?

Composer is a PHP based dependency manager which reduce the overload of installing dependencies while installing any framework or component. It more or less automates the process of installation and get the things to work efficiently.

Installing Composer

You can visit the Composer Website in order to install it over to your machine.

In case you are using MAC, I will suggest that you use HomeBrew to get things going as it makes it easier to setup things over the time.

Installing Laravel

Once you have the composer installed on your system, you can use the following command to install Laravel:

composer global require "laravel/installer=~1.1"

Memory Installation Issue

If you are able to get through this step at the first go consider yourself really lucky. More or less many developers come across the problem of Memory getting exhausted. They face some what the kind of error displayed below

Installing laravel/laravel (v4.0.6) - Installing laravel/laravel (v4.0.6)

Loading from cache Created project in /home/mysite/laravel Loading composer repositories with package information

Installing dependencies (including require-dev)

Fatal error:Allowed memory size of 100663296 bytes exhausted (tried to allocate 32 bytes) in phar:///usr

The reason for this kind of error occurs because when you ran the installation command, Composer tries to resolve the dependencies and accordingly download them in order to install Laravel successfully. So the question that comes up is how to resolve such kind of issue. There are couple of steps that are required. Lets see what are those steps in order to resolve such kind of an error.

  • PHP.ini
    Open the location where your php.ini is stored. Make sure you are altering the the correct one because it might be a possibility that your system consists of multiple versions of the file. You can use the following command to know the exact location over the terminal

php --ini

  1. Use a text editor or vi command on the terminal to edit the file.
  2. Find the following property in the config file : memory_limit
  3. Enhance the limit say to 512M
  4. Save the file and restart Apache to reflect the changes.
  5. You can cross check it by using php_info() function.
  • Update Composer

sudo composer self-update

This will allow the composer to update all the dependencies required for the installation so that the update process does not run in parallel with the installation process. This reduces the load to major extent and allows the framework to install successfully.

Post Installation Steps

Now there are a couple of ways to have a new installation for a Laravel Project.

  • Using Laravel as a command – I preferably use this option to get the things going. For this to work efficiently you need to perform few steps.
    • Open terminal.
    • Type echo $PATH – to print the path to command line.
    • Type in the following command:
      1. sudo vi .bash_profile
      2. At the end of the PATH variable add the following component ~/.composer/vendor/bin
      3. Save the file using :wq
      4. Close the instance of terminal and reopen it for changes to take effect.
      5. Type in laravel to see the options
      6. To create a new project, go the directory and type in the following
        • laravel new project-name
        • The above command will create a new directory with fresh installation of laravel.
  • Using Composer

You can use the following command to create a new project using composer also:

composer create-project laravel/laravel your-project-name --prefer-dist

Running Laravel

With PHP5.4+ above, you can launch your application from the command line on localhost:80 using the serve Artisan command:

php artisan serve

Homestead Setup

As by now, I assume that you are already done with the basic installation of laravel. So lets begin by setting up a virtual environment for the our applications.

Homestead is a pre-packaged Vagrant Box that provides the environment to run our applications on local environment.

Pr-requisites

Before we can install the homestead environment, we should have vagrant and virtualbox installed on the system. Its pretty easy installation so I dont think so that it requires any kind of explanation here. You just need to download the dmg files from the respective sites and install it as any other software.

Steps to install

  • Add the vagrant box to the environment

vagrant box add laravel/homestead

  • Use composer to install the homestead environment

composer global require "laravel/homestead=~2.0"

  • Add the ~/.composer/vendor/bin to .bash_profile as explained in the previous steps to make homestead as an executable command. If you have already added it for laravel you need not to do this again so you can ignore this step in such a case.
  • Homestead configuration resides in file known as Homestead.yaml
  • Use homestead init  command on terminal to create the file
  • You will be required to create a ssh key to make things work appropriately. Use the following command on your terminal to create one.

ssh-keygen -t rsa -C "you@homestead"

  • Now type homestead edit on the terminal to edit the file. Here are the following things that should be taken into consideration
  • Shared Folders: You will be required to map your folders that will be used inside virtual environment.
  • Site Mapping: This will be the path to your Laravel installation public folder.
    Now add the ip that is available under the Homestead.yaml file to your etc/hosts file on your main machine not your virtual machine.

192.168.10.10 homestead.app

  • Use vagrant up to launch your virtual machine from terminal.

Now you can access the application using your favourite browser as you access any other website.

Once you are done with all the steps you are all set to go for development. I hope you enjoyed the post and look forward for more.

Please leave your suggestions and comments in case the post helped you or in case you want me to improve on anything across.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s