How to Install Laravel

Here’s a comprehensive guide on how to install the Laravel PHP framework on your server.

Introduction

As a favorite amongst PHP developers, Laravel boasts of an elegant syntax and streamlined development processes. Here, we’ll guide you through the installation process so that you can start developing directly on your server.

Choose from our Laravel HostingVPS Hosting, or Dedicated Servers to host your Laravel applications and websites today!

Prerequisites

Before you dive into Laravel, make sure your server meets the following requirements:

  • PHP version of 8.1 or higher.
    • PHP extensions required: BCMath, Ctype, cURL, DOM, Fileinfo, JSON, Mbstring, OpenSSL, PCRE, PDO, Tokenizer, and XML.
  • A web server such as Apache or Nginx.
  • A database management system such as MySQL, MariaDB, or PostgreSQL.
  • Composer installed. Laravel uses Composer as a package manager to manage its dependencies.

We’re going to help you check that you have all prerequisites installed. If you don’t, we’ll link you to documentation to help install them, ensuring a smooth Laravel installation process.

PHP Version

Check to see if you have the required PHP 8.1 (or above) installed as the system default for your server by running php -v which should output the version.

PHP 8.1.23 (cli) (built: Sep 13 2023 02:57:29) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.23, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.23, Copyright (c), by Zend Technologies

If you need to install a newer PHP version and/or PHP extensions, for servers with cPanel you can Upgrade PHP using EasyApache 4.

Web Server

To check to see if you are running a web server and the version, in your server’s terminal run httpd -v for Apache or nginx -v for Nginx.

$ httpd -v
Server version: Apache/2.4.57 (cPanel)
Server built:   Sep 12 2023 20:22:59

$ nginx -v
nginx version: nginx/1.23.4 (imh_ngx_20230406)

If you don’t see a web server installed, please visit our Apache Tutorials and Nginx Tutorials for installation steps.

Composer

To check to see if Composer is installed on your server by running composer -V in your server’s terminal which should produce output similar to this

Composer version 2.4.4 2022-10-27 14:39:29

If Composer is not installed on your server, you can install it locally for your project or globally by visiting the Composer Installation Documentation.

Use Laravel Installer to Create a Project

The Laravel Installer is a tool that provides a streamlined method for creating a fresh Laravel project. It’s a convenient alternative to the traditional method of installing Laravel through Composer. By using the Laravel Installer, users can swiftly initialize a new Laravel application by executing a simple command in their terminal or command prompt. This simplifies the process, making it more efficient and user-friendly, especially for those who frequently work with Laravel projects. The installer downloads the latest version of Laravel, sets up all the directories and permissions, and makes the application ready for development.

If you prefer to create a Laravel project with the default configuration that you can modify later, you can quickly Create a New Laravel Project with Composer.

As the user for the account that you want to install Laravel, use Composer to install the Laravel Installer.

composer global require laravel/installer --update-with-all-dependencies

You want to make sure that you add $HOME/.config/composer/vendor/bin to your PATH so the Laravel executable is found when you run the Laravel command to create a new project.

For Bash, you can automatically add this to your $HOME/.bashrc file by typing the following

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

You can either log out and back in or run source ~/.bashrc for the changes to take effect

Create a New Laravel Project

Now that you successfully set up the Laravel Installer, you can use the laravel command to create a new Laravel project. Navigate to the document root of the target domain and start the Laravel installer with your_project_name being a name of your choice for the project.

laravel new your_project_name

This will create the directory your_project_name and start the installer.

   _                               _
  | |                             | |
  | |     __ _ _ __ __ ___   _____| |
  | |    / _` | '__/ _` \ \ / / _ \ |
  | |___| (_| | | | (_| |\ V /  __/ |
  |______\__,_|_|  \__,_| \_/ \___|_|


 ┌ Would you like to install a starter kit? ────────────────────┐
 │ › ● No starter kit                                           │
 │   ○ Laravel Breeze                                           │
 │   ○ Laravel Jetstream                                        │
 └──────────────────────────────────────────────────────────────┘

Laravel’s documentation provides detailed information on Starter Kits.

Create a New Laravel Project with Composer

For a simpler and quicker alternative to using the Laravel Installer, you can use the composer command to create a new project. Navigate to the empty document root of the domain you want Laravel installed and initiate your project.

composer create-project laravel/laravel .

Once that completes, you should see the default Laravel files in the current working directory.

If you see an error stating that the directory is not empty

                                                                                                                              
Project directory "/home/producthub/laraveltest.product-content-hub.com/lartest/test-project/test-project/." is not empty.  
                                                                                                                              

Make sure there aren’t any hidden files that may be blocking the installation by running ls -lah to list all files (including hidden files) and directories within your current working directory.

Viewing your New Laravel Installation

To be able to see the default Laravel installation page by visiting your domain, add the following to your .htaccess file in your domain’s document root to make sure that your website is being served from Laravel’s /public directory.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

If you installed Laravel into a subdirectory using the Laravel Installer, you can either move the installed files and directories to the domain’s document root or you can add the following to your .htaccess file in your domain’s document root (replacing your_project_name with the actual directory that Laravel was installed):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ your_project_name/public/$1 [L]
</IfModule>

You should now be able to view your Laravel installation by visiting your domain in your web browser.

Conclusion

Now that you have installed Laravel and confirmed that it was successful, you can begin building your project! The .env file contains different environment configuration values such as database information and application URL that can be edited to fit the needs of your new project.

Derrell Willis
Derrell Willis Manager, Developer Relations

More Articles by Derrell

Was this article helpful? Join the conversation!