Getting Started with Laravel (Chapter 2- Installing Homestead)

corona image

Coronavirus Impact on eCommerce Industry!

In the last blog, we installed our first Laravel application on our local machine and served it using php artisan serve. But we didn’t connect any database or anything. So today we are going to do that. Instead of serving our Laravel application with php artisan serve command on local machine, we are going to serve our application using Vagrant a virtual machine that take cares of all the dependencies for your application. I don’t want to go in the details of what the Homestead or virtual machine or vagrant is. You can simply just google about them or visit Laravel official documentation for that. So without farther a due let’s get started- “Installing Homestead”.

Installation Process

As we are going to install a virtual machine first, you need to enable virtualization from your local PC BIOS settings. After that download any virtualization software that you prefer. In my case, I used Virtual box. So just go to their website download the one which suits your PC configuration and install. After installing Virtual box, you need to install vagrant for your PC. To do that visit https://www.vagrantup.com/downloads.html . After that download the one which suits your PC configuration. As I am Ubuntu user, I downloaded the Debian package.

Adding Laravel/Homestead box to vagrant installation

Now, we need to add Laravel/homestead box to our vagrant installation. So to do that open up your terminal type in this command vagrant box add Laravel/homestead and hit enter. After that type in 3, if you are using virtual box and press enter. Depending on your internet speed, it may take 30-45 minutes to download.

If you are following correctly till now, you are ready to install Homestead.

So just copy git clone https://github.com/laravel/homestead.git ~/Homestead this git command and clone this repository by typing in this line in terminal. After cloning this repository cd into this directory from your terminal by cd Homestead. After that inside Homestead directory on your terminal, enter bash init.sh ( for Ubuntu/Linux/Mac) or init.bat (For windows) .

Connecting Our Application with Homestead

Now all the installations steps are done. Finally we are ready to serve our application. But first let’s make some necessary changes to connect our application with Homestead. So go to your homestead directory on your PC from file explorer or any GUI application.

Open Homestead.yaml file in any text editor.

Homestead.yaml

In this file on the folders section

-map: ( Type the directory of your project folder)

to:( location of your application in vagrant virtual machine)

Sites section: -map: (any domain you want for your application except .com domain for instance for my crud app I used crud.test)

to: (location of the index.php file which is in public folder of your project) we need to map inside the public folder inside vagrant.

databases: just type the database name you prefer for your application. I used crud for my project. After doing all of that save the file and open your hosts file.

For ubuntu open your terminal cd into etc folder ,

cd /etc .

Open hosts file by sudo nano hosts in your terminal from etc directory. And, add the domain name, you added in Homestead.yaml crud.test . After that press ctrx+x to exit and Y to save and hit enter to save the modified hosts file.

Fire Up Vagrant to Serve Our Application

We are almost done for now. Time to fire up vagrant and serve our application. So go to your terminal, cd into Homestead folder type and enter vagrant up to fire up vagrant/our virtual machine.

Now our application should be running on localhost on this domain crud.test. Just verify that by opening your browser and visiting this address crud.test

Now let’s connect our database with our application and check if everything is working perfectly as should. So to do that first lets use php artisan make:auth command to create Laravel default authentication/login system. To do that ssh into your vagrant machine from terminal. cd Homestead -> vagrant ssh -> cd crud . Now if you are inside crud project folder inside vagrant, run auth command and create default authentication method.

Now open your project folder. Copy .env.example file to .env and open the .env file.

For DB_Database = your database name for this project
DB_USERNAME = homestead

DB_PASSWORD = secret.

And, everything is done. Now run php artisan migrate to migrate your database columns to your database you created from your virtual machine.

And, everything is done. You might be wondering why going through all of these hassle. Well, at first to make our project encapsulated from OS dependencies to easily manage project. Also connecting to individual databases for individual application and many more. We are done for today. In the next tutorial, we will learn about Model, Controllers, route view and all of the fun stuffs.

My Previous Blogs on Laravel:

Getting Started with LARAVEL ( Chapter 01)

Things you need to know about Laravel in 2019

Share this post

In the previous blog ( Things you need to know about Laravel in 2019) I talked about Laravel and its origin, current and future position in the Web Application/Software development field. This is going to be a small series on Laravel where I will start from the basics and in the end you will have a ready to deploy small CRUD application. As you are following the tutorial I expect you have a basic understanding of Object-Oriented Programming PHP & MySQL. So in this tutorial series, we will follow the official laravel documentation. The documentation will help guide you through if you face any difficulties. So let’s begin.

In this tutorial, we’ll learn the basics of Laravel application installation and running your first Laravel starter application.

Installation:

To start building your first Laravel application you’ll need to install some dependencies first. As the Laravel framework has few requirements So Homestead comes with all the requirements you need to work on your Laravel application. Homestead is an official virtual machine for Laravel. In the next tutorial of this series, I’ll discuss everything you need to know about it. So, for now, will just stick to the most simple one. I am using Ubuntu but You may use any OS you prefer. Because Everything is almost the same. So there’s no need to panic.

As we are not using Homestead for this part of the tutorial, So you need to make sure you have PHP and its extensions installed on your local computer. Therefore I am assuming you have a basic understanding of PHP you should have PHP and its extensions installed on your system.

Now you need to install Composer on your machine. So to install composer just open up your terminal, type in the following command and press enter.

Installing composer:


php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

php composer-setup.php

php -r "unlink('composer-setup.php');"

After installing composer run the command below to install composer for Laravel globally on your system.

Run composer to check whether the composer is installed properly or not.

Composer Installation check & Composer commands


composer global require laravel/installer

Once you are done installing composer you are ready to create your first Laravel starter application.

To create your first Laravel application run composer create-project --prefer-dist laravel/laravel crud. On this command “crud” is the name of the application. You can type any name you prefer instead of crud. As I prefer to create all my applications inside a folder to keep everything organized, So For me it’s inside the Documents/projects folder. So I will CD into the folder and run the command to create the application.

Creating a new project from composer command

After successfully running the command composer will start setting up a fresh Laravel application in your Documents/projects folder. And it will look like this.

Project files & folders

The next thing we need to do is to generate a unique application id for your Laravel application. To do that first you need to open up your GUI file explorer go inside your folder and copy .env.example to .env . It should look like this.

.env file

Now run php artisan key:generate from CLI tool/ terminal inside your project directory.

Artisan command to generate a key for the application

Now you are ready to run your application. And to do that type and enter php artisan serve from your terminal.

Serving the app on the local machine

And voila! your app is running on this local IP address. On my case which it’s running on http://127.0.0.1:8000 . Now open up your favorite browser and type in the url http://127.0.0.1:8000 .

Congratulations! your application is ready to rock and roll.

And your starter application

This is just a basic laravel application so there’s not much to it. So in the next tutorial, we’ll Install Homestead and discuss how to use homestead with your laravel application. Connecting MySQL database and folder structure of laravel.

Share this post

Subscribe to our newsletter