How to Install Laravel 5.5 on Ubuntu 16.04 with Apache

6

If you are reading this then you already know what the heck is Laravel. To wrap it up in a single line, it is one of the best PHP framework available right now with MVC(Model-View-Controller) Architecture.

You should learn Laravel if you already are familiar with the Core PHP. This is because while developing large projects there are several issues like update management, code management and much more.

A framework provides a way to do separate things in different ways making it easier to develop and update. Some of the things I really liked about Laravel are inbuilt authentication system, easy validations, and Eloquent model.

Install Laravel on Ubuntu

There are several tutorials available on the internet for installing but while following them I was unable to setup laravel completely. So I collected all the errors and googled to fix them and wrote them one by one and compiled this tutorial.

So without any further due let’s jump on to installing laravel.

Steps to Install Laravel 5.5 on Ubuntu 16.04 with Apache

If you are developing a Laravel application then I will strongly recommend you to develop it using Ubuntu instead of using Windows. Because to be honest you will not learn everything on windows like you do on Ubuntu. And to be fair when you deploy your application on the server it will be Ubuntu, not Windows.

In my case,  I prefer using Virtual Machine to develop an application and here I am using VMWare Workstation to setup the virtual laravel development environment.

You can skip the next step if you are not interested in creating a virtual machine. You can also install it on a physical Ubuntu machine.

Installing Ubuntu on VMWare Workstation

  1. Click Create a New Virtual Machine. Select the Ubuntu iso file. (In my case it was Ubuntu 16.04 x64 64-bit)VMWare Workstation Create Virtual Machine 1
  2. Follow the on screen instructions and enter user name and password for the new Ubuntu Installation.VMWare Workstation Create Virtual Machine 2
  3. Here are the configurations for my Ubuntu virtual machine.VMWare Workstation Configuration
  4. Boot up the Ubuntu and wait for the installation to complete.Ubuntu Login

Updating the Ubuntu OS

Ubuntu Update

Before we begin installing Laravel it is better to check for updates for modules and Linux distribution.

  • sudo apt-get update to fetch the latest updates.
  • sudo apt-get dist-upgrade to upgrade modules and distribution.

Installing Required Packages for Laravel

There are several requirements of laravel that are listed on the official website of Laravel. But here are all the packages that you will need to deploy a successful laravel installation.

sudo apt-get install php-dev php-mcrypt php-gd php-mbstring php-xml php-common php-zip apache2-dev libapache2-mod-php mysql-server composer npm

Note: During MySQL Server installation you will be prompted to choose a password for the root user. Enter the desired password and remember it because you will need it while creating a connection between the Laravel app and the MySQL server.

The above command will install all of the packages that you will ever need for a Laravel installation. Here is a brief overview about what these packages are for.

  • php-dev: PHP Development Package
  • php-mcrypt: PHP mcrypt to encrypt the data
  • php-gd: It can also be used to create and manipulate image files like GIF, PNG, JPEG, WBMP, and XPM
  • php-mbstring: express more than 256 characters in the regular bytewise coding system
  • php-xml: PHP XML Support
  • php-common: Common PHP Modules
  • php-zip: PHP Zip support
  • apache2-dev: Apache Development Package
  • libapache2-mod-php: PHP Execution
  • mysql-server: MySQL Database Server
  • composer:  Tool for dependency management in PHP
  • npm: Package manager for JavaScript

Installing Laravel

Now you are ready to fire up the laravel installation on your Ubuntu system. Type the below command to get installation started

composer global require “laravel/installer”

Wait for the installation to complete(Here Laravel 5.5). But wait when you type laravel in the terminal you will get command not found error. Here is how to fix that.

  1. Type nano .bashrc
  2. Add this line at top of the file:
    export PATH=”$PATH:$HOME/.config/composer/vendor/bin”
  3. Fire up source ~/.bashrc

Now type laravel in the terminal and boom! you are ready to roll.

Creating New Laravel App

To create a new laravel app you just simply need to type:

laravel new {appname}

Here we have installed the new app in /home/{username}/{appname}

Now you can fire up the php artisan serve and go to localhost:8000 to see laravel in action. But we are not going to do that because instead of this temporary server we are going to configure apache2 because it is a robust solution.

Laravel ENV Error

If you see an error like ‘error something went wrong’ then here is what to do.

  1. Go to the laravel app folder and rename .env.example to .env.
  2. cd {yourapplocation}
  3. Type php artisan key:generate to generate the encryption key for the app.
  4. Reload localhost:8000

If the error is not yet fixed then enable the debug option from {yourapplocation}/config/app.php

Configuring the Apache Server

By default localhost serve /var/www/html. But we want it to display the content of {yourapp}/public content. So we will need to modify Apache Virtual Host file to setup everything correctly:

  1. Open Apache default configuration file by typing: sudo nano /etc/apache2/sites-available/000-default.conf
  2. Change DocumentRoot /var/www/html
    to DocumentRoot /{yourapplocation}/public
  3. Add the following lines below the edited line.
    <Directory /{yourapplocation}/public>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
    </Directory>

If you are using SSL with your server and application then make the same changes in /etc/apache2/sites-available/default-ssl.conf

To make sure that Apache has enabled the required modules for laravel type the commands given below:

sudo a2enmod rewrite
sudo a2enmod php7.0

Laravel App HomepageIt will enable the mod-rewrite and PHP module for the Apache server. Now all you need to do is to reboot the server so that the changes can take place. Type sudo service apache2 restart to restart the Apache server.

Open localhost or 127.0.0.1 to check whether laravel app is working or not.

Some Extra Things to Take Care Of

Although steps in this section are not necessary. We will highly recommend you to perform these before beginning the development of the application.

Fixing File Permission

If there is ever a permission error fire up the following commands.

sudo chmod -R 755 /{yourapplocation}
sudo chmod -R 777 {yourapplocation}/storage

Updating NodeJS & npm to Latest Version

As far I recall I didn’t have the latest version of nodejs & npm after performing all of the above steps and that caused errors with some dependencies in my project so here is what to do: (This steps will install the latest version of nodejs and npm required for Laravel 5.5)

  1. Remove NodeJS and npm completely using sudo apt-get purge nodejs npm.
  2. Select the Appropriate Node Version and FireUp any of the following three commands:
    sudo curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash –
    or
    sudo curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash –
    or
    sudo curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash –
  3. Install the latest version of both packages using sudo apt-get install -y nodejs npm

Installing Laravel Composer Dependencies

Type the following commands to install and update the composer dependencies of your Laravel app.

composer install
composer update

So that was all. I hope that you got your laravel installation working on the first go. If not the let us know where you are stuck using the comment section below. We would be glad to help you out.

6 COMMENTS

  1. This is one hell of an article on laravel.
    Currently, I am running laravel in windows but I am going to give it a try and develop in Ubuntu.
    This clears all doubt on how to install laravel in Ubuntu.

    Thanks for the article.

  2. Are you sure you need to put both nodejs and npm in the sudo apt-get install -y ? I think just nodejs is fine, in fact its an error for me if I tried to install npm like that…

    • Yeah Bengotek, You are absolutely correct. But if you are in development environment then no need to worry.

      Actually today I deployed laravel to production server without performing this step.

      It’s optional. Let me know what you think.

      • I’ve done the apache2 part exactly like you said ( except its php7.1 not 7.0), however when I went to localhost, the default ubuntu welcome page was still shown instead of the laravel page, I’ve changed the 000-default.conf file to:

        #DocumentRoot /var/www/html
        DocumentRoot ~/laravelproject/public

        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted

        I’ve restarted the apache2 service too.

        • That’s impossible. To verify you can go-to var/www/html and modify the index file to check whether Apache is still using that folder or not. I will be able to resolve the problem better if we communicate directly via Skype or something. [email protected]

LEAVE A REPLY

Please enter your comment!
Please enter your name here