Installing Virtual Environment with Laravel Homestead - Tutorial
Tags: webdev, beginners, laravel, tutorial
Published: 2019-07-16 18:29:11

Using a virtual environment it is very common when you're working in development area, since there are so many reasons for its use, for example, if you need to work on a different operating system, maybe you want to use Linux on top of Windows, even if you are working on the same operating system, but the application doesn't support the latest version, you can run it on an old version on a virtual machine.

This is a list of some benefits of using virtual machines:

• We can run different operating systems on different virtual machines in the same physical machine at the same time.
• We can have test environments before moving to a production environment.
• Better use of shared resources.
• Reduce the costs of data centers by reducing your physical infrastructure.
• We can work with the same virtual machine configuration on different hardware.

The use of virtual machine also has disadvantages like they are less efficient than real machines because they access the hardware indirectly. Running software on top of the host operating system means that it will have to request access to the hardware from the host. That will slow the usability, but even with that, it's worth its use.

Installing a virtual machine may seem difficult but it is not, so let's start with the tutorial!

<b>Installing VirtualBox</b>

Virtual Box is virtualization software that allows us to install operating systems within it, it is open-source software and it´s executed in the mains operating systems like Linux, Windows, and Mac. Because of that, it's the perfect option for this tutorial.

First, we need to go to: 
https://www.virtualbox.org/wiki/Downloads
Choose a VirtualBox for your platform and install it. Make sure that you download the correct version for your operating system. 
For Linux https://www.virtualbox.org/wiki/Linux_Downloads
If you’re using Windows, double click the .exe setup file to install VirtualBox. 

![](https://thepracticaldev.s3.amazonaws.com/i/gmxcxgs11hu38jpplo0u.png)

If you’re using Mac, simply open the VirtualBox .dmg file and click on the .pkg file to install. 

<b>Installing Vagrant</b>

Vagrant is the software that will be between our virtual machine provider and us. It allows us to create virtual machines declaratively since it automates everything through a tool through a configuration file. In this file, we declare how we want our virtual machine. Vagrant is also portable since those configuration files can be stored in repositories and shared so that everyone has the same environment.

The next step is to install Vagrant. 
Vagrant works on Mac, Linux, Windows.
Please go to: http://www.vagrantup.com/downloads.html 
and choose the option for your operating system.

![](https://thepracticaldev.s3.amazonaws.com/i/57g0zljk0gfqmboo3ebb.png)

<b>Install Homestead</b>

Homestead is a pre-packaged Vagrant package that provides a good development environment without the need to install PHP. You can install Homestead just by cloning the Homestead Repository. You will need to install Git first if you don’t have it on your system. 

Please go to: https://git-scm.com/downloads 

After installing the above-mentioned softwares, you need to add the laravel/homestead box to your Vagrant installation. To do so, run the below command:

$ vagrant box add laravel/homestead 

After loading metadata, you will be prompted to select your choice for Hyperv, Parallels, Virtualbox or vmware_desktop, enter 3 and hit Enter.

![](https://thepracticaldev.s3.amazonaws.com/i/u210c0stkq41489pgw9h.png)

Now, open Git Bash in your desired directory and clone the Homestead Repository by running the command:

$ git clone https://github.com/laravel/homestead.git Homestead

Once downloaded, go to the Homestead directory and run this command:

$ bash init.sh 

That will create the Homestead.yaml file. The Homestead.yaml file will be placed in your ∼/.homestead directory.

Please note that the ∼/.homestead directory is hidden by default, make sure that you can see hidden files.

<b>Configuring Homestead.yaml File</b>

Open your Homestead.yml file which is inside Homestead directory.

![](https://thepracticaldev.s3.amazonaws.com/i/y9wqayqnsvar52jimb5o.png)

As you can see, we can configure the IP address, memory, cpus and provider of our VM. Here you can distribute your resources as you like. 

![](https://thepracticaldev.s3.amazonaws.com/i/b9zsgknwgdwupr6ashn8.png)

We need to generate an SSH key for Homestead to authenticate the user and connect to the virtual machine. If you’re working with Git, you may have an SSH key already. If you don’t have it, simply run this command to generate it: 
ssh-keygen -t rsa -b 4096 -C your_email@example.com
The command will generate an SSH key for you and put it in the ∼/.ssh directory automatically, you don’t need to do anything else. 

![](https://thepracticaldev.s3.amazonaws.com/i/dg54zf5b85n0k25tjra3.png)

We use folders section to specify the directory that we want to share with our Homestead environment. If we add, edit or change any files on our local machine, the files will be updated automatically on our Homestead virtual machine. 

![](https://thepracticaldev.s3.amazonaws.com/i/gyoroa0py83ntx1s2ioi.png)

This section allows us to map a domain to a folder on our VM. For example, we can map homestead.app to the public folder of our Laravel project, and then we can easily access our Laravel app via this address: "http://homestead.app". 

Remember that, when we add any domain, we must edit the hosts file on our local machine to redirect requests to our Homestead environment. 

On OS X and Linux it’s in /etc/host

On Windows, you can find the hosts file at C:WindowsSystem32\drivers\etc\hosts.
 
After opening the file, you need to add this line at the end of the file:

192.168.10.10 homestead.app

We have finisehd with the basic configuration to our Homestead.yam file.

Once we have edited Homestead.yaml file, cd to the Homestead directory, run this command to boot our virtual machine: 

$ vagrant up

You also can use SSH inside your virtual machine with the following command:

$ vagrant ssh

<b>Installing Laravel</b>

Laravel is the most useful framework on a regular basis because is the right choice for projects with complex backend requirements.  Before using Laravel, make sure you have Composer installed on your machine.

https://getcomposer.org/doc/00-intro.md

Once you have installed Composer go to your project's folder that you configured in Homestead.yaml file and create a new project  by issuing this command in your terminal:

composer create-project --prefer-dist laravel/laravel myproject

![](https://thepracticaldev.s3.amazonaws.com/i/75p9e8ozugj6vj7u8xzp.png)

![](https://thepracticaldev.s3.amazonaws.com/i/hds4xtkv9z8crjgqrvml.png)

Done! Now you can build your app inside. 

Thanks for reading!
Have a nice day, coders💜