Installing Laravel in Localhost
Ubuntu-20.04 LTS, php-8.0, Laravel-8, MariaDB-10.3

Preparing Ubuntu

If you are using windows, you can install Windows Subsystem for Linux (WSL).
Follow this official guide to install Ubuntu. https://docs.microsoft.com/en-us/windows/wsl/install-win10#manual-installation-steps.

Update list of available packages from apt package manager.

sudo apt update

Upgrade the system to install available upgrades for currently installed packages.

sudo apt upgrade

Install php8.0

Since this is a personal package archive, you will be shown a warning before you proceed. You will need to use it at your own risk. However, this PPA is trusted by many.

sudo add-apt-repository ppa:ondrej/php

Again update the apt to pull down the list of packages from new source.

sudo apt update

Install php8.0.

sudo apt-get install php8.0-fpm

Install Composer

Go to the home directory.

cd ~

Install composer.

Follow the Command-line installation section in https://getcomposer.org/download/

Make composer globally accessible.

mv composer.phar /usr/local/bin/composer


Install Laravel

Prepare the system for Laravel and Composer. (System requirements for Laravel8)

sudo apt-get install php8.0-bcmath php8.0-common php8.0-mbstring php8.0-xml php8.0-curl php8.0-intl php8.0-gd php8.0-zip openssl zip unzip

Install laravel via composer.

composer global require laravel/installer

Place Composer's system-wide vendor bin directory in your $PATH so the laravel executable can be located by your system. This can be done by editing .bashrc file as shown below. 

Open .bashrc file using nano text editor

nano ~/.bashrc

Paste the following code at the end of the file. You can use Alt+ / or arrow keys to go to the last line.

export PATH="$PATH:$HOME/.config/composer/vendor/bin"

Save the document. [You can use Ctrl+O to write the file and press enter to confirm the file. Then Ctrl+X to save the file.]

Use the edited file.

source ~/.bashrc

Create a new Laravel project. [Or you can clone a project using git clone.]

laravel new example-project

Configure Laravel

Go to the laravel project directory.

cd example-project

Install vendor packages.

composer install

If you created a new project using laravel command, .env file will be automatically get created.

Create .env file. To do this we will copy the .env.example file provided by laravel.

cp .env.example .env

Generate app key.

php artisan key:generate

Edit .env file and add database and other relevant information.

Install MariaDB

Install Maria DB from apt.

sudo apt install mariadb-server

Start Mariadb server.

sudo service mysql start

Run the security script to add root password.

sudo mysql_secure_installation

You will be given the choice to change the MariaDB root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases. It is recommended that you answer yes to these options.

Login to the database.

sudo mysql -h localhost -u root -p

Create a database <testdb>.

CREATE DATABASE testdb; 

Create a user <testuser> with password <password>.

CREATE user 'testuser'@localhost IDENTIFIED BY 'password'; 

Grant database permission to the user.

GRANT ALL ON testdb.* TO 'testuser' IDENTIFIED BY 'password';

Exit mariadb.

exit;

Install mysql extension for php.

sudo apt-get install php8.0-mysql

Add database name <testdb> username <testuser> and password <password> in the .env file.

Happy Coding !!!

Share this post
Archive
Sign in to leave a comment