How to install Laravel 8 on Ubuntu 20.04
How to install Laravel 8 on Ubuntu 20.04

How To Install Laravel 8 On Ubuntu 20.04 LTS

On this short tutorial, we will explain how to install Laravel 8 framework with Apache on Ubuntu 20.04 LTS operating system.

Introduction

Laravel is a free, open-source PHP web framework for web applications following the model–view–controller (MVC) architectural pattern and based on Symfony. Laravel comes with a rich set of features that will increase the speed of web development, including: Artisan, MVC Architecture, Object-relational mapping, Template Engine, Unit-Testing, and Database Migration System.
Installing Laravel 8 On Ubuntu 20.04 LTS

There are several steps and prerequisites that must be met before we can install Laravel 8. The Laravel installation process will be explained in more detail in the sub-chapters below.

  • Installing PHP
  • Installing Apache Web Server
  • Installing MariaDB And Creating Database for Laravel 8
  • Installing Composer
  • Installing Laravel 8 On Ubuntu 20.04
  • Configuring Apache Web Server For Laravel Site
  • Accessing Laravel 8 From a Web Browser

Prerequisites

Before we start the Laravel framework installation process on Ubuntu 20.04 LTS, there are prerequisites that must be met, namely:

  • An Ubuntu 20.04 LTS Server with sufficient disk space
  • An account with sudo or root access to run privileged commands
  • Internet access for downloading any required packages
  • Updated repository on our server

Every time we will install a new application we are advised to update the local repository to the latest version and we need to install required applications which is needed by the Laravel. To do this we can use the command line:

$ sudo apt-get update
$ sudo apt install zip unzip software-properties-common

Installing PHP

PHP is one of the important components for the running of Laraval on a system, on this tutorial we will install PHP 7.4 and PHP additional extensions by using command line :

$ sudo apt install -y php7.4 php7.4-gd php7.4-mbstring php7.4-xml php-zip
$ sudo apt install php-json php-xml php-zip php-pdo  php-tokenizer php-bcmath

The output will be as shown below :

mpik@bckinfo:~$ sudo apt install -y php7.4 php7.4-gd php7.4-mbstring php7.4-xml php-zip
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following additional packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php7.4 libapr1 libaprutil1
libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 libonig5 libzip5 php-common php7.4-cli
php7.4-common php7.4-json php7.4-opcache php7.4-readline php7.4-zip
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom php-pear
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php7.4 libapr1 libaprutil1
libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 libonig5 libzip5 php-common php-zip php7.4
php7.4-cli php7.4-common php7.4-gd php7.4-json php7.4-mbstring php7.4-opcache php7.4-readline
php7.4-xml php7.4-zip
0 upgraded, 24 newly installed, 0 to remove and 542 not upgraded.
Need to get 6,569 kB of archives.
After this operation, 28.4 MB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu focal/main amd64 libapr1 amd64 1.6.5-1ubuntu1 [91.4 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu focal/main amd64 libaprutil1 amd64 1.6.1-4ubuntu2 [84.7 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu focal/main amd64 libaprutil1-dbd-sqlite3 amd64 1.6.1-4ubuntu2 [10.5 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu focal/main amd64 libaprutil1-ldap amd64 1.6.1-4ubuntu2 [8,736 B]
Get:5 http://us.archive.ubuntu.com/ubuntu focal/main amd64 liblua5.2-0 amd64 5.2.4-1.1build3 [106 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 apache2-bin amd64 2.4.41-4ubuntu3.4 [1,180 kB]
Get:7 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 apache2-data all 2.4.41-4ubuntu3.4 [159 kB]
Get:8 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 apache2-utils amd64 2.4.41-4ubuntu3.4 [84.0 kB]
Get:9 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 apache2 amd64 2.4.41-4ubuntu3.4 [95.5 kB]
Get:10 http://us.archive.ubuntu.com/ubuntu focal/main amd64 php-common all 2:75 [11.9 kB]
Get:11 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 php7.4-common amd64 7.4.3-4ubuntu2.5 [980 kB]
Get:12 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 php7.4-json amd64 7.4.3-4ubuntu2.5 [19.2 kB]
Get:13 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 php7.4-opcache amd64 7.4.3-4ubuntu2.5 [198 kB]
Get:14 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 php7.4-readline amd64 7.4.3-4ubuntu2.5 [12.6 kB]
Get:15 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 php7.4-cli amd64 7.4.3-4ubuntu2.5 [1,421 kB]
Get:16 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 libapache2-mod-php7.4 amd64 7.4.3-4ubuntu2.5 [1,364 kB]
Get:17 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 libonig5 amd64 6.9.4-1 [142 kB]
Get:18 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 libzip5 amd64 1.5.1-0ubuntu1 [46.7 kB]
Get:19 http://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 php7.4-zip amd64 7.4.3-4ubuntu2.5 [21.6 kB]
Get:20 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 php-zip all 2:7.4+75 [1,996 B]
Get:21 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 php7.4 all 7.4.3-4ubuntu2.5 [9,244 B]
Get:22 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 php7.4-gd amd64 7.4.3-4ubuntu2.5 [27.9 kB]
Get:23 http://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 php7.4-mbstring amd64 7.4.3-4ubuntu2.5 [396 kB]
Get:24 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 php7.4-xml amd64 7.4.3-4ubuntu2.5 [97.3 kB]
Fetched 6,569 kB in 1min 3s (104 kB/s)

Setting up php7.4 (7.4.3-4ubuntu2.5) …
Processing triggers for ufw (0.36-6) …
Processing triggers for systemd (245.4-4ubuntu3) …
Processing triggers for man-db (2.9.1-1) …
Processing triggers for libc-bin (2.31-0ubuntu9) …
Processing triggers for php7.4-cli (7.4.3-4ubuntu2.5) …
Processing triggers for libapache2-mod-php7.4 (7.4.3-4ubuntu2.5) …

Then we will install PHP extension, by submitting command line :





mpik@bckinfo:~$ sudo apt install php-json php-xml php-zip php-pdo php-tokenizer php-bcmath
Reading package lists… Done
Building dependency tree
Reading state information… Done
Note, selecting 'php7.4-common' instead of 'php-pdo'
php7.4-common is already the newest version (7.4.3-4ubuntu2.5).
php7.4-common set to manually installed.
The following additional packages will be installed:
php7.4-bcmath php7.4-xml php7.4-zip
The following NEW packages will be installed:
php-bcmath php-json php-tokenizer php-xml php-zip php7.4-bcmath php7.4-xml php7.4-zip
0 upgraded, 8 newly installed, 0 to remove and 541 not upgraded.
Need to get 148 kB of archives.
After this operation, 699 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

Creating config file /etc/php/7.4/mods-available/zip.ini with new version
Setting up php-zip (2:7.4+75) …
Setting up php-tokenizer (1.1.3-2) …
Setting up php-xml (2:7.4+75) …
Setting up php-bcmath (2:7.4+75) …
Processing triggers for libapache2-mod-php7.4 (7.4.3-4ubuntu2.5) …
Processing triggers for php7.4-cli (7.4.3-4ubuntu2.5) …

Installing Apache Web Server

Laravel 8 need a web server to host its modules. Ee have two web server options that can be used, namely: Apache Web Server and NginX. For the purposes of installing Laravel 8 in this tutorial, we will use Apache as the web server. On this step we will install Apache Web server.

To install Apache web server, we will use the Ubuntu repository which was already exist. To install Apache web server, we will submit the command line :

$ sudo apt install apache2 libapache2-mod-php7.4

The output will be as show below :

mpik@bckinfo:~$ sudo apt install apache2 libapache2-mod-php7.4
Reading package lists... Done
Building dependency tree       
Reading state information... Done
apache2 is already the newest version (2.4.41-4ubuntu3.4).
apache2 set to manually installed.
libapache2-mod-php7.4 is already the newest version (7.4.3-4ubuntu2.5).
libapache2-mod-php7.4 set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 542 not upgraded.

We will start Apache web server service after installation was completed done. To do this, we will submit the command line :

$ sudo systemctl start apache2
$ sudo systemctl status apache2
$ sudo systemctl enable apache2

The output will be shown below :

mpik@bckinfo:~$ sudo systemctl start apache2
mpik@bckinfo:~$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-08-05 00:18:44 PDT; 4min 33s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 16232 (apache2)
      Tasks: 6 (limit: 9455)
     Memory: 12.7M
     CGroup: /system.slice/apache2.service
             ├─16232 /usr/sbin/apache2 -k start
             ├─16235 /usr/sbin/apache2 -k start
             ├─16236 /usr/sbin/apache2 -k start
             ├─16237 /usr/sbin/apache2 -k start
             ├─16238 /usr/sbin/apache2 -k start
             └─16239 /usr/sbin/apache2 -k start

Aug 05 00:18:44 bckinfo systemd[1]: Starting The Apache HTTP Server...
Aug 05 00:18:44 bckinfo apachectl[16222]: AH00558: apache2: Could not reliably determine the server's fully qual>
Aug 05 00:18:44 bckinfo systemd[1]: Started The Apache HTTP Server.
mpik@bckinfo:~$ sudo systemctl enable apache2
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable apache2

Installing MariaDB And Creating Database for Laravel 8

Laravel 8 needs a database to store and retrieve the configuration and data. On this tutorial we will use MariaDB as Laravel’s database. Beside MariaDB, Laravel also supports andy database systems such as : MySQL, SQLite, Postgres, or SQL Server. To install MariaDB we will do the following command line :

$ sudo apt install mariadb-server

The output will be shown below :

mpik@bckinfo:~$ sudo apt install mariadb-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libevent-core-2.1-7 libevent-pthreads-2.1-7 libmecab2 mecab-ipadic mecab-ipadic-utf8 mecab-utils
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  galera-4 gawk libdbd-mariadb-perl libdbi-perl libmariadb3 libperl5.30 libpmem1 libreadline5 libsigsegv2 libterm-readkey-perl
  mariadb-client-10.6 mariadb-client-core-10.6 mariadb-common mariadb-server-10.6 mariadb-server-core-10.6 perl perl-base perl-modules-5.30
  socat
Suggested packages:
  gawk-doc libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl mailx mariadb-test perl-doc libterm-readline-gnu-perl
  | libterm-readline-perl-perl make libb-debug-perl liblocale-codes-perl
The following packages will be REMOVED:
  mysql-client-8.0 mysql-client-core-8.0 mysql-server mysql-server-8.0 mysql-server-core-8.0
The following NEW packages will be installed:
  galera-4 gawk libdbd-mariadb-perl libdbi-perl libmariadb3 libpmem1 libreadline5 libsigsegv2 libterm-readkey-perl mariadb-client-10.6
  mariadb-client-core-10.6 mariadb-common mariadb-server mariadb-server-10.6 mariadb-server-core-10.6 socat
The following packages will be upgraded:
  libperl5.30 perl perl-base perl-modules-5.30
4 upgraded, 16 newly installed, 5 to remove and 564 not upgraded.
Need to get 27.8 MB/36.3 MB of archives.
After this operation, 18.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...
Setting up mariadb-server (1:10.6.3+maria~focal) ...
Processing triggers for systemd (245.4-4ubuntu3) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...

To verify MariaDB on installed on our system, we will query its services and version by submitting command line :

$ sudo systemctl status mariadb
$ mariadb --version

The output will be shown :

mpik@bckinfo:~$ sudo systemctl status mariadb
● mariadb.service - MariaDB 10.6.3 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/mariadb.service.d
             └─migrated-from-my.cnf-settings.conf
     Active: active (running) since Thu 2021-08-05 18:28:24 PDT; 1min 8s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 36224 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 9 (limit: 9455)
     Memory: 73.7M
     CGroup: /system.slice/mariadb.service
             └─36224 /usr/sbin/mariadbd

Aug 05 18:28:26 bckinfo /etc/mysql/debian-start[36254]: sys
Aug 05 18:28:26 bckinfo /etc/mysql/debian-start[36254]: Phase 6/7: Checking and upgrading tables
Aug 05 18:28:26 bckinfo /etc/mysql/debian-start[36254]: Processing databases
Aug 05 18:28:26 bckinfo /etc/mysql/debian-start[36254]: information_schema
Aug 05 18:28:26 bckinfo /etc/mysql/debian-start[36254]: performance_schema
Aug 05 18:28:26 bckinfo /etc/mysql/debian-start[36254]: sys
Aug 05 18:28:26 bckinfo /etc/mysql/debian-start[36254]: sys.sys_config                                     OK
Aug 05 18:28:26 bckinfo /etc/mysql/debian-start[36254]: Phase 7/7: Running 'FLUSH PRIVILEGES'
Aug 05 18:28:26 bckinfo /etc/mysql/debian-start[36254]: OK
Aug 05 18:28:26 bckinfo /etc/mysql/debian-start[36518]: Triggering myisam-recover for all MyISAM tables and aria-recover for all Aria tables
mpik@bckinfo:~$ mariadb --version
mariadb  Ver 15.1 Distrib 10.6.3-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

After database engine was installed, the next step is to create a database for holding Laravel data. We will use SQL scripts for creating database as show on the following steps.

CREATE DATABASE laravel_diginet_db;
CREATE USER 'laravel_diginet'@'localhost' IDENTIFIED BY 'LRV1234';
GRANT ALL ON laravel_diginet_db.* TO 'laravel_diginet'@'localhost';
FLUSH PRIVILEGES;

The output will be shown below :

mpik@bckinfo:~$ sudo  mysql -u root -p
[sudo] password for mpik: 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 45
Server version: 10.6.3-MariaDB-1:10.6.3+maria~focal mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE laravel_diginet_db;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> CREATE USER 'laravel_diginet'@'localhost' IDENTIFIED BY 'LRV1234';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> CREATE USER 'laravel_diginet'@'localhost' IDENTIFIED BY 'LRV1234';
ERROR 1396 (HY000): Operation CREATE USER failed for 'laravel_diginet'@'localhost'
MariaDB [(none)]> CREATE USER 'lvrldiginet'@'localhost' IDENTIFIED BY 'LRV1234';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> quit
Bye

Installing Composer

Laravel needs Composer to run properly. Composer is a dependency manager for PHP (as npm is to Node.js or pip is to Python). Composer provides a framework for managing libraries and dependencies and required dependencies. We will install Composer with following command lines.

We will use curl command line to download Composer source file.

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

The output is as follow :

mpik@bckinfo:~$ curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading…

Composer (version 2.1.5) successfully installed to: /home/mpik/composer.phar
Use it: php composer.phar

Then move it to composer home directory, by submitting command line :

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

Then change file mode to be an executable file :

$ sudo chmod +x /usr/local/bin/composer

Then verify its version by texting command line :

$ composer --version

The output is as shown below :

mpik@bckinfo:~$ ls -ltr *.phar
-rwxr-xr-x 1 mpik mpik 2256047 Aug 5 18:56 composer.phar
mpik@bckinfo:~$ sudo mv composer.phar /usr/local/bin/composer
[sudo] password for mpik:
mpik@bckinfo:~$ sudo chmod +x /usr/local/bin/composer
mpik@bckinfo:~$ composer --version
Composer version 2.1.5 2021-07-23 10:35:47

Installing Laravel 8 On Ubuntu 20.04

By using Composer, we will install Laravel 8 on Ubuntu 20.04 server. Here are the steps.

Creating project on Composer, by using command line :

$ composer create-project laravel/laravel bckinfo

The output :

mpik@bckinfo:~$ composer create-project laravel/laravel laraveldiginet
Creating a "laravel/laravel" project at "./bckinfo"
Installing laravel/laravel (v8.5.23)
  - Downloading laravel/laravel (v8.5.23)
  - Installing laravel/laravel (v8.5.23): Extracting archive
Created project in /home/mpik/bckinfo
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies
Lock file operations: 110 installs, 0 updates, 0 removals
  - Locking asm89/stack-cors (v2.0.3)
  - Locking brick/math (0.9.2)
  - Locking dflydev/dot-access-data (v3.0.0)
  - Locking doctrine/inflector (2.0.3)
  - Locking doctrine/instantiator (1.4.0)
...
78 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/sail
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
76 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi
No publishable resources for tag [laravel-assets].
Publishing complete.
> @php artisan key:generate --ansi
Application key set successfully.

The command line above will create a new directory called laravelodiginet and installs all the files and directories for Laravel. After the installation is completed done, then navigate to the installation directory (laraveldiginet) and check the Laravel version.

mpik@bckinfo:/var/www/html$ cd laraveldiginet
mpik@bckinfo:/var/www/html/laraveldiginet$ php artisan
Laravel Framework 8.53.1

Until this stage, the Laravel Framework version 8.50.0 is installed.

Configuring Apache Web Server For Laravel Site

On this stage, we will configure Apache web to server Laravel 8 site. We will update /etc/apache2/sites-available/laravel.conf file to accommodate the requirement of Laravel.

$ sudo vi /etc/apache2/sites-available/laravel.conf

The output :

mpik@bckinfo:/var/www/html/laraveldiginet$ sudo vi /etc/apache2/sites-available/laravel.conf
[sudo] password for mpik:

<VirtualHost *:80>
ServerName test.bckinfo.com
ServerAdmin admin@bckinfo.com
DocumentRoot /var/www/html/laraveldiginet/public
<Directory /var/www/html/laraveldiginet>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Then restart the Apache web server. Here is the following steps :

mpik@bckinfo:/var/www/html/laraveldiginet$ sudo a2ensite laravel.conf
Enabling site laravel.
To activate the new configuration, you need to run:
systemctl reload apache2
mpik@bckinfo:/var/www/html/laraveldiginet$ sudo a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
systemctl restart apache2
mpik@bckinfo:/var/www/html/laraveldiginet$ sudo systemctl reload apache2
mpik@bckinfo:/var/www/html/laraveldiginet$ sudo ystemctl restart apache2
sudo: ystemctl: command not found
mpik@bckinfo:/var/www/html/laraveldiginet$ sudo systemctl restart apache2
mpik@bckinfo:/var/www/html/laraveldiginet$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-08-05 20:20:02 PDT; 8s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 41402 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 41426 (apache2)
Tasks: 6 (limit: 9455)
Memory: 11.3M
CGroup: /system.slice/apache2.service
├─41426 /usr/sbin/apache2 -k start
├─41427 /usr/sbin/apache2 -k start
├─41428 /usr/sbin/apache2 -k start
├─41429 /usr/sbin/apache2 -k start
├─41430 /usr/sbin/apache2 -k start
└─41431 /usr/sbin/apache2 -k start
Aug 05 20:20:02 bckinfo systemd[1]: Starting The Apache HTTP Server…
Aug 05 20:20:02 bckinfo apachectl[41423]: AH00558: apache2: Could not reliably determine the server's fully qualifi>
Aug 05 20:20:02 bckinfo systemd[1]: Started The Apache HTTP Server.
mpik@bckinfo:/var/www/html/laraveldiginet$

Accessing Laravel 8 From a Web Browser

After all steps are done successfully, then we could access Laravel 8 from website.

Conclusion

On this short guide, we have learnt how to install Laravel 8 on Ubuntu 20.04 LTS server successfuly.

(Visited 59 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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