Getting started with Laravel 4 on Ubuntu – Installation and Configuration

After many years as a happy CodeIgniter PHP framework user, I have concluded that CodeIgniter has reached the end of its life. After some research about “modern” PHP frameworks, I concluded that Laravel is the future. Laravel borrows many of the great features of a variety of frameworks and builds a comprehensive and well-supported platform.

Here are the basic steps to get started with Laravel on Ubuntu. (This tutorial is based on Laravel 4.2 and Ubuntu 14.04, which are current as of this writing.)

  1. First, you need to make sure that you have the LAMP (an abbreviation for ‘Linux-Apache-MySQL-PHP’) server platform, including the curl utilities, Git version control, and OpenSSL, installed on Ubuntu. To do that, run this command in the terminal, which will automatically install all dependencies.

    sudo apt-get -y install php5 mysql-server mysql-client phpmyadmin php5-curl curl git openssl

    During the installation process, you’ll be prompted to specify a MySQL root account password. After the installation process completes, open your web browser and enter http://localhost/ in the address bar. You should see the “It works!” default page.

    Note: An alternative method to install LAMP server is to use the Ubuntu tasksel utility:

    sudo tasksel install lamp-server
    

    Check this article for additional details.

  2. The Composer dependency management tool that Laravel relies on requires that the PHP mcrypt extension module be enabled. The installation process in step #1 above will install it (the package named php5-mcrypt). However, it may not be enabled. To enable it, run this command in the terminal.

    sudo php5enmod mcrypt

    Now restart the Apache web server to load the mcrypt module:

    sudo service apache2 restart

    To confirm that the mcrypt module loaded, create a small PHP script in a text editor (I like Vim!) containing:

    <?php
       echo phpinfo();
    ?>
    

    Save this file to the root folder for Apache web server /var/www/html and name it phpinfo.php. Open your web browser and navigate to http://localhost/phpinfo.php. The browser should display a long report showing the details of PHP configuration on your system, including a section similar to this:

    php_mcrypt

    Note: Alternately, you can use the PHP command line -m option to check if mcrypt is enabled or not:

    php -m | grep mcrypt

    If mcrypt is enabled, this command should display a line with mcrypt on it.

  3. You may have observed that you need root permissions to save the phpinfo.php file to the /var/www directory. This is a bit inconvenient and we can relatively easily fix this. Instead of using the default Apache web server directory of /var/www, we’ll create a so-called Name-based Virtual Host which refers to our projects directory in our home directory as the “root” (base) folder for the web server. To create the projects directory, run this in the terminal:

    cd ~
    mkdir projects

    The cd ~ command is a Linux shortcut for changing to your home directory. (You could have also run cd $HOME instead!)

    Now, we need to create a new Apache “site” file for our virtual host.

    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/projects.local.conf

    Usually, you will want to give your “site” file a name similar to the domain name that you will use. In this case, we will set up a new local domain name of projects.local. Open the new projects.local.conf with a text editor. Remember that since it is a root, you must use sudo to launch the text editor. For example:

    sudo vim /etc/apache2/sites-available/projects.local.conf

    Replace the contents of the file with this.

    <VirtualHost projects.local>
    
    	ServerName projects.local
    	ServerAlias projects.local
    
    	ServerAdmin webmaster@localhost
    	DocumentRoot /home/username/projects
    	
    	<Directory /home/username/projects/>
    		Options -Indexes
    		DirectoryIndex index.php
    		Options Indexes FollowSymLinks
    		AllowOverride All
    		Require all granted
        </Directory>
    
    	ErrorLog ${APACHE_LOG_DIR}/error.log
    	CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    </VirtualHost>
    

    Make sure that you replace username in /home/username/projects with your actual user name. Also, ensure that the path name in the directive has a ‘/’ at the end. Save the file and close the text editor.

    We need to enable this new “site” configuration and restart the Apache web server to activate it.

    sudo a2ensite projects.local.conf
    sudo service apache2 restart

    Edit your /etc/hosts and add a line containing this:

    127.0.0.1	projects.local

    This allows you to use the host (domain) name alias projects.local in your web browser URLs. To test this, open your web browser and enter http://projects.local/ in the address bar. You should see the standard Apache “It works!” page, as above. Copy the phpinfo.php file that you created above to your ~/projects directory and point your web browser to http://projects.local/phpinfo.php which should display the phpinfo page.

  4. Now we are ready to install the Composer dependency management tool. In the terminal, enter these commands:

    cd ~/projects
    mkdir temp
    cd temp
    sudo curl -sS https://getcomposer.org/installer | sudo php

    This downloads and installs the Composer tool as a PHP phar package and installs it for you in the temp directory that you created.

    To simplify using Composer, we can make at available regardless of the directory that we are in by copying it to a directory in the environment $PATH. To do this, run:

    sudo mv composer.phar /usr/local/bin/composer

    You’ll notice that we have have removed the phar file extension for convenience. Now, to run Composer, we just need to enter composer followed by the appropriate Composer command and arguments/parameters. In many cases, you’ll see examples online that use the syntax php composer.phar; you can simply replace that with composer. (You can also delete the temp directory that you created, since it was only needed as a temporary place to install Composer.)

    To confirm that everything is working, just run composer in the terminal by itself. You should see something like this:

       ______
      / ____/___  ____ ___  ____  ____  ________  _____
     / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
    \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                        /_/
    Composer version e77435cd0c984e2031d915a6b42648e7b284dd5c 2014-07-02 15:44:54
    
    Usage:
      [options] command [arguments]
    
    

  5. Finally, we are ready to actually install the Laravel framework, after completing these preliminaries. The good news is that when creating projects in the future, we only need to run this series of commands!

    For this example, we’ll use a folder named laravel in the projects folder of our home directory. To install Laravel, navigate to your projects directory in the terminal and run:

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

    This will create a new installation of the latest (stable) version of Laravel in the laravel folder. This shows the power and convenience of Composer in that this single command does all of the work of downloading Laravel and its dependencies and installing them.

    You can learn more about the Composer commands and command-line options, but just a quick explanation about --prefer-dist, since you will frequently want to use it. This option tells Composer to use the compiled version of the package that you are installing, instead of compiling it on your local machine, if necessary.

    The next thing that we need to do is set the permissions of the Laravel app/storage folder:

    chmod -R 777 laravel/app/storage

    The last step in the configuration of Laravel is to modify the Laravel folder structure to remove the public folder, which removes public from the URL for the project for simplicity. To do this, in the laravel folder, run:

    mv public/* . && rm -rf public

    Open the index.php in the laravel folder in a text editor and replace the two occurrences of __DIR__.'/../bootstrap/autoload.php' with __DIR__.'/bootstrap/autoload.php'. Essentially, you are removing /.. from the path. Save the index.php file.

    We are ready to test the Laravel installation! Open your web browser and enter http://projects.local/laravel/ in the address bar. If everything is configured properly, you should see the “You have arrived.” page with the Laravel logo.

You have successfully installed Laravel. Next time, we’ll start building our first simple Laravel application.

Advertisements