Call + (44) 0843 289 4539

How to install Laravel 5.6 on Ubuntu 18.04

Learning and understanding Laravel, the PHP framework for web artisans can greatly improve your PHP development experience and actually make it fun, by concentrating on the things that matter most. Laravel takes care of all the boring lower value cruft work a developer has to go through just to get a typical site up and running

It really is an awesome framework, however, for a new developer to get it installed can be somewhat intimidating and there are so many tools and components that come along with it, that things can get a little confusing.

Although the documentation on Laravel is quite extensive and can really help to get you working with the framework, Ironically we have found that the documentation to get it all installed and working can be a little confusing and in some areas lacking.

In this post you learn how to install Laravel on Ubuntu 18.04 to get up and running quickly and get started building your new exciting product quickly! .

Install PHP 7.2 on ubuntu 18.04

Installing PHP 7 is not essential if you are going to make use of Homestead environment. However, it is always useful to have PHP installed on your development machine or laptop. In my case, I often use it to quickly develop utility scripts. I may also have several other PHP software development frameworks installed.

We are going to install Laravel and all the associated components making use of the Terminal commands.

Linux & Ubuntu Terminal Command Cheat sheets

In order to do this open a terminal window ctrl + alt + t

In Ubuntu 18.04, the latest release of PHP 7.2 is now available in the default repositories so you no longer need to additional repositories.

It’s always good practice to update your repositories and carry out any upgrades

sudo apt update && apt upgrade -y

We can now go ahead and install PHP7.2 with all the additional components we need to install Laravel.

sudo apt install php7.2 php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-mysql php7.2-mbstring php7.2-zip php7.2-fpm php7.2-xml -y

To ensure we have everything installed and PHP is working lets quickly check the version using php -v



How to Install MySQL on Ubuntu 18.04

You can install whichever database server you wish but in my case, I needed to use mysql

sudo apt install mysql-server

Once installed just you’ll want to run the included security script. This changes some of the less secure default options for things like remote root logins and sample users.

mysql_secure_installation

How to install curl ubuntu 18.04

Typically on fresh installs of Ubuntu iscURL not installed. cURL is a computer software project providing a library and command-line tool for transferring data using various protocols

sudo apt install curl

Install composer on Ubuntu 18.04

The Laravel uses the composer to manage your dependencies, so the next step is to install  Composer.

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Change permissions on composer so you can run it without sudo

sudo chown -R $USER ~/.composer/



Install Symfony on Ubuntu 18.04

Laravel leverages another PHP framework, Symfony, quite intensively so it worthwhile installing Symfony on your development machine too.

We’ll use Composer to install the Skeleton and create a testproj

composer create-project symfony/skeleton testproj

Afer installing and creating the test projects lets run it to check we have everything working as expected.

Symfony install

Lets quickly run the project to test all is working. Change into the directory cd testproj and execute php -S 127.0.0.1:8000 -t public

We can now open our browser and navigate to http://localhost:8000/ .

We should see the test page which confirms all is up and running
Symfony test page



Install Laravel on Ubuntu 18.04

We’re now ready to install Laravel using composer

composer global require "laravel/installer"

Add Laravel to Path on ubuntu 18.04

All that is left to do now is ensure Laravel is added to you PATH

Approach 1

To do simply open your ~/.bashrc

gedit ~/.bashrc

Towards the bottom of the file just paste the following line

export PATH="$HOME/.composer/vendor/bin:$PATH"

After editing your ~/.bashrc you will need to refresh your current terminal to activate those changes. To do so simply

source ~/.bashrc

Approach 2

If you would prefer to do this all in one line without manually editing your ~/.bashrc you can do so by

echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' >> ~/.bashrc

After editing your ~/.bashrc you will need to refresh your current terminal to activate those changes. To do so simply

source ~/.bashrc



Start a Laravel project on Ubuntu 18.04

We’re all set now to create project using Laravel. We can use either method of starting a project i.e. using Laravel commands of composer based. For the purpose of this guide we’ll be using Laravel.

To start a new project create a directory you would like to store your projects i.e. mkdir projects

change in to the directory cd projects and then we can use the Laravel command to create a new project which in this instance is a simple To Do List web application we’ll name notepad, but you can name it whatever you choose.

 

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

Once the application is generated we can cd notepad the php artisan serve and the we then open a browser and visit http://localhost:8000 we’ll see the laravel sample page

laravel test page

Laravel is now up and running and you can continue to develop using just this, however to really enjoy the benefits of developing with Laravel I recommend installing Homestead.

install VirtualBox on Ubuntu 18.04

VirtualBox is a cross-platform virtualization application. Enabling you to extend the capabilities of your computer so that it can run multiple operating systems (inside multiple virtual machines) at the same time.

Using VirtualBox is a great way to test new versions of operating systems before upgrading your main machine.

For software developers, VirtualBox allows you to mimic your production platform. No more headaches because some software feature which worked great on your machine but isn’t working in production.   Simply do your development within the virtual machine.

 

To install Virtual Box using the terminal

sudo apt install virtualbox

Do not install Extension Pack via the terminal

At the time of writing this post, there seemed to be an issue with installing the virtual box extension pack via the terminal, so I needed use a work-around.

Download Virtual Box Extension pack 5.2.10 and manually install it.

Once download is complete Open virtual box and install the extension pack:
File -> Preferences -> Extensions -> Adds new package (“+” Button)



install Vagrant on Ubuntu 18.04

Vagrant is a software for creating a full development environment within minutes. Vagrant provides a simple and easy to use command-line client for managing these environments, and an interpreter for the text-based definitions of what each environment looks like, called Vagrantfiles.

It’s Open Source and free to use.
sudo apt install vagrant
Once vagrant has finished installing we can now install the Homestead box
vagrant box add laravel/homestead
This should take a few minutes to complete, depending on your machine & internet connection. It has taken as long as 20 minutes, but it only ever has to be done once.

SSH Key Generation

If you’re already using Github and have previously generated you SSH keys you don’t to do this step. If not go ahead and create SSH keys
ssh-keygen -t rsa -C "use your email address here"



Install Homestead

There are two ways you can use Homestead i.e. Global or Per Project.

I will guide you through using on a per project basis. To do this we change into our project folder and install homestead using composer

cd ~/code/sourcelink and composer require laravel/homestead --dev

We now need to execute vendor/bin/homestead make which create out homestead.yaml and Vagrantfile

This will create a homestead.yaml in the Homestead directory which will contain all your homestead configuration details.

Lets go head and edit this details using a text Editor
gedit Homestead.yaml
I always store all my code and projects in a directory in home drive called code, in my case I have created a project called sourcelink which I would like to continue developing. I will configure homestead to run and synchronise this project.

ip: 192.168.10.10
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
    - ~/.ssh/id_rsa
folders:
    -
        map: /home/gary/code/sourcelink
        to: /home/vagrant/code
sites:
    -
        map: sourcelink.test
        to: /home/vagrant/code/public
databases:
    - homestead
name: sourcelink
hostname: sourcelink

Edit Host file

We need to edit the host file to ensure that it points to the directory for the url http://sourcelink.test

To edit the hosts file we’ll use gedit sudo gedit /etc/hosts and add our mapping of 192.168.10.10 sourcelink.test

Once all this configuration is complete you can now execute vagrant up wait a couple of minutes and your entire Laravel Full Stack application is up and running.

Summary

I have to admit that it is porbably not the most friction free experience I have had configuring the various software development frameworks, but Laravel more than makes up for it as you start working with it.

Hope this guide helps you on your way to becoming an efficient Laravel developer and save you time and frustration on your journey.

Follow Me

Gary Woodfine

Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
Follow Me
%d bloggers like this: