How to Install Laravel on Ubuntu

How to Install Laravel on Ubuntu

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Step 1: Update & Upgrade Your System
  4. Step 2: Install PHP and Required Extensions
  5. Step 3: Install Composer
  6. Step 4: Install Laravel via Composer
  7. Step 5: Configure Your Web Server (Nginx Example)
  8. Step 6: Set Directory Permissions
  9. Step 7: Environment Configuration
  10. Step 8: Run Migrations & Serve
  11. Troubleshooting & Tips
  12. Conclusion

Introduction

Laravel is one of the most popular PHP frameworks for building modern, maintainable web applications. Its elegant syntax, built‑in tools, and vibrant ecosystem make it a go‑to choice for developers worldwide. In this tutorial, you’ll learn how to install Laravel on your Ubuntu server (or local desktop) so you can start building your next project in no time.

Prerequisites

Before diving in, ensure you have the following:

  • Ubuntu 18.04, 20.04, 22.04, or later
  • A user with sudo privileges
  • Basic familiarity with terminal commands
  • Internet connection to download packages
Tip: If you’re on a remote server (e.g., a VPS), connect via SSH:
ssh youruser@your_server_ip

Step 1: Update & Upgrade Your System

Always start by updating your package lists and upgrading installed packages:

sudo apt update
sudo apt upgrade -y

This ensures you have the latest security patches and package versions.

Step 2: Install PHP and Required Extensions

Laravel 10 (the latest as of mid‑2025) requires PHP 8.1 or higher and several extensions:

sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

# Install PHP and extensions
sudo apt install -y \
    php8.2 \
    php8.2-cli \
    php8.2-fpm \
    php8.2-mysql \
    php8.2-xml \
    php8.2-mbstring \
    php8.2-curl \
    php8.2-zip \
    php8.2-gd \
    php8.2-intl \
    unzip
  • php8.2-fpm if you plan to use Nginx
  • php8.2-cli for running Artisan commands
  • Database extensions: MySQL (php*-mysql), PostgreSQL (php*-pgsql), SQLite3 (php*-sqlite3), etc.

Verify your PHP version:

php -v
# Expect: PHP 8.2.x (cli) ...

Step 3: Install Composer

Composer is the de facto dependency manager for PHP:

# Download installer
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php

# Verify signature (optional but recommended)
HASH="$(curl -sS 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;"

# Install globally
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
rm composer-setup.php

# Check version
composer --version
# Expect: Composer version x.y.z

Step 4: Install Laravel via Composer

You have two common methods:

A) Using the Laravel Installer

  1. Install globally:
  2. composer global require laravel/installer
  3. Ensure ~/.composer/vendor/bin (or ~/.config/composer/vendor/bin on some Ubuntu releases) is in your PATH. Add to ~/.bashrc or ~/.zshrc:
  4. export PATH="$HOME/.config/composer/vendor/bin:$PATH"
  5. Create a new project:
  6. laravel new myproject

B) Using Composer Create-Project

If you prefer not to use the installer:

cd /var/www            # or your desired directory
sudo composer create-project laravel/laravel myproject

This clones the starter kit directly into myproject/.

Step 5: Configure Your Web Server (Nginx Example)

Here’s how to set up Nginx to serve your Laravel app. If you prefer Apache, skip to the Permissions step and adjust accordingly.

  1. Install Nginx:
  2. sudo apt install -y nginx
    sudo systemctl enable --now nginx
  3. Create a server block file, e.g., /etc/nginx/sites-available/myproject:
  4. server {
        listen 80;
        server_name yourdomain.com;  # or your_server_ip
    
        root /var/www/myproject/public;
        index index.php;
    
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-Content-Type-Options "nosniff";
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.2-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ /\.(?!well-known).* {
            deny all;
        }
    }
  5. Enable & restart:
  6. sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl reload nginx
  7. Firewall (if UFW is active):
  8. sudo ufw allow 'Nginx Full'

Step 6: Set Directory Permissions

Laravel needs write permissions on storage and bootstrap/cache:

cd /var/www/myproject
sudo chown -R www-data:www-data .
sudo find . -type f -exec chmod 644 {} \;
sudo find . -type d -exec chmod 755 {} \;
sudo chmod -R ug+rwx storage bootstrap/cache

This ensures your web server (usually www-data on Ubuntu) can write logs, cache, and compiled files.

Step 7: Environment Configuration

Copy the example environment file and generate a new key:

cd /var/www/myproject
cp .env.example .env
composer install        # if you used installer and haven’t installed deps yet
php artisan key:generate

Open .env in your favorite editor and set:

APP_NAME="My Laravel App"
APP_ENV=production      # or local
APP_DEBUG=false         # true only for development
APP_URL=http://yourdomain.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead  # or your DB name
DB_USERNAME=homestead
DB_PASSWORD=secret

Save changes when done.

Step 8: Run Migrations & Serve

If you’ve configured your database, migrate:

php artisan migrate

To test locally, you can use the built‑in server:

php artisan serve
# Visit http://127.0.0.1:8000

On production, your Nginx (or Apache) setup handles serving.

Troubleshooting & Tips

  • 500 Error: Check Nginx or Apache error logs (/var/log/nginx/error.log) and Laravel’s storage/logs/laravel.log.
  • Permissions Issues: Double‑check www-data ownership and the writable directories.
  • Missing PHP Extensions: If Artisan complains about a missing extension, install it via sudo apt install php8.2-<extension>.
  • Composer Memory Limit: If you hit memory errors, run:
  • php -d memory_limit=-1 /usr/local/bin/composer update
  • HTTPS: Secure your site with Let’s Encrypt:
  • sudo apt install certbot python3-certbot-nginx
    sudo certbot --nginx -d yourdomain.com

Conclusion

Congratulations! You now have a fully installed Laravel application running on Ubuntu. From here, explore Laravel’s features—Eloquent ORM, Blade templating, queues, events, and more—to build rich web experiences. Remember to keep your system and dependencies up to date, follow security best practices, and enjoy building with Laravel!

#Laravel #Ubuntu #install Laravel on Ubuntu #PHP framework installation #Composer #PHP 8.2 #Nginx #web development tutorial #Laravel setup #Ubuntu server

Subscribe to our newsletter

12k+

Subscribers

Weekly

Frequency

Free

Always