Install Laravel on Ubuntu 22.04 LTS Jammy JellyFish Linux

Tutorial to learn the steps for installing PHP Laravel framework on Ubuntu 22.04 LTS Jammy JellyFish using the command terminal for developing web apps.

PHP doesn’t need an introduction, it has been around for many years powering web applications that need a dynamic programming language to work but one thing it is not (anymore): modern.

Programming languages ​​such as Ruby and Python have become increasingly popular, especially in recent years. They are “cool” and appeal better to the next generation of coders. Whereas it is unfortunate PHP is getting a bit old and you can tell. This is exactly where Laravel comes into play. We can consider it as a new-generation PHP framework and that’s what makes it so popular. Inspired by Ruby on Rails and .NET, Taylor Otwell created Laravel to get the most out of PHP and to prove that more is possible. Also, he wasn’t satisfied with the other PHP frameworks. They are no longer contemporary. He doesn’t only want to help developers be more productive but also to show that clean programming with PHP can also be fun again.

In this article, let’s discuss the initial phase to work with Laravel is to install it on Ubuntu-based Linux systems.

Steps to install Laravel on Ubuntu 22.04 LTS Linux

The steps given here to set up the Laravel framework on Ubuntu 22.04 will be the same for other versions of Ubuntu such as 18.04 or 20.04 as well.

1: System update

First of all, update your system so that it could have the latest updates for security and the installed packages. This will also refresh the APT package manager index cache.

sudo apt update && sudo apt upgrade

Also, install git:

sudo apt install git

2. Install LAMP on Ubuntu 22.04

Laravel framework needs PHP to work. However, here we are also installing Apache, and MySQL on your Ubuntu Linux to serve pages of the app, generated on this framework using Apache whereas to save the data, we have MySQL. Hence, let’s first install the LAMP stack using the command given below.

sudo apt install apache2 mysql-server php php-gd php-mbstring php-xml php-zip php-curl

Once the installation is completed, you can check the services of Apache and MySQL are working without any errors or not.

For Apache:

systemctl status apache2 --no-pager -l
check Apache service status

For MySQL

systemctl status mysql --no-pager -l
MySQL Service status

3. Secure MySQL and create a Database

We need a Database to store the data generated by your Laravel app project, hence here we are using a MySQL Database server for that.

However, before creating a database, let’s secure our MySQL server.

Login to MySQL first:

sudo mysql

Set root password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'MyPassword@123';

Note: Change MyPassword@123 with the strong password, you want to set.

Exit:

exit;

Run the Secure Installation script again.

sudo mysql_secure_installation

The script will ask these questions.

Enter the password for user root: type your set password and then press ENTER.
Change the password for root? Press N, then ENTER.
Remove anonymous users? Press Y, then ENTER.
Disallow root login remotely? Press Y, then ENTER.
Remove the test database and access to it? Press Y, then ENTER.
Reload privilege tables now? Press Y, then ENTER.

Once done create a Database for Laravel:

Log in to the Database, if you are not already:

sudo mysql

Now, use the given commands, one by one:

CREATE DATABASE yourdb;
CREATE USER 'youruser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON yourdb.* to 'youruser'@'localhost';
FLUSH PRIVILEGES;
quit;

Note: Replace yourdb with whatever name you want to give your Database, whereas youruser is the user you want to create while the password is the secret key to protect your created user account.

4. Install PHP Composer for Laravel

To install Laravel and the dependencies required by it to work properly, we need to use Composer. It is a library manager for PHP projects. Well, we can install Composer using the default package manager of our Ubuntu 22.04, however, the version provided by it will not be the latest one. Therefore, here we are going for an installer script, officially provided by the developers of this PHP library management tool.

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

5. Download the Laravel framework project

Let’s clone Laravel from its GitHub repository directly into our web root directory.

Switch to the web directory:

cd /var/www

Clone Laravel

sudo git clone https://github.com/laravel/laravel.git

6. Install Laravel on Ubuntu 22.04

Once you have downloaded Laravel on your system, switch to its directory and use the Composer to start the installation process.

cd /var/www/laravel

Use the Composer

sudo composer install

Once the installation is completed the change permission to files :

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

—–From here onwards we will show how to set it up for accessing the Laravel app using Apache——

7. Create the Laravel environment configuration file

We already have an example environment file, let’s use it to create one for our Laravel app.

While inside the Laravel directory, run:

sudo cp .env.example .env

Generate an encryption key for your app:

php artisan key:generate

Add the Database details:

sudo nano .env

Go to the database section and change the values:

Add Database details Laravel app

Save the file using Ctrl+O, hit the Enter key, and then exit the file using Ctrl+X.

8. Apache Configuration for PHP Laravel App

Edit the default Apache configuration file and change the web root document directory path. This will point it to serve the files provided by Laravel instead of the default Apache page.

sudo nano /etc/apache2/sites-enabled/000-default.conf

Edit :

<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>

Save the file and restart the Apache.

sudo systemctl restart apache2

9. Get the app’s demo page

We have configured the Laravel PHP framework to access through the Apache web server over port 80. Hence, simply open your system’s browser that can access the ip-address of Ubuntu 22.04 where you have installed Laravel, and point it to it.

For example:

http://192.168.17.139

Install Laravel PHP framework on Ubuntu 22.04 Jammy

If you want to know how to use the Laravel command line to create and work on various projects without using the Apache web server then here is the tutorial for that. Use the Laravel command line 

Other Articles:

How to Install phpMyAdmin with Apache on Ubuntu 22.04 LTS
Install Composer on Ubuntu 22.04 | 20.04 LTS
How To install Flarum Forum software on Ubuntu 22.04 | 20.04

6 thoughts on “Install Laravel on Ubuntu 22.04 LTS Jammy JellyFish Linux”

  1. Sorry teacher 🙏 whats solution for?
    chown: cannot access ‘var/www/laravel’ : No such file or direcrory

    Please help 🙏🙏🙏

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.