How to Install Laravel Framework on Ubuntu 18.04 LTS

Filed Under: PHP

In this guide, we look at how to install Laravel framework on Ubuntu 18.04 server. Laravel is a free and open source PHP web framework. It was created in 2011 by Taylor Otwell and has since been used for development of robust mobile applications using the MVC model.

Let’s dive in and see how we can install Laravel framework on Ubuntu 18.04 LTS Server.

Before we install Laravel framework, let’s first install the prerequisite packages that will be required.

System prerequisites

Your system will need to satisfy the requirements below before proceeding.

  • Apache Web server
  • PHP >= 7.1.3 with OpenSSL, PDO, Mbstring, Tokenizer, XML, Ctype and JSON PHP Extensions.
  • Composer – an application-level package manager for the PHP

1. Install Apache Web server and PHP 7.2

We are going to add a third party PHP repository. Even though Ubuntu’s own repository has its own PHP, a third party repository is very much recommended because it gets more frequently updated.

To add the repository execute the command:

$ sudo add-apt-repository ppa:ondrej/php

Sample Output

Ondrej Ppa

Next, update your system’s repositories.

$ sudo apt update

Sample Output

Ubuntu Sudo Apt Update

Next, install Apache and PHP 7.2 alongside other prerequisites.

$ sudo apt-get install apache2 libapache2-mod-php7.2 php7.2 php7.2-xml php7.2-gd php7.2-opcache php7.2-mbstring 

Sample Output

Install Apache and PHP

2. Install Composer

Before we embark on the installation, we will first install a few useful tools. These are git version control, curl and unzip packages.

$ sudo apt install curl git unzip

Sample Output

Install Curl Zip Git

Next, we need to install composer. The composer is another useful tool that handles dependency management in PHP and allows you to package the required libraries associated with a package as one. Composer will download and install all the packages required to run the Laravel framework.

To install Composer, execute the following commands


$ cd /opt
$ curl -sS https://getcomposer.org/installer | php

Sample Output

Install Composer

The curl command downloads Composer to the /opt directory. Since we need composer running globally, we must move it to the /usr/local/bin directory under 'composer' name.

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

This will allow us to run composer from anywhere.

3. Install Laravel Framework

Now, let’s navigate to the public_html directory of our Ubuntu System. To install Laravel, we will navigate to the /var/www/html directory.

cd /var/www/html

Next, we will create a directory “your-project” with Laravel installation. The composer will proceed to use Git to download and install all packages and modules that Laravel requires for functioning.

$ sudo composer create-project laravel/laravel your-project --prefer-dist

Sample Output

Installing Composer and Laravel

4. Configure Apache Web Server for Laravel

The next step is to configure our Apache Web server. We need to assign the necessary permissions to the project directory which will allow access to it from the www-data group and give it write permissions to the storage directory. To get this done, run the following commands.


# sudo chgrp -R www-data /var/www/html/your-project
# sudo chmod -R 775 /var/www/html/your-project/storage

Now let’s navigate to /etc/apache2/sites-available directory and run the command below to create a configuration file for our Laravel install.

$ vim /etc/apache2/sites-available/laravel.conf

Add the following content:


ServerName localhost

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/your-project/public

AllowOverride All

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

Save the file and Exit.

Finally, we are going to enable the newly created laravel.conf file. But before that, let’s disable the default config file.

$ sudo a2dissite 000-default.conf

Sample Output

disable defaut apache configuration

Next, enable the Laravel config file.

$ sudo a2ensite laravel.conf

Sample Output

apache enable laravel configuration

Then enable rewrite mode:

$ sudo a2enmod rewrite

Sample Output

apache enable rewrite mode

Lastly, restart the Apache service.

$ sudo systemctl restart apache2

To verify that Apache is running execute the command:

systemctl status apache2

Sample Output

Check Apache2 Status

5. Test Laravel Website

At this point, You have successfully installed Laravel on your Ubuntu 18.04 LTS System. To confirm that the installation went as expected visit your server’s IP address.

http://server-IP-address

In my case, the server’s IP is http://38.76.11.149

Laravel GUI on Ubuntu

We hope that this guide has been helpful. Feel free to leave your feedback at the comment section.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages