DEV Community

Cover image for Installing Virtual Environment with Laravel Homestead - Tutorial
Lesly Villalobos
Lesly Villalobos

Posted on • Edited on

Installing Virtual Environment with Laravel Homestead - Tutorial

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!

Installing VirtualBox

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.

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

Installing Vagrant

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.

Install Homestead

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.

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.

Configuring Homestead.yaml File

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

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.

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.

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.

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

Installing Laravel

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

Done! Now you can build your app inside.

Thanks for reading!
Have a nice day, coders💜

Top comments (3)

Collapse
 
aleedhillon profile image
Ali A. Dhillon

I would suggest to skip the

vagrant box add laravel/homestead
Enter fullscreen mode Exit fullscreen mode

part and do everything else and then just

vagrant up
Enter fullscreen mode Exit fullscreen mode

and it will download the required box automatically. Because when you add the box through the

vagrant box add laravel/homestead
Enter fullscreen mode Exit fullscreen mode

it downloads a different box probably of Hyper-V type and with a different version and then when you

vagrant up
Enter fullscreen mode Exit fullscreen mode

it says oh no we don't need this box we need that box so we gonna take an hour more to download that box so just skip the vagrant box add laravel/homestead part and just vagrant up (after doing other steps) and it will download the box it needs automatically.

Collapse
 
svpernova09 profile image
Joe Ferguson

Well done!

Collapse
 
lvtdeveloper profile image
Lesly Villalobos

I appreciate your comment. Thanks :)