Installing Laravel on Linux Ubuntu
This article explains how to install Laravel, prerequisites and the MySQL database server on Linux Ubuntu.
Installing the prerequisites and the Laravel installer
First, we will install the curl tool. Open a terminal window and write:
sudo apt install curl
Install PHP, Composer and Laravel Installer using curl and a remote installation script:
/bin/bash -c "$(curl -fsSL https://php.new/install/linux/8.4)"
Restart the terminal window. This step is important, please do not skip it.
If you already have PHP and Composer, you can install the Laravel installer using the Composer dependency package manager tool:
composer global require laravel/installer
Installing Node.js and accompanying tools
Installing Node.js for use in Laravel is not strictly required but is advisable when developing and compiling CSS and JS assets using Laravel.
Install the nvm (Node Version Manager) tool:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
Restart the terminal.
Install Node.js using the nvm tool.
nvm install 22
Here, we opted to install the Node.js version 22. This command also installed the npm (Node Package Manager) tool.
Update the npm tool to the latest version:
npm install -g npm@latest
Installing MySQL server on Linux
Laravel supports multiple databases such as SQLite, MySQL, MariaDB, PostgreSQL and SQL Server databases. We will opt for the MySQL database as it is often used by web hosting providers.
To install the MySQL server on Linux, open a terminal and write:
sudo apt install mysql-server
Log in to a local MySQL server as a root user:
sudo mysql -u root
For simplicity reasons, we can remove the password for the root user. Execute these two commands:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
FLUSH PRIVILEGES;
To exit the MySQL prompt/server, type the following and press Enter:
exit;
Now, we can connect to a local MySQL server without a password and without the need for the sudo command. In the terminal, we write:
mysql -u root
Let us create a MySQL database which can have the same name as our Laravel project - myapp:
create database myapp;
Exit MySQL:
exit;
Important: In production, there should be both a username and a strong password for a database. For simplicity reasons, on a local MySQL server, we have removed the password for now.
Creating a Laravel project
Now, we can create a new Laravel application/project. In the terminal, we write:
laravel new myapp
In the dialog that asks "Which starter kit would you like to install?" choose None and press Enter.
The Laravel installer will now create the myapp folder and install the Laravel-related packages and files into the myapp folder.
In the next dialog that asks "Which database will your application use?", choose MySQL and press Enter.
In the next dialog that says "Default database updated. Would you like to run the default database migrations?" choose Yes and press Enter. This command will create a few default tables inside our myapp MySQL database we created earlier.
In the next dialog that says Would you like to run npm install and npm run build? Choose Yes and press Enter.
We should see the following message:
"INFO Application ready in [myapp]. You can start your local development using:..."
This indicates that we have successfully created the Laravel project, installed all the dependencies and now we can start the local server.
Navigate to the myapp/ folder:
cd myapp
And start a local server using the following command:
composer run dev
This command starts a local web server, usually with the address of http://127.0.0.1:8000, which is the same as: http://localhost:8000.
Please note that you should keep this terminal window open as long as you want your local server running. To stop the server, in the terminal window, press Ctrl + C key combination.
Open a web browser and enter the http://127.0.0.1:8000 URL in the browser's address bar. You should see the default Laravel welcome page.
In our next lesson, we will install an editor and learn how to edit the project files.