How To Install Laravel on Ubuntu 26.04 LTS

Install Laravel on Ubuntu 26.04

Installing Laravel on a fresh Ubuntu server looks simple on paper, but small mistakes can break routing, permissions, or database access later. This guide shows you how to Install Laravel on Ubuntu 26.04 the right way, with each command explained so you understand both the how and the why.

I wrote this from a Linux sysadmin point of view, so the focus is not just getting Laravel to run. The goal is to build a clean, secure, and maintainable setup that works for beginners, developers, and sysadmins who want a proper Ubuntu deployment.

You will set up PHP, Composer, MySQL, and a web server, then connect everything into a working Laravel app. Along the way, you will also learn how to configure Laravel on Ubuntu 26.04 in a way that avoids common production mistakes.

Prerequisites

  • Ubuntu 26.04 LTS installed and updated.
  • A user with sudo access.
  • SSH access or terminal access to the server.
  • A domain name if you want to point Laravel to a public site.
  • Basic comfort with Linux commands.
  • Enough disk space for PHP packages, Composer, and your project files.

Step 1: Update Your System

Keeping the server updated reduces the chance of package conflicts and security issues. It also makes sure Ubuntu can pull the latest available package metadata before you install anything.

Update package lists

sudo apt update

This command refreshes the local package index. In simple terms, it tells Ubuntu what software versions are available right now.

Upgrade installed packages

sudo apt upgrade -y

This applies available updates to your current system packages. The -y flag skips the confirmation prompt, which is useful during a clean server setup.

Expected output

Reading package lists... Done
Building dependency tree... Done
Calculating upgrade... Done

Why this step matters

Laravel depends on PHP extensions, database libraries, and system tools. If your base system is outdated, later installs can fail because of version mismatches.

Step 2: Install PHP and Required Extensions

Laravel runs on PHP, so this is the core of the whole setup. Ubuntu 26.04 LTS ships with a modern PHP version, which makes it a strong base for a current Laravel install.

Install PHP packages

sudo apt install php php-cli php-fpm php-mysql php-xml php-mbstring php-curl php-zip php-bcmath php-gd unzip git -y

This installs PHP plus the most common extensions Laravel needs. These packages support database access, string handling, HTTP requests, archives, math functions, and image work.

Verify PHP version

php -v

Expected output

PHP 8.5.x (cli) (built: ...)
Copyright (c) The PHP Group

Why each extension matters

  • php-cli lets you run Artisan commands from the terminal.
  • php-fpm helps Nginx or Apache run PHP efficiently.
  • php-mysql connects Laravel to MySQL or MariaDB.
  • php-xml supports XML processing used by Composer and some packages.
  • php-mbstring helps Laravel handle UTF-8 text correctly.
  • php-curl is useful for API calls and HTTP requests.
  • php-zip helps Composer extract downloaded packages.
  • php-bcmath supports precise math operations.
  • php-gd is useful for image handling.

If one of these is missing, Laravel may install but still break later during runtime.

Step 3: Install Composer

Composer is the dependency manager for PHP. Laravel uses it to install framework files, libraries, and project dependencies.

Download the installer

cd ~
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

This downloads the official Composer installer script to your home directory.

Verify the installer

HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

This checks that the installer file has not been changed or damaged. That matters because Composer becomes part of your server toolchain.

Install Composer globally

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

This puts composer in your system path, so you can run it from anywhere.

Clean up

php -r "unlink('composer-setup.php');"
composer --version

Without Composer, you cannot create a Laravel project properly. It also ensures version control over your PHP packages, which is important for stable deployments.

Step 4: Install and Secure MySQL

Laravel needs a database for users, sessions, jobs, and application data. MySQL is a common choice because it is stable and widely supported.

Install MySQL

sudo apt install mysql-server -y

This installs the database engine and supporting files.

Start and enable the service

sudo systemctl enable --now mysql

This makes sure MySQL starts now and also boots automatically after a server restart.

Secure the installation

sudo mysql_secure_installation

This script helps remove weak defaults, such as anonymous users and unsafe remote root access.

Create a database and user

sudo mysql -u root -p
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'StrongPassword!';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

A dedicated database user protects your server. If the app is ever compromised, the attacker only gets access to the one database you assigned.

Step 5: Install a Web Server

You can run Laravel with Apache or Nginx. Both work well, but they handle requests differently.

Apache option

sudo apt install apache2 libapache2-mod-php -y
sudo a2enmod rewrite
sudo systemctl enable --now apache2
sudo systemctl restart apache2

Apache is easier for many beginners because it handles PHP more directly. The rewrite module is important because Laravel depends on URL rewriting for routing.

Nginx option

sudo apt install nginx -y
sudo systemctl enable --now nginx

Nginx usually pairs with PHP-FPM instead of handling PHP directly. That setup is often faster and cleaner on busy Linux servers.

Laravel routes all web traffic through public/index.php. Without the right web server config, clean URLs will fail and your app will not behave correctly.

Step 6: Create the Laravel Project

Now you can create the app itself. This is the point where Laravel files get placed on disk and the framework skeleton is generated.

Create a project with Composer

cd /var/www/
sudo composer create-project --prefer-dist laravel/laravel myapp

This downloads Laravel and its dependencies into a new folder named myapp.

Expected output

Creating a "laravel/laravel" project at "./myapp"
Installing laravel/laravel ...

Alternative with Laravel installer

composer global require laravel/installer
echo 'export PATH="$HOME/.config/composer/vendor/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
laravel new myapp

composer create-project is the simplest and most predictable method. It gives you a fresh install with the current Laravel version and avoids manual file setup.

Step 7: Set File Permissions

Laravel writes to storage and bootstrap/cache, so those folders need correct permissions. This is one of the most common sources of new install errors.

Set ownership

sudo chown -R www-data:www-data /var/www/myapp

This makes the web server user own the project files.

Set general permissions

sudo chmod -R 755 /var/www/myapp

This allows read and execute access for most directories.

Allow Laravel to write cache files

sudo chmod -R 775 /var/www/myapp/storage
sudo chmod -R 775 /var/www/myapp/bootstrap/cache

These folders need write access because Laravel stores logs, cache files, and temporary data there.

If permissions are too strict, Laravel throws write errors. If they are too loose, you create a security risk. The goal is balance, not maximum access.

Step 8: Configure the Environment File

The .env file tells Laravel how to connect to the database and how the app should behave. This file is one of the most important parts of the install.

Copy the example file

cd /var/www/myapp
cp .env.example .env

This creates your active environment file from Laravel’s template.

Generate the application key

php artisan key:generate

This creates the encryption key Laravel needs for cookies and encrypted data.

Edit the config

nano .env
APP_NAME=MyApp
APP_ENV=production
APP_DEBUG=false
APP_URL=http://your-domain.com

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

APP_DEBUG=false keeps sensitive errors hidden from visitors. The database settings tell Laravel where to connect, and the app key keeps encrypted data secure.

Step 9: Configure the Virtual Host

This is where you connect your web server to Laravel. The most important rule is to point the document root to Laravel’s public directory, not the project root.

Apache virtual host

Create the config file:

sudo nano /etc/apache2/sites-available/myapp.conf
<VirtualHost *:80>
    ServerName your-domain.com
    ServerAlias www.your-domain.com
    DocumentRoot /var/www/myapp/public

    <Directory /var/www/myapp/public>
        AllowOverride All
        Require all granted
    </Directory>

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

Enable the site:

sudo a2ensite myapp.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Nginx server block

Create the config file:

sudo nano /etc/nginx/sites-available/myapp
server {
    listen 80;
    server_name your-domain.com;
    root /var/www/myapp/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.5-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Enable it:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Laravel keeps sensitive app files outside the web root. Pointing the server at public protects the rest of the application and keeps routing working correctly.

Step 10: Run Migrations and Test

Now you can create the database tables Laravel needs and confirm that the install works.

Run migrations

cd /var/www/myapp
php artisan migrate

This creates the default tables in your database.

Test the app

Open your browser and visit your domain or server IP. You should see the Laravel welcome page.

Optional local test

php artisan serve

This starts Laravel’s built-in development server. It is useful for testing, but it should not be used in production.

Migrations build the app schema. Without them, Laravel can boot, but database features will fail when the app tries to read or write data.

Install Laravel on Ubuntu 26.04

Troubleshooting

Even a clean How To Install Laravel on Ubuntu 26.04 setup can hit a few issues. These are the most common ones.

1. Composer install fails

If Composer stops with an error, check that PHP CLI and required extensions are installed.

php -v
composer --version

If the Laravel installer itself fails on Ubuntu 26.04, try:

composer global require laravel/installer --prefer-source

2. 403 Forbidden in browser

This usually means the document root is wrong or Apache rewrite is off.

sudo a2enmod rewrite
sudo systemctl restart apache2

For Nginx, confirm the root points to /var/www/myapp/public.

3. SQLSTATE access denied

This means Laravel cannot connect to MySQL. Recheck the username, password, and database name in .env.

nano /var/www/myapp/.env

Then clear the config cache if needed.

php artisan config:clear

4. Permission denied on storage or cache

Laravel needs write access to storage and bootstrap/cache.

sudo chown -R www-data:www-data /var/www/myapp
sudo chmod -R 775 /var/www/myapp/storage
sudo chmod -R 775 /var/www/myapp/bootstrap/cache

5. Blank page or 500 error

This often points to a bad .env value, missing app key, or PHP extension issue.

php artisan key:generate
php artisan config:clear

Then check the server error log.

r00t is a Linux Systems Administrator and open-source advocate with over ten years of hands-on experience in server infrastructure, system hardening, and performance tuning. Having worked across distributions such as Debian, Arch, RHEL, and Ubuntu, he brings real-world depth to every article published on this blog. r00t writes to bridge the gap between complex sysadmin concepts and practical, everyday application — whether you are configuring your first server or optimizing a production environment. Based in New York, US, he is a firm believer that knowledge, like open-source software, is best when shared freely.

Related Posts