Jul 6, 2023 5 min read

How to Install Laravel on Ubuntu 22.04 LTS?

Install Laravel on Ubuntu 22.04 LTS with our step-by-step tutorial. It simplifies web development by providing a clean and elegant syntax.

Install Laravel on Ubuntu 22.04 LTS
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Laravel on Ubuntu 22.04 LTS, let's briefly understand – What is Laravel?

Laravel is a popular open-source PHP framework that simplifies web development by providing a clean and elegant syntax. With its robust features and expressive syntax, Laravel enables developers to build scalable and efficient web applications. It offers a wide range of built-in tools, such as routing, caching, and authentication, making development faster and more enjoyable.

Laravel follows the Model-View-Controller (MVC) pattern, ensuring a structured and organized codebase. Its extensive ecosystem and active community support make Laravel a preferred choice for developers worldwide.

In this tutorial, you will install Laravel on Ubuntu 22.04. We will also address a few FAQs on how to install Laravel on Ubuntu 22.04.

Advantages of Laravel

  1. Clean & Elegant Syntax: Laravel's codebase is expressive and readable, enhancing developer productivity.
  2. Robust Features: Built-in tools like routing, caching, and authentication streamline development processes.
  3. MVC Architecture: Ensures organized code, making maintenance and scalability easier.
  4. Extensive Ecosystem: A thriving community and vast libraries offer ample resources and support.
  5. Rapid Development: Laravel's features and shortcuts accelerate web app creation, saving time and effort.

How to Install Laravel on Ubuntu 22.04 LTS?

Install Laravel on Ubuntu 22.04 LTS by following the procedures outlined below.

Step 1 – Update the System Repositories

First, use the apt package manager to simply run the following command to update the “Ubuntu 22.04” system repositories:

sudo apt update

Step 2 – Install and Configure apache2 Web Server

Install the “apache2” web server in Ubuntu 22.04 after the system update. This server is where the Laravel application is hosted:

sudo apt install apache2

Using the “apt" package manager, “apache2” has been installed from its official repository.

If the “apache2” services are not already enabled, run the "systemctl" command to do so.

sudo systemctl start apache2

Once more, use the "systemctl" command and the "status" keyword to check the status of the "apache2" server:

sudo systemctl status apache2

An appearance of green highlights indicates that "apache2" is in an effective state, or "active (running)".

Step 3 – Create and Set Up a Database for Laravel

The "Laravel" application's database must be created next. This is due to the fact that it contains all of its data. It supports SQL server, SQLite, MariaDB, and PostgreSQL.

In this instance, the following command is used to install the "mysql-server":

sudo apt install mysql-server

After installation, enter the root user's password in the following manner to access the "mysql shell":

sudo mysql -u root -p

As demonstrated below, create the database with the name "my_laravel_db":

> CREATE DATABASE my_laravel_db:

The database user "my_laravel_user" should then be created and secured with the password "password1234". The user can adjust these credentials in accordance with the needs:

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

Grant all root user access to the user "my_laravel_user" as follows:

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

Last but not least, close the "mysql" shell by typing "exit" and pressing "Enter":

> exit

Logging in as "my_laravel_user" will allow you to access its shell for verification.

mysql -u my_laravel_user -p

Leave the "mysql shell" to carry out the following steps:

> exit

Step 4 – Install PHP With Essential Dependencies

Since, PHP 7 or a later version is required by the Laravel application. Fortunately, Ubuntu 22.04's default repository has it.

To install PHP together with all necessary dependencies and extensions, use the following command:

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" has been set up successfully.

Now, use the following command to install "php-curl" using the "apt" package manager:

The most recent "php-curl" version "8.1" has been successfully installed.

Verify the "php" version to see if it has been installed:

php --version

The output confirms the installation of "PHP 8.1.2".

Step 5 – Install Composer

The term “Composer" refers to a package manager used to control the "PHP" application/programming language's dependencies and absolutely necessary libraries.

For downloading and installing “Composer" from its official website, enter the following curl command:

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

Now, transfer the downloaded “composer.phar" file to the /usr/local/bin directory using the mv(move) command:

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

Using the chmod command, give the composer the "x(execute)" rights to make it executable for all users on the current system:

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

Run the -version command to verify that the Compose is installed and running with the most recent version, 2.5.1:

composer --version

Step 6: Install Laravel

Change the current working directory to the webroot directory /var/www/html to install Laravel after the composer has been set up:

cd /var/www/html

Install the Laravel application by using the command type "composer" in the following manner:

sudo composer create-project laravel/laravel laravelapp

The aforementioned order was successfully executed.

Step 7 – Change Laravel Ownership and Permissions

Throughout the installation process that created all of its files and folders, the new directory "laravelapp" is created.

To give the "laravelapp" directory ownership of the web server, use the "chown(change ownership)" directory:

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

Set the "laravel" directory's permissions to "775", which allows just the specified user and group to read, write, and execute the file while allowing others to browse and edit it.

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

It has been successful setting the defined permissions.

Step 8 - Verify the Laravel Installation

To check the "Laravel" version, use the following command and navigate to the "laravelapp" installation directory:

cd laravelapp
php artisan

It has been confirmed that the "Laravel Framework 9.48.0" version of Ubuntu on 22.04 LTS has been installed successfully.

How to Remove Laravel From Ubuntu 22.04?

Simply delete the new created composer project using the "rm" command to remove the Laravel application:

sudo rm /usr/local/bin/composer

The "VPS (Virtual Private Server)" no longer has the Laravel Framework 9.48.0.

FAQs to Install Laravel on Ubuntu 22.04 LTS

What is the recommended method to install Laravel on Ubuntu 22.04 LTS?

Use Composer to install Laravel by running: composer global require laravel/installer.

How do I create a new Laravel project on Ubuntu 22.04 LTS?

Run laravel new project-name to create a new Laravel project.

Can I use a different database instead of MySQL with Laravel on Ubuntu 22.04 LTS?

Yes, Laravel supports various databases. Update the database settings in the .env file to use PostgreSQL, SQLite, or others.

How can I configure a web server to run Laravel on Ubuntu 22.04 LTS?

Set up a virtual host in your web server configuration pointing to the public directory of your Laravel project.

How do I serve a Laravel application during development on Ubuntu 22.04 LTS?

Run php artisan serve from the project's root directory. The application will be accessible at http://localhost:8000.

What if I encounter permission issues while installing Laravel on Ubuntu 22.04 LTS?

Make sure the appropriate permissions are set for project directories. Use chmod and chown commands to adjust permissions if needed.

Conclusion

We hope this tutorial helped you understand how to install Laravel on Ubuntu 22.04.

If you have any suggestions or queries, kindly leave them in the comments section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.