How to Install Laravel Latest Version on Ubuntu
Laravel is a popular PHP framework for building web applications with elegant and expressive syntax. It offers many features such as dependency injection, database abstraction, queues, testing, and more. In this tutorial, we will show you how to install Laravel latest version on Ubuntu 20.04 LTS.
Prerequisites
Before you start, you need to have the following:
- A Ubuntu 20.04 machine with sudo access
- Apache web server installed and running
- PHP 8.1 or higher installed with the required extensions
- MariaDB server installed and running
- Composer installed globally
You can check the versions of Apache, PHP, and MariaDB by running the following commands:
apache2 -v
php -v
mysql -V
You can install Composer by following the instructions on its official website.
Step 1: Create a Database for Laravel
Laravel uses a database to store and manage data for your web application. You can use any database system that Laravel supports, such as MySQL, PostgreSQL, SQLite, or SQL Server. In this tutorial, we will use MariaDB as our database system.
To create a database for Laravel, you need to log in to the MariaDB server as the root user or a user with sufficient privileges. You can do this by running the following command:
sudo mysql -u root -p
Enter the password for the root user when prompted.
Next, create a database named laravel
and a user named laraveluser
with a password of your choice. You can do this by running the following commands:
CREATE DATABASE laravel;
CREATE USER 'laraveluser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL ON laravel.* TO 'laraveluser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Replace your_password
with a strong password of your choice.
Step 2: Install Laravel
Now that you have a database ready, you can install Laravel on your Ubuntu machine. The easiest way to install Laravel is to use Composer, a PHP package manager that handles the dependencies and installation of Laravel.
To install Laravel, you need to navigate to the web root directory of your Apache server, which is usually /var/www/html
. You can do this by running the following command:
cd /var/www/html
Next, use the Composer create-project
command to download and install Laravel in a directory named laravel
. You can do this by running the following command:
sudo composer create-project --prefer-dist laravel/laravel
This will create a new Laravel project in the laravel
directory with all the required files and dependencies.
Step 3: Configure Laravel
After installing Laravel, you need to configure some settings for your web application. The main configuration file for Laravel is .env
, which is located in the root directory of your Laravel project. This file contains various settings such as database credentials, app name, app URL, app key, and more.
To configure Laravel, you need to edit the .env
file with your favorite text editor. You can do this by running the following command:
sudo nano /var/www/html/laravel/.env
In the .env
file, you need to change the following settings:
APP_NAME
: The name of your web application. You can set it to anything you like, such asLaravel Blog
.APP_URL
: The URL of your web application. You need to set it to the domain name or IP address of your Ubuntu machine, such ashttp://example.com
orhttp://192.168.0.10
.DB_DATABASE
: The name of the database you created for Laravel. You need to set it tolaravel
.DB_USERNAME
: The username of the database user you created for Laravel. You need to set it tolaraveluser
.DB_PASSWORD
: The password of the database user you created for Laravel. You need to set it to the password you chose.
The rest of the settings can be left as default, unless you want to change them for your specific needs.
After editing the .env
file, save and close it by pressing Ctrl+O
and Ctrl+X
.
Step 4: Configure Apache
The last step is to configure Apache to serve your Laravel web application. You need to create a virtual host file for your Laravel web application and enable the rewrite module for Apache.
To create a virtual host file for your Laravel web application, you need to create a new file named laravel.conf
in the /etc/apache2/sites-available
directory. You can do this by running the following command:
sudo nano /etc/apache2/sites-available/laravel.conf
In the laravel.conf
file, you need to paste the following configuration:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html/laravel/public
<Directory /var/www/html/laravel>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/laravel_error.log
CustomLog ${APACHE_LOG_DIR}/laravel_access.log combined
</VirtualHost>
Replace example.com
and www.example.com
with your domain name or IP address.
This configuration tells Apache to serve your Laravel web application from the /var/www/html/laravel/public
directory, which is the public directory of your Laravel project. It also allows the .htaccess
file in the Laravel directory to override the Apache settings, which is necessary for the Laravel routing to work properly.
After creating the laravel.conf
file, save and close it by pressing Ctrl+O
and Ctrl+X
.
Next, enable the virtual host file and the rewrite module for Apache by running the following commands:
sudo a2ensite laravel.conf
sudo a2enmod rewrite
Finally, restart the Apache service to apply the changes by running the following command:
sudo systemctl restart apache2
Step 5: Access Laravel from a Browser
Now that you have installed and configured Laravel on your Ubuntu machine, you can access your Laravel web application from a browser. Open your web browser and type the URL of your Laravel web application, such as http://example.com
or http://192.168.0.10
. You should see the default Laravel welcome page, which means that your Laravel installation is successful.
Congratulations, you have successfully installed Laravel latest version on Ubuntu 20.04 LTS. You can now start developing your web application with Laravel. Happy coding!