How to Install Laravel on Ubuntu 20.04

Install Laravel on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure Laravel on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Laravel is a very popular open-source PHP framework with the expressive and elegant syntax used to design modern and beautiful web applications. Laravel aims to take the pain out of web development and make it an enjoyable and creative experience, turning web devs into web artisans.

Install Laravel on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install LAMP Stack.

You need to setup LAMP stack on your Ubuntu system. If you haven’t done so already, use our traditional LAMP guide to set up Ubuntu to serve PHP before you continue.

Step 3. Install Composer.

Composer a dependency manager tool for PHP programming language. We will use it to download and install the Laravel Core and all the required Laravel components:

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

Finally, verify that the composer installed correctly by running:

composer -V

Step 4. Install Laravel on Ubuntu system.

First, clone the master branch of the Laravel from GitHub repository:

cd /var/www
git clone https://github.com/laravel/laravel.git

Next, change the Laravel directory and use the composer to install all dependencies required for the Laravel framework:

cd /var/www/laravel
sudo composer install

Then, set the directory permissions accordingly:

chown -R www-data.www-data /var/www/laravel
chmod -R 755 /var/www/laravel
chmod -R 777 /var/www/laravel/storage

Step 5. Create Environment Settings.

Now we create the Laravel environment configuration file:

mv .env.example .env

Then, generate base64 random number encryption key:

$ php artisan key:generate

Application key set successfully.

AFter generate random encryption, now we edit the .env configuration file and update the required settings. Also, make sure APP_KEY is properly set as generated in the above command:

nano .env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:meilanap+AeHu7kc2chedelicsq2BQ/1gfFWEpoAk=
APP_DEBUG=true
APP_URL=http://localhost
...

Step 6. Creating a MariaDB database for Laravel.

Laravel uses MariaDB database to store all its data like posts, pages, users, plugins and themes settings. Log in to your MariaDB server with the following command and enter your MariaDB root password:

mysql -u root -p

Once you’re in the MariaDB console, create a new database:

CREATE DATABASE laravel;
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'secret-passwd';
GRANT ALL ON laravel.* to 'laravel'@'localhost';
FLUSH PRIVILEGES;
quit

Then, edit the .env file and update database settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret-passwd

Step 7. Configuring Apache for Laravel.

Create a new virtual host configuration file for your Laravel website, named your-domain.com.conf:

nano /etc/apache2/sites-available/your_domain.com.conf

And add the following content to the file:

<VirtualHost *:80>    
      ServerAdmin admin@your-domain.com
      DocumentRoot "/var/www/laravel/public"
      ServerName your-domain.com
      ServerAlias www.your-domain.com

      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined

      <Directory /var/www/laravel/>
         Options FollowSymlinks
         AllowOverride All
         Require all granted
 </Directory>
</VirtualHost>

To enable the virtual host we have just created, run the following command:

ln -s /etc/apache2/sites-available/your-domain.com.conf /etc/apache2/sites-enabled/your-domain.com.conf

For the new configuration to take effect, restart the Apache service by typing:

sudo a2ensite your_domain.com.conf 
sudo a2enmod rewrite
sudo systemctl restart apache2

Step 8. Setup Laravel.

Now you can access the Laravel web install wizard using an HTTP connection:

https://your-domain.com

That’s all you need to do to install Laravel on Ubuntu 20.04 Focal Fossa. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.