How to Install Laravel PHP Framework on Ubuntu – Laravel is a flexible opensource PHP framework for web artisans. It can be used as an alternative to the CodeIgniter framework. Laravel is designed for ease of use to allow developers to create great applications.

If you’re looking for a simple and straightforward PHP framework to design your next application, you’ll find Laravel to be useful. This brief tutorial is going to show students and new users how to install the Laravel PHP framework on Ubuntu 16.04 | 17.10 | 18.04 with Apache2 and PHP 7.2 support.

Step 1 – Installing LAMP Stack

First of all, you need to setup LAMP stack on your Ubuntu system. Laravel required PHP 7.2.5 or higher version to be installed.

Install PHP

sudo apt install software-properties-common 
sudo add-apt-repository ppa:ondrej/php 
sudo apt install -y php7.4 php7.4-gd php7.4-mbstring php7.4-xml

Apache2

sudo apt install apache2 libapache2-mod-php7.4

Install MySQL

sudo apt install mysql-server php7.4-mysql

You also need MySQL post-installation instructions.

Step 2 – Installing Composer

PHP Composer is used to installing required dependencies for the PHP application. Execute the following commands to install and configure Composer on your system.

curl -sS https://getcomposer.org/installer | php 
sudo mv composer.phar /usr/local/bin/composer 
sudo chmod +x /usr/local/bin/composer

Step 3 – Download and Install Laravel

The latest Laravel version is available under the Github repository. Use the below command to clone the master branch of the Laravel from the GitHub repository.

cd /var/www 
git clone https://github.com/laravel/laravel.git

Switch to the Laravel directory and use the composer to install all dependencies required for the Laravel framework.

cd /var/www/laravel 
sudo composer install

The dependencies installation may take some time as per your network speed. After successfully installing all dependencies, set the proper permissions on all files.

chown -R www-data.www-data /var/www/laravel 
chmod -R 755 /var/www/laravel 
chmod -R 777 /var/www/laravel/storage

Step 4 – Create Environment Settings

Next, create the Laravel environment configuration file. You can do it by renaming the .evn.example file to .env. This will use to setup an application environment for the project.

mv .env.example .env

Now generate base64 random number encryption key, which used by the Illuminate encrypter service.

php artisan key:generate
Application key set successfully.

Edit the .env configuration file and update the required settings. Also, make sure APP_KEY is properly set as generated in the above command.

vi .env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:HFdS7c9rhDp+AeHu7kc2OLBPuxHqq2BQ/1gfFWEpoAk=
APP_DEBUG=true
APP_URL=http://localhost
...

You can also change the APP_NAME with the name of your application and APP_URL to the URL you need to access your Laravel application.

Step 5 – Create MySQL User and Database

Next, create a MySQL database for your Laravel application. Also, create a MySQL user to connect the database from the Laravel application. Login to your MySQL server and create MySQL database and user by running following commands.

Now edit the .env file and update database settings.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret

Step 6 – Apache Configuration

Next, edit Apache default virtual host configuration file (ie: 000-default.conf) and update Document Root to the Laravel public directory as below:

vim /etc/apache2/sites-enabled/000-default.conf

Update the configuration like below:

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/laravel/public

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/laravel>
                AllowOverride All
        </Directory>

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

</VirtualHost>

Reload Apache configuration changes by restarting service using below command

sudo systemctl restart apache2

Step 7 – Access Laravel Application

You have successfully configured the Laravel 7 PHP framework on your system. Access Laravel application in your favorite web browser

Install Laravel 7

Let’s start building an awesome application using Laravel 7 PHP Framework. Thanks.