How to Install Laravel on Ubuntu

Install Laravel on Ubuntu

Are you ready to dive into the world of modern PHP development? Installing Laravel on Ubuntu might seem daunting at first, but I’m here to walk you through every single step of the process. By the end of this comprehensive guide, you’ll have a fully functional Laravel development environment running on your Ubuntu system.

Laravel has become the go-to PHP framework for developers worldwide, and for good reason. With over 70,000 stars on GitHub and being used by companies like Disney, The New York Times, and Pfizer, it’s clear that Laravel isn’t just another framework – it’s a game-changer in web development.

What is Laravel and Why Use It?

Laravel is an elegant, expressive PHP web framework that makes development a breeze. Think of it as your Swiss Army knife for web development – it comes packed with everything you need to build modern web applications without reinventing the wheel.

What sets Laravel apart from other PHP frameworks? Here’s the deal:

  • Elegant syntax: Laravel’s code reads almost like English, making it incredibly intuitive
  • Built-in features: Authentication, routing, sessions, and caching come out of the box
  • Artisan CLI: A powerful command-line interface that automates repetitive tasks
  • Eloquent ORM: Makes database interactions feel natural and effortless
  • Blade templating: A lightweight yet powerful templating engine

According to the 2024 Stack Overflow Developer Survey, Laravel ranks as the 4th most loved web framework, with 62.7% of developers expressing satisfaction with it. That’s not just a number – that’s a testament to how Laravel makes developers’ lives easier.

Prerequisites for Installing Laravel on Ubuntu

Before we jump into the installation process, let’s make sure you have everything you need. Think of this as gathering your tools before starting a home improvement project.

System Requirements

Your Ubuntu system needs to meet these minimum requirements:

  • Ubuntu 18.04 LTS or newer (Ubuntu 22.04 LTS recommended)
  • At least 2GB RAM (4GB recommended for comfortable development)
  • Minimum 10GB free disk space
  • Internet connection for downloading packages

Required Software and Tools

Here’s what we’ll be installing throughout this guide:

  • PHP 8.1 or higher with required extensions
  • Composer (PHP dependency manager)
  • Node.js and NPM for frontend asset compilation
  • A web server (Apache or Nginx)
  • MySQL or PostgreSQL (optional but recommended)

Don’t worry if you don’t have these installed yet – we’ll cover everything step by step.

Step-by-Step Guide to Install Laravel on Ubuntu

Now comes the fun part! Let’s get our hands dirty and install Laravel on your Ubuntu system. I’ll guide you through each step with clear commands and explanations.

Step 1: Update Your Ubuntu System

First things first – let’s make sure your Ubuntu system is up to date. This is like checking your car’s oil before a long road trip.

Open your terminal (Ctrl+Alt+T) and run these commands:

sudo apt update
sudo apt upgrade -y

The update command refreshes your package list, while upgrade installs any available updates. This ensures you’re working with the latest versions of system packages.

Step 2: Install PHP and Required Extensions

Laravel requires PHP 8.1 or higher, so let’s install PHP along with the necessary extensions. Think of these extensions as specialized tools that Laravel needs to function properly.

sudo apt install php8.2 php8.2-cli php8.2-common php8.2-mysql php8.2-zip php8.2-gd php8.2-mbstring php8.2-curl php8.2-xml php8.2-bcmath php8.2-tokenizer -y

Here’s what each extension does:

  • php8.2-cli: Command-line interface for PHP
  • php8.2-mysql: MySQL database support
  • php8.2-zip: ZIP archive handling
  • php8.2-gd: Image processing capabilities
  • php8.2-mbstring: Multibyte string handling
  • php8.2-curl: HTTP request functionality
  • php8.2-xml: XML processing support
  • php8.2-bcmath: Arbitrary precision mathematics
  • php8.2-tokenizer: PHP code tokenization

Verify your PHP installation:

php --version

You should see something like “PHP 8.2.x” in the output.

Step 3: Install Composer

Composer is Laravel’s best friend – it manages all PHP dependencies automatically. Installing Composer is like getting a personal assistant for your PHP projects.

Download and install Composer:

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

Verify the installation:

composer --version

You should see the Composer version information displayed.

Step 4: Install Node.js and NPM

While Laravel is a PHP framework, modern web development often requires JavaScript tools for frontend asset compilation. Node.js and NPM are essential for this.

Install Node.js and NPM:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

Verify the installation:

node --version
npm --version

Step 5: Install Laravel via Composer

Now for the main event! Let’s install Laravel globally so you can create new projects easily from anywhere on your system.

composer global require laravel/installer

Add Composer’s global bin directory to your PATH:

echo 'export PATH="$HOME/.config/composer/vendor/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Verify Laravel installation:

laravel --version

Step 6: Create Your First Laravel Project

Time to create your first Laravel project! This is like planting your first seed in a garden.

cd ~/
laravel new my-first-laravel-app
cd my-first-laravel-app

Alternatively, you can create a project using Composer directly:

composer create-project laravel/laravel my-first-laravel-app
cd my-first-laravel-app

Configuring Your Laravel Environment

Your Laravel application needs some configuration to work properly. Think of this as setting up your workspace before starting a project.

Setting Up Environment Variables

Laravel uses a .env file for environment-specific configuration. This file is like your application’s personal settings menu.

cp .env.example .env
php artisan key:generate

The key:generate command creates a unique application key that Laravel uses for encryption and security.

Database Configuration

If you plan to use a database (which most applications do), you’ll need to configure it in your .env file.

For MySQL, install it first:

sudo apt install mysql-server -y
sudo mysql_secure_installation

Then edit your .env file:

nano .env

Update these database settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password

Installing and Configuring a Web Server

To serve your Laravel application, you’ll need a web server. I’ll show you how to configure both Apache and Nginx – choose the one you prefer.

Apache Configuration for Laravel

Apache is like the reliable old friend of web servers – it’s been around forever and just works.

Install Apache:

sudo apt install apache2 -y
sudo a2enmod rewrite

Create a virtual host for your Laravel application:

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

Add this configuration:

<VirtualHost *:80>
    ServerName laravel.local
    DocumentRoot /home/your-username/my-first-laravel-app/public

    <Directory /home/your-username/my-first-laravel-app/public>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/laravel_error.log
    CustomLog ${APACHE_LOG_DIR}/laravel_access.log combined
</VirtualHost>

Enable the site and restart Apache:

sudo a2ensite laravel.conf
sudo systemctl restart apache2

Nginx Configuration for Laravel

Nginx is the sports car of web servers – fast, efficient, and modern.

Install Nginx:

sudo apt install nginx -y

Create a server block:

sudo nano /etc/nginx/sites-available/laravel

Add this configuration:

server {
    listen 80;
    server_name laravel.local;
    root /home/your-username/my-first-laravel-app/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Enable the site:

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Common Installation Issues and Solutions

Even the smoothest installations can hit bumps. Here are the most common issues you might encounter and how to solve them.

Permission Problems

Laravel needs write permissions to certain directories. If you see permission errors, run:

sudo chown -R $USER:www-data storage bootstrap/cache
chmod -R 775 storage bootstrap/cache

PHP Extension Errors

If Laravel complains about missing PHP extensions, install them:

sudo apt install php8.2-[extension-name]
sudo systemctl restart apache2  # or nginx

Composer Installation Issues

If Composer fails to install packages, try:

composer install --no-scripts
composer update
php artisan key:generate

Testing Your Laravel Installation

Let’s make sure everything is working correctly. Start the Laravel development server:

php artisan serve

Open your browser and navigate to http://localhost:8000. You should see the beautiful Laravel welcome page – that’s your sign of success!

For production-like testing, add this to your /etc/hosts file:

sudo nano /etc/hosts

Add:

127.0.0.1 laravel.local

Now you can visit http://laravel.local in your browser.

Best Practices for Laravel Development on Ubuntu

Now that you have Laravel running, here are some best practices to follow:

  1. Use version control: Initialize a Git repository for your project
  2. Environment separation: Never commit your .env file to version control
  3. Regular backups: Back up your database and code regularly
  4. Keep dependencies updated: Run composer update regularly
  5. Use Laravel’s built-in tools: Leverage Artisan commands for common tasks

Consider setting up additional tools like:

  • Laravel Valet for easy local development
  • Laravel Telescope for debugging and monitoring
  • Laravel Horizon for queue monitoring
  • PHPUnit for testing

Frequently Asked Questions

1. What version of PHP do I need for Laravel?

Laravel 10.x requires PHP 8.1 or higher, while Laravel 11.x requires PHP 8.2 or higher. I recommend using the latest stable PHP version available for your Ubuntu distribution to ensure optimal performance and security.

2. Can I install Laravel without Composer?

While technically possible, it’s not recommended. Composer is the standard dependency manager for PHP and the officially supported way to install Laravel. It handles all dependencies automatically and makes updates much easier.

3. Should I use Apache or Nginx for Laravel?

Both work excellently with Laravel. Apache is more beginner-friendly with its .htaccess support, while Nginx offers better performance for high-traffic applications. For development, either choice is fine – go with what you’re more comfortable with.

4. How do I update Laravel after installation?

To update Laravel, use Composer: composer update for minor updates, or follow the upgrade guide in Laravel documentation for major version upgrades. Always backup your project before updating.

5. What should I do if I encounter permission errors?

Permission errors are common with Laravel. Make sure your web server user has the right permissions: sudo chown -R $USER:www-data storage bootstrap/cache and chmod -R 775 storage bootstrap/cache. These directories need write permissions for Laravel to function properly.

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts