Set up laravel project in a virtual machine with laravel homestead
Tags: laravel, webdev, beginners, tutorial
Published: 2020-04-22 11:15:23

<a href="https://www.buymeacoffee.com/jsafe00" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-green.png" alt="Buy Me A Coffee" style="height: 51px !important;width: 217px !important;" ></a>


<b> Why Laravel Homestead? </b>

Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine.  No more worrying about messing up your operating system! That’s why I highly recommended this virtual environment setup.

Before launching your Homestead environment, you must download and install on your pc the ff:

1.	VirtualBox – an application that lets you run an operating system within your operating system. It keeps the data and activity of the operating system inside of it, which is called the guest operating system, separate from your regular operating system, which is called, the host operating system
Download virtual box here: https://www.virtualbox.org/wiki/Downloads

2.	Vagrant - tool for working with virtual environments, and in most circumstances, this means working with virtual machines. Vagrant provides a simple and easy to use command-line client for managing these environments, and an interpreter for the text-based definitions of what each environment looks like, called Vagrantfiles. Vagrant is open source, which means that anyone can download it, modify it, and share it freely.
Download vagrant here: http://www.vagrantup.com/downloads.html

3.	Homestead – Homestead is a vagrant package that provides a dev environment without the need to install PHP. To install homestead, you need to have a Git installed in your system. If none, download and install  Git here: https://git-scm.com/downloads

4.	Composer - tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. 
Download composer here: https://getcomposer.org/doc/00-intro.md

I like to put my Laravel project in this directory: 

C:\Users\Dell

In your desired directory, execute the command below:

<mark> composer create-project --prefer-dist laravel/laravel Laravel </mark>

Now, we have a fresh laravel project in your local machine.

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pd8gk45cy13mai7b28aj.png)

Run the below command thru git bash in your desired directory: 
<mark> vagrant box add laravel/homestead </mark> 

You will be prompted to select your choice for 
1.	hyperv
2.	parallels
3.	virtualbox
4.	vmare_desktop

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k34x0mms0xv408lnubbn.png)

Choose number 3 then press enter.
 
Once laravel/homestead is added successfully for virtualbox, run this command: 

<mark> git clone https://github.com/laravel/homestead.git Homestead </mark>
-	this will clone the homestead repository.

Then execute the ff:

<mark> bash init.sh </mark> 
-	this will create the Homestead.yaml file.

Your Homestead.yaml file is inside your Homestead folder. Open it and configure accordingly.
Please see below configuration. I will explain it by section. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/krsp2wg6bk9qvonjhrqa.png)

The above configuration is all about your IP address, cpus and provider of your VM. You can update it according to your desire but on my part, I’ll leave it as is.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v22whqzcmm08hcnm45a6.png)  

If you are already working with git, you may have an SSH already. But if you don’t have, please run the command below: 

<mark> ssh-keygen -t rsa -b 4096 -C your_email@example.com </mark>

This will create a ssh key for you and you don’t need to do anything else.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y4tgjivuziyxw2d2da7c.png)
 
This is the part where we specify which folders in Windows should be synchronized with the virtual machine. In my case my map is: C:/Users/Dell/Laravel since my Laravel project is in that directory and I want it to mimic in /home/vagrant/Code directory in my virtual machine.

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gs8ssizhlbh9fazurfgc.png)

This section will let us map our domain to a folder in our VM. In my case I name my domain as homestead.test then map it to /home/vagrant/code/public in my virtual machine.

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cvkft53b0v3rq5we0i8u.png)
This is the summary of my Homestead.yaml

Don’t forget to edit your host file at: C:\Windows\System32\drivers\etc\hosts, then add this at the end of the file:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2k3cy9b7rs22y8ljr0td.png)
192.168.10.10 homestead.test

Go to your Homestead directory
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5v37fdr7o38kb7lz7io1.png)
 
<mark> cd Homestead </mark>

Then run this command:
 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eguv7al3r86d6uriqtcu.png)
<mark> vagrant up </mark>

this will create the environment. If the environment was already created and the up is just resuming a machine or booting it up.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4rynyks4uf782jr6nn2r.png)
Execute vagrant ssh:

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0t5oh49tfzu3cjcsatg1.png)

Type cd Code then ll. The Laravel project that we have in our local machine is mimic in our vm. 

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s244rgvpxbka43bml3u6.png)

We can easily access our Laravel project via this address: http://homestead.test.

To shut down the vagrant machine, execute the ff:
 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kara57bd2k74xd7fb68o.png)
<mark> vagrant halt </mark>

Note: if you change something on your Hometead.yaml, you need to run the command:
<mark> vagrant reload –provision </mark>
-	vagrant reload --provision will reboot the VM and run the provision steps.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o90e41eq57xmkrlgcirf.jpg)
My programming blog begins here, my Home - Laravel Homestead.  

Please don't hesitate to leave a comment or ask any question to make this tutorial easily doable as possible to others.

<a href="https://www.buymeacoffee.com/jsafe00" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-black.png" alt="Buy Me A Coffee" style="height: 51px !important;width: 217px !important;" ></a>