How to Install Laravel on Ubuntu 22.04 LTS?

Laravel is a PHP-based, free, and open-source web framework for creating and building high-end web applications. It is available for both small and industrial-level development applications. It is famous for its effective and simple syntax, premium features, and powerful tools that help even beginners.

This post will illustrate a complete procedure to install Laravel on Ubuntu 22.04.

How to Install Laravel on Ubuntu 22.04 LTS?

Follow the below-defined essential steps to install Laravel on Ubuntu 22.04 LTS.

Step 1: Update the System Repositories

First, update the “Ubuntu 22.04” system repositories by just executing the following command with the help of its “apt” package manager:

$ sudo apt update

Step 2: Install and Configure apache2 Web Server

After the system update, install the “apache2” web server in Ubuntu 22.04. This server is used to host the Laravel application:

$ sudo apt install apache2

The “apache2” has been installed from its official repository using the “apt” package manager.

Execute the “systemctl” command to enable the “apache2” services if it is not running after the installation:

$ sudo systemctl start apache2

Again, use the “systemctl” command with the “status” keyword to verify the “apache2” server status:

$ sudo systemctl status apache2

The green highlights confirms that “apache2” is in its “active(running)” state, i.e., working properly.

Step 3: Create and Configure a Database for Laravel

The next step is to create the database for the “Laravel” application. This is because all of its data is stored in it. It supports MariaDB, PostgreSQL, SQL server, and SQLite.

In this scenario, the “mysql-server” is installed using this command:

$ sudo apt install mysql-server

After its installation, access the “mysql shell” as the root user by entering its password in this way:

$ sudo mysql -u root -p

Create the database having the name “my_laravel_db” as shown below:

> CREATE DATABASE my_laravel_db:

Next, create the database user “my_laravel_user” protected by the password “password1234”. The user can set these credentials as per requirements:

> CREATE USER 'my_laravel_user'@'localhost' IDENTIFIED BY 'password1234';

Grant all the root user privileges to the user “my_laravel_user” in the following way:

> GRANT ALL ON laravel_db.* TO 'my_laravel_user'@'localhost';

In last, exit the “mysql” shell by typing the “exit” keyword and hitting the “Enter” key:

>exit

For verification, login as the “my_laravel_user” and you will navigate to its shell:

> mysql -u my_laravel_user -p

Exit the “mysql shell” to perform further steps:

> exit

Now the user is in the normal user prompt, i.e., “itslinuxfoss”.

Step 4: Install  PHP With Essential Dependencies

As the Laravel application requires PHP 7 or the above version. Luckily it is available in the default repository of Ubuntu 22.04

Run the following command to install PHP with its essential dependencies and extensions:

$ sudo apt install php libapache2-mod-php php-mbstring php-cli php-bcmath php-json php-xml php-zip php-pdo php-common php-tokenizer php-mysql

The “PHP” is successfully installed. 

Now install the “php-curl” by using the “apt” package manager via this command:

The “php-curl” having the latest version “8.1” has been successfully installed.

Check the “php” version to confirm whether it is installed or not:

$ php --version

The output confirms that “PHP 8.1.2” has been installed.

Step 5: Install Composer

The Composer refers to a package manager used to manage the dependencies and the necessarily required libraries of the “PHP” application/programming language.

Type the following “curl” command for downloading and installing the “Composer” from its official website:

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

Now, use the “mv(move)” command to move the downloaded “composer.phar” file to the  /usr/local/bin location:

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

Assign the “x(execute)” permissions to the composer to make it executable for all current system’s users using the “chmod” command:

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

Execute the “–version” command that confirms that the “Composer” having latest version “2.5.1” has been installed and executable:

$ composer --version

Step 6: Install Laravel

Once the composer is installed,  change the present working directory to the webroot directory “/var/www/html” to install Laravel:

$ cd /var/www/html

Install the Laravel application by utilizing the “composer” as the command type in this way:

$  sudo composer create-project laravel/laravel laravelapp

The above command has been executed successfully.

Step 7: Change Laravel Ownership and Permissions

The new directory “laravelapp” is created during the installation that installed all its files and directories.

Use the “chown(change ownership)” directory to allow the “laravelapp” directory the web server ownership:

$ sudo chown -R www-data:www-data /var/www/html/laravelapp

Set the “775” permissions to the “laravel” directory that means only the user and its associated group read, write, and execute it while others can also read and write in it:

$ sudo chmod -R 775 /var/www/html/laravelapp/storage

The defined permissions are set successfully.

Step 8: Verify the Laravel Installation

Navigate to the “laravelapp” installation directory and execute the following command to check the “Laravel” version by using these commands: 

$ cd laravelapp
$ php artisan

It is verified that the “Laravel Framework 9.48.0” version has successfully installed Ubuntu on 22.04 LTS

How to Remove Laravel From Ubuntu 22.04?

To remove the Laravel application, simply delete the newly created composer project using the “rm” command:

$ sudo rm /usr/local/bin/composer

The Laravel Framework 9.48.0 has been removed from the “VPS(Virtual Private Server)”.

Conclusion

On Ubuntu 22.04 LTS, the Laravel application can be easily installed through “composer” as a command with “superuser”, i.e., “sudo” privileges. It requires the “PHP” and the “composer” as a prerequisite. This guide has illustrated the complete procedure to install Laravel on ubuntu 22.04 LTS.