How to Install Laravel on Ubuntu 22.04

how to install laravel on ubuntu 22.04

In this tutorial, we are going to explain in step-by-step detail how to install Laravel on the latest Ubuntu 22.04 distribution.

Laravel is an open-source PHP framework developed for faster implementation and development of web applications along with many built-in features and many libraries. Laravel framework is based on Symfony and is following the MVC architectural pattern. The framework is created by Taylor Otwell and its source code is hosted on GitHub.

Installing Laravel on Ubuntu 22.04 is a very easy and straightforward process with the Composer package manager that can take up to 10 minutes. Let’s get this working!

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

Every fresh installation of the Ubuntu 22.04 requires the system packages to be updated to the latest versions available.

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install Apache Web Server

Install the Apache Web server with the following command:

sudo apt install apache2

Once, installed start and enable the service.

sudo systemctl enable apache2 && sudo systemctl start apache2

Check if the service is up and running:

sudo systemctl status apache2

You should receive the following output:

root@host:~# 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 Fri 2022-05-06 09:01:22 UTC; 4h 24min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 658 (apache2)
      Tasks: 8 (limit: 4566)
     Memory: 19.6M
        CPU: 2.320s
     CGroup: /system.slice/apache2.service
             ├─  658 /usr/sbin/apache2 -k start
             ├─  681 /usr/sbin/apache2 -k start
             ├─  682 /usr/sbin/apache2 -k start
             ├─  683 /usr/sbin/apache2 -k start
             ├─  684 /usr/sbin/apache2 -k start
             ├─  685 /usr/sbin/apache2 -k start
             ├─13745 /usr/sbin/apache2 -k start
             └─13746 /usr/sbin/apache2 -k start

May 06 17:01:21 host.test.vps systemd[1]: Starting The Apache HTTP Server...
May 06 17:01:22 host.test.vps systemd[1]: Started The Apache HTTP Server.

Step 3. Install PHP8.1 with dependencies

To install the PHP8.1 along with extensions execute the following command:

sudo apt-get install php8.1 php8.1-cli php8.1-common php8.1-imap php8.1-redis php8.1-snmp php8.1-xml php8.1-zip php8.1-mbstring php8.1-curl

Step 4. Install Composer

We need to install the Composer responsible for installing all the Laravel components.

curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer

Check the Composer installation:

composer

You should receive the following output:

root@vps:~# composer
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.3.5 2022-04-13 16:43:00

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display help for the given command. When no command is given display help for the list command
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi|--no-ansi           Force (or disable --no-ansi) ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
      --no-scripts               Skips the execution of all scripts defined in composer.json file.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Step 5. Install Laravel

Go into the document root of the website and install Laravel with the composer. In this blog post we will use the default apache2 document root:

cd /var/www/html/

composer create-project laravel/laravel "YOUR APPLICATION NAME"

We will use test-project as the application name. Once the command for creating the application is executed you should receive the following output:

root@vps:/var/www/html# composer create-project laravel/laravel test-project
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Creating a "laravel/laravel" project at "./test-project"
Info from https://repo.packagist.org: #StandWithUkraine
Installing laravel/laravel (v9.1.7)
  - Downloading laravel/laravel (v9.1.7)
  - Installing laravel/laravel (v9.1.7): Extracting archive
Created project in /var/www/html/test-project
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information

After successful installation you should receive the following output:

Package manifest generated successfully.
78 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 --force
No publishable resources for tag [laravel-assets].
Publishing complete.
> @php artisan key:generate --ansi
Application key set successfully.

Go into the test-project directory and set the correct permissions:

cd /var/www/html/test-project

chown -R www-data:www-data .

chmod -R 775 storage/

Step 6. Create Apache Virtual Host File

Go into the Apache directory where the configuration files are stored and create a configuration file for the Laravel application.

cd /etc/apache2/sites-available/

touch laravel.conf

Paste the following lines of code, save the file and close it.

<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /var/www/html/test-project/public

<Directory /var/www/html/test-project>
AllowOverride All
</Directory>

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

</VirtualHost>

Enable the Apache configuration for Laravel.

a2ensite laravel.conf

Check the syntax:

apachectl -t

You should receive the following output:

root@vps:~# apachectl -t
Syntax OK

If the syntax is OK, restart the Apache service.

systemctl reload apache2

Once, the Apache service is restarted you can access the Laravel website at http://yourdomain.com

Congratulations! You successfully installed and configured Laravel on Ubuntu 22.04 with Composer and Apache as a web server.

Of course, if you find some difficulties while installing Laravel you can always contact our system admins and with their expertise, they will install Laravel on Ubuntu 22.04 for you. All you need to do is to contact our support. We are available 24/7.

If you liked this post on how to install Laravel on Ubuntu 22.04, please share it with your friends on social networks or simply leave a reply below. Thanks.

Leave a Comment