How to Install Laravel on Ubuntu? A Complete Guide for Developers

author
Mayur Upadhyay

Quick Summary

Installing Laravel on Ubuntu involves several steps, including setting up a web server, PHP, a database, and Composer. This guide walks developers through installing Laravel on Ubuntu, covering everything from updating your OS and setting up Apache, PHP, and Composer to configuring a database and environment settings.

When building high-quality web apps with Laravel or other PHP frameworks, most users choose Windows. But, Ubuntu can be more stable and even secure for web development than Windows. It easily handles multiple tasks for a smoother development workflow.

Installing Laravel on Ubuntu requires PHP, Composer, and a web server. One needs to set up a powerful development environment to build great Laravel apps. We’ll explain exactly how the experts install Laravel on Ubuntu in this thorough article. So, let’s begin!

Why Use Laravel on Ubuntu?

The easy syntax and developer-friendly features make Laravel the go-to web development environment in Ubuntu. The better stability, security, and performance characterize this Linux distribution. Also, Ubuntu’s command-line interface gives developers precise control.

Apart from that, its extensive package ecosystem makes essential tools and libraries easy to access. You can install Laravel on Ubuntu, as it comes with APT for advanced package management. This simplifies dependency management and security patching in your IDE.

Prerequisites for Installing Laravel on Ubuntu

Before installing Laravel on Ubuntu, the system must meet these requirements:

  1. Ubuntu OS: Install the latest Ubuntu (18.04 LTS or higher). When installing Laravel on Ubuntu, older versions may not work.
  2. PHP: Latest Laravel versions require PHP 8.1 or higher. For best performance, you will require PHP extensions like mbstring, xml, bcmath, openssl, and fileinfo.
  3. Composer: The Composer dependency manager is important for installing Laravel and its packages. For a step-by-step explanation of this process, refer to our guide on installing Laravel using Composer. Composer installs Laravel globally on Ubuntu.
  4. Web Server: Apache or Nginx is required for production, but optional for local development.
  5. Database: Laravel supports MySQL, PostgreSQL, SQLite, and SQL Server. Select the most suitable one for your project.

Other requirements may apply depending on your project and if you plan to hire Laravel developers for ongoing maintenance.

How to Install Laravel on Ubuntu?

You can install Laravel on Ubuntu after meeting these requirements. The procedure is as follows:

Step 1: Update Ubuntu OS

Make sure your Ubuntu system has the latest updates and packages before installing. Run these terminal commands:

sudo apt update
sudo apt upgrade -y

Laravel installation on Ubuntu requires this step to ensure compatibility with the latest packages and security updates.

Step 2: Install Apache Web Server

Laravel applications are frequently hosted by Apache, a powerful web server. Installing Laravel on Ubuntu requires a reliable web server. Here’s the command to install Apache:

sudo apt install apache2 -y

Once installed, Apache will start automatically. You can check its status by running:

sudo systemctl status apache2
sudo systemctl enable apache2

Step 3: Install PHP and A Few Important Extensions

Modern Laravel versions require PHP 8.1 and above. For optimal Laravel performance, several PHP extensions are recommended. Installing Laravel in Ubuntu requires proper PHP configuration as well.

Install PHP and extensions with this command:

sudo apt install php libapache2-mod-php php-mbstring php-xml php-bcmath php-json php-curl php-zip php-gd php-tokenizer php-fileinfo php-cli -y

For the latest PHP version, add the Ondrej PPA repository:

sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install php8.2 php8.2-cli php8.2-common php8.2-curl php8.2-mbstring php8.2-xml php8.2-zip -y

This command installs PHP and its dependencies. Additional package management tools can provide more control or specific PHP versions.

Step 4: Install Database System

Laravel supports multiple databases. Our guide covers MySQL, but you can also use PostgreSQL or SQLite. Install Ubuntu with MySQL for Laravel:

sudo apt install mysql-server mysql-client -y

Secure your MySQL installation:

sudo mysql_secure_installation

During installation, set a MySQL root password. Keep your password strong. Either you hire an Ubuntu developer or manage the system yourself, this step is crucial.

Step 5: Install Composer

Composer manages PHP dependencies. It is one of the most important web development tools for managing Laravel projects and dependencies. 

Download and install Composer worldwide.

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

Composer installer is downloaded, executed, and placed in a convenient location.

Step 6: Install Laravel Installer Globally

To install Laravel globally on Ubuntu system, use Composer to install the Laravel installer:

composer global require laravel/installer

Put Composer’s global vendor bin directory in PATH:

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

Now you can start Laravel projects using the laravel new command from anywhere in your system.

Step 7: Create a New Laravel Project

Create a new Laravel project with Composer and the Laravel installer. Run in the directory of your choice:

Using Laravel installer:

laravel new my-laravel-app

Or using Composer:

composer create-project --prefer-dist laravel/laravel my-laravel-app

Change my-laravel-app to your project name. When you install Laravel on Ubuntu, this command creates a project directory with the specified name.

Step 8: Set Proper Permissions

Laravel needs storage and bootstrap/cache permissions. Set permissions properly:

cd my-laravel-app
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache

These permissions let the web server read and write these directories.

Step 9: Configure Apache Virtual Host

When installing Laravel on Ubuntu, configure an Apache virtual host to serve your application:

Create a new configuration file:

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

Add the following configuration:

<VirtualHost *:80>
    ServerName your-domain.com
    ServerAdmin webmaster@your-domain.com
    DocumentRoot /var/www/html/my-laravel-app/public
    <Directory /var/www/html/my-laravel-app>
        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 required modules:

sudo a2ensite laravel.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

Step 10: Database Configuration

Database credentials are stored in Laravel environment variables. Check your Laravel project’s root for the .env file.

nano .env

Configure the database connection details:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=your_db_user
DB_PASSWORD=your_db_password

Create the database:

mysql -u root -p
CREATE DATABASE laravel_db;

Step 11: Run Laravel Development Server

For development purposes, you can start the Laravel development server:

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

This launches a development server at http://localhost:8000. Check your Laravel installation in a web browser.

Laravel installation can begin after Ubuntu is ready. Make sure to hire a reliable Laravel development company to set up, deploy, and optimise your server.

Troubleshooting Common Issues 

Installing Laravel on Ubuntu can cause various issues:

  • Permission Errors: Use suitable chmod commands to fix permission denied problems.
  • Composer Memory Issues: Increase the PHP memory limit for large installs.
  • Missing PHP Extensions: Check for missing PHP extensions.
  • Apache Configuration Issues: Check virtual host settings.

Developers installing Laravel in Ubuntu for the first time may encounter these challenges, especially when trying to install Laravel in a subdirectory or configure custom deployment paths.

Conclusion

Successfully installing Laravel on Ubuntu is the starting point of your web development. Build powerful and distributable projects using Ubuntu’s out-of-the-box performance and security. Again, Laravel installation on Ubuntu involves installing the Apache web server, PHP, a database, and Composer, then creating a new project and configuring the virtual host. Make sure that no step has been skipped so your installation is 100% successful.

Need help with Laravel project setup, customization, or scaling? Use our expert support to save time and employ best practices.

FAQs on Installing Laravel on Ubuntu

Can I use a different web server than Apache or Nginx?

Most Ubuntu Laravel installations use Apache or Nginx. Other PHP-supporting web servers like Caddy or LiteSpeed can serve Laravel apps.

How do I deploy my Laravel application to a production server?

After installing Laravel on Ubuntu locally, hosting environment-specific deployment methods apply. Git hooks, Laravel Forge, Envoyer, and permissioned manual file transfers are common methods.

Is it necessary to install a database before installing Laravel?

No, although a database is advised before starting Laravel. Laravel supports many databases.

What’s the difference between a local and global Laravel installation?

The Laravel new command lets you build projects from any location after installing Laravel globally on Ubuntu. Local installation requires Composer’s create-project command each time.

Can I use Laravel Herd on Ubuntu?

Laravel Herd Ubuntu native support is limited, but Docker-based alternatives like Laravel Sail offer a similar development environment.

author
Leading digital transformation projects with precision and creativity, Mayur turns complex ideas into results-driven web solutions. As Project Leader at WPWeb Infotech, he leverages his expertise in Laravel, PHP, Shopify, and Drupal to deliver innovation, scalability, and long-term success.

Want to speed up your Laravel development?

Save valuable time and resources by partnering with our experienced Laravel developers. We help you streamline the setup, improve performance, and deliver scalable Laravel solutions with efficiency.