How to Install Laravel on Ubuntu 20.04
Tags: php, laravel, linux, ubuntu
Published: 2022-03-26 13:29:50

Laravel is an open-source PHP web framework. It is mainly used for building PHP-based web applications.

Laravel is suitable for both small-scale and enterprise-level application development. Its elegant syntax, advanced features, robust tools help simplify web application development. Laravel is highly scalable and has built-in support for distributed cache systems.

**Step 1: Install Apache web server:**

```
 sudo apt install apache2
```
Once installed, Apache should be running. If it's not, for whatever reason, start it:

```
sudo systemctl start apache2
```
Then enable it to start on boot time.

```
sudo systemctl enable apache2
```
To verify the status of Apache, execute:

```
 sudo systemctl status apache2
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pa559fgjsb369y76fii3.png)

**Step 2: Install PHP and additional PHP extensions:**

```
sudo apt install php libapache2-mod-php php-mbstring php-cli php-bcmath php-json php-xml php-zip php-pdo php-common php-tokenizer php-mysql
```
When the installation is complete, verify the PHP version.

```
php -v
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nl4o1vemj3eoekkl0cd.png)

**Step 3: Create Database for Laravel Application:**
But first, we need to install a database server. Laravel supported database systems are MariaDB, MySQL, SQLite, Postgres, or SQL Server.
We will go with the MariaDB database engine.

```
sudo apt install mariadb-server
```
Once the database server is installed, log into the MariaDB prompt:

```
sudo  mysql -u root -p
```
Once logged in create the database, database user, and grant all privileges to the database user.

```
CREATE DATABASE laravel_db;
```

```
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'secretpassword';
```

```
GRANT ALL ON laravel_db.* TO 'laravel_user'@'localhost';
```

```
FLUSH PRIVILEGES;
```

```
QUIT;
```
**Step 4: Install Composer:**
Composer is a dependency package manager for PHP. It provides a framework for managing libraries and dependencies and required dependencies. To use Laravel, first install composer.

```
curl -sS https://getcomposer.org/installer | php
```
This downloads the composer.phar file.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1t9ohj8ozz9st6nv67i.png)
Next, move the composer file to the /usr/local/bin path.

```
sudo mv composer.phar  /usr/local/bin/composer
```
Assign execute permission:

```
sudo chmod +x   /usr/local/bin/composer
```
Verify the Composer version installed:

```
composer --version
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7axydnrv6gp06hxhavv0.png)

**Step 5: Install Laravel 8 on Ubuntu:**
With Composer installed, the next course of action is to install Laravel.

Navigate to the webroot directory, type:

```
cd /var/www/html
```
Now, install Laravel using the composer command, type:

```
sudo composer create-project laravel/laravel laravelapp
```
The command creates a new directory called laravelapp and installs all the files and directories for Laravel.

Change the ownership of the Laravel directory to the webserver user and also the permissions:

```
sudo chown -R www-data:www-data /var/www/html/laravelapp
sudo chmod -R 775 /var/www/html/laravelapp/storage
```
Feel free to replace laravelapp with a preferred directory name.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3na5lz2qfunck29tm878.png)
Once the installation is done navigate to the installation directory and check the Laravel version.

```
 cd laravelapp
```

```
php artisan
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zx20uawixsqh9beaw46.png)

**Step 6: Configure Apache to serve Laravel site:**
Lastly, we need to set up the Apache webserver to host the Laravel site. For that to happen, we need to create a virtual host file

```
sudo vim /etc/apache2/sites-available/laravel.conf
```
Next, past the content shown and replace the example.com ServerName directive with the FQDN or public IP of the server ( Or private IP in case the server is on a LAN network ).

`<VirtualHost *:80>
ServerName example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/html/laravelapp/public
<Directory /var/www/html/laravelapp>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>`
Save the changes and exit the file. Next, enable the Laravel site and Apache rewrite module using these two commands.

```
sudo a2ensite laravel.conf
```

```
sudo a2enmod rewrite
```
To apply the changes, restart Apache

```
sudo systemctl restart apache2
```
**Step 7: Access Laravel from a browser:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnm0bb6kww8h7ioqwfsa.png)
Follow Me : [Twitter](https://twitter.com/Bilalniaz15)  [LinkedIn](https://www.linkedin.com/in/bilal-niaz-147792214/)