How to Install Laravel with LAMP on Ubuntu 22.04

Introduction

Laravel is a web application framework with expressive, elegant syntax. Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as authentication, routing, sessions, and caching.

Pre-requisites

  • A system with Ubuntu 22.04 installed and running.
  • root access to the system.
  • LAMP Stack installed and running. For this, you can refer to one of our guides on installing the LAMP Stack (Apache, MariaDB, and PHP).

Install Git

Git is required to clone the Laravel repository. Install Git with:

apt -y install git
git --version

Output:

root@vps:~# git --version
git version 2.34.1

Install PHP 8.3 and Required Extensions

By default, Ubuntu 22.04 comes with PHP 8.1, but Laravel requires PHP 8.2 or higher. Install PHP 8.3 and necessary extensions,

apt install -y software-properties-common
add-apt-repository ppa:ondrej/php -y
apt update
apt install -y unzip php8.3 php8.3-zip php8.3-mysql php8.3-xml php8.3-curl php8.3-mbstring

Switch to PHP 8.3,

update-alternatives --config PHP

Verify installed PHP modules,

php -m

Install Composer

Composer is a dependency manager for PHP, essential for Laravel. Install it with,

php -r "copy('http://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
mv composer.phar /usr/local/bin/composer

Verify the installation,

composer -V

Output:

root@vps:~# composer -V
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.8.6 2025-02-25 13:03:50
PHP version 8.3.19 (/usr/bin/php8.3)
Run the "diagnose" command to get more detailed diagnostics output.
root@vps:~# 

Set Up Laravel

Configure Apache and Clone Laravel

cd /var/www/html
rm index.html
chown -R $USER:$USER /var/www/html

Clone Laravel repository,

git clone https://github.com/laravel/laravel.git /var/www/html

Install Laravel Dependencies

Navigate to Laravel directory and install dependencies,

cd /var/www/html
composer install

Setup the environment file,

cp .env.example .env

Set correct permissions,

chown -R www-data:www-data /var/www/html/storage

Generate application key,

php artisan key:generate

Configure Database

Create MySQL Database and User

Access MySQL,

mysql -uroot

Create a new database and user,

CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'Stront_Password_Here';
GRANT ALL PRIVILEGES ON *.* TO 'laravel_user'@'localhost';
CREATE DATABASE laravel_db;
SHOW DATABASES;
exit;

Note: Replace Strong_Password_Here with actual Strong Password

Configure Database in Laravel

Open the .env file and update the database details:

nano .env

Find and update the following lines,

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=Strong_Password_Here

Configure Apache Virtual Host

To make Laravel accessible through a domain, configure Apache,

nano /etc/apache2/sites-available/laravel.conf

Add the following configuration,

<VirtualHost *:80>
    ServerAdmin laravel.domainhere.info
    ServerName laravel.domainhere.info
    DocumentRoot /var/www/html/public

    <Directory /var/www/html>
        AllowOverride All
        Require all granted
    </Directory>

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

Enable the site and rewrite module,

a2ensite laravel.conf
a2enmod rewrite
systemctl restart apache2

Run Migrations

Apply the database migrations,

php artisan migrate

Restart Apache for changes to take effect,

systemctl restart apache2

Access Laravel Application

You can configure your domain name in the Apache configuration file to access Laravel using your domain.

Once configured, open your domain in a web browser. This will direct you to the Laravel landing page on your server:

http://dev.domainhere.info

Replace dev.domainhere.info with the actual IP or domain configured on the server.

If you have not configured a domain name and want to access Laravel using your server’s IP address, run the following command:

php artisan serve --host=0.0.0.0 --port=8000

Then, open the Laravel application in your browser:

http://your-server-ip:8000

Replace your-server-ip with your actual server IP address.

This makes Laravel accessible from any machine on the network.

Conclusion

You have successfully installed Laravel on your Ubuntu 22.04 server with a LAMP stack. Laravel is now ready for development, and you can start building your application. Be sure to configure your Apache settings or use a reverse proxy if you plan to deploy your project to production.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS