Feb 11, 2015
Setting up a Virtual Machine
How to set up a virtual machine with VirtualBox, Vagrant and Homestead to create an isolated production-like environment on your local machine.

How to set up a virtual machine with VirtualBox, Vagrant and Homestead to create an isolated production-like environment on your local machine.
VirtualBox
First you’ll need a virtualization provider. For this guide we’ll use VirtualBox. You can download it here from the VirtualBox website. Look for VirtualBox X.X.XX for OS X Hosts. Once it’s downloaded, follow the installer. Easy.
Vagrant
Vagrant helps you build and manage development environments really quickly, everything is automatic and allows you to create identical environments over and over again on any computer.
You can download it from here. Once downloaded follow the GUI to install.
Now we have the important tools installed, we can start setting up an environment.
There are loads of different ways to go from here. You could install a plain Ubuntu image and set up the exact packages you want, but we just want a generic PHP environment.
Vagrant refers to virtual machines as “boxes”. We want to create one for PHP development.
That’s where Homestead comes in. Built by Taylor Otwell, the creator of Laravel, it’s a pre-made Vagrant box that’s perfect for PHP/Laravel work.
Homestead
Open up Terminal. Since installing Vagrant, we now have access to the vagrant command-line tool. We can add the Homestead box by running:
vagrant box add laravel/homestead
This downloads the Homestead box (Ubuntu and pre-installed packages), so it’s fairly large and might take a little while. You only have to do this once, even if you have 20 VMs. Vagrant caches the box so you can provision new environments quickly.
Now it is time to set up a VM and start using it. You will have to do the following for every development environment you create.
If you’re just building simple applications, you can use the same environment for all of them.
Find a safe place on your machine and navigate to it in Terminal (I use ~/Developer/Virtual).
cd ~/Developer/Virtual
git clone https://github.com/laravel/homestead.git Homestead
This will pull down the Homestead tools. Open Homestead and run the initialization script.
cd Homestead
./init.sh
Generate an SSH key
If you have used SSH before, you may already have one. Otherwise, use this command to generate an SSH key. This is used to connect to your VM.
ssh-keygen -t rsa -C "you@email"
You can probably hit Enter a few times and use the default settings.
Back to Homestead
Now we need to configure the Virtual Machine. Set up our sites, domains and folders.
The file we want to edit is ~/.homestead/Homestead.yaml. Open ~/.homestead in Finder and edit the file in Sublime, or:
nano ~/.homestead/Homestead.yaml
Folders
folders:
- map: ~/Code
to: /home/vagrant/Code
The first line maps a local folder ~/Code to a folder on the VM at /home/vagrant/Code. Adding multiple folders can look like this. Be careful with indentation.
folders:
- map: ~/Developer/MyApplication
to: /home/vagrant/MyApplication
- map: ~/Developer/WebsiteOne
to: /home/vagrant/WebsiteOne
- map: ~/Developer/ClientApp
to: /home/vagrant/ClientApp
Now these folders will sync. If you edit something in ~/Developer/ClientApp it will update in the VM at /home/vagrant/ClientApp, and vice versa.
Sites
Now we need a way to access these folders.
sites:
- map: homestead.app
to: /home/vagrant/Code/Laravel/public
The first line maps a domain (homestead.app) to a directory on the VM (/home/vagrant/Code/Laravel/public). The same folders we set up before can be exposed as sites like this:
sites:
- map: myapp.local
to: /home/vagrant/MyApplication
- map: websiteone.dev
to: /home/vagrant/WebsiteOne
- map: client.app
to: /home/vagrant/ClientApp/public
Be careful setting the VM directory if your application doesn’t run in the document root.
To make these domains work, we need to tell your computer to look for them on the VM. This can be done by editing your computer’s /etc/hosts file.
sudo nano /etc/hosts
Add each domain, pointed at the VM. When adding multiple domains they can all go on the same line, separated by commas.
192.168.10.10 myapp.local, websiteone.dev, client.app
Woo!
Now it’s time to launch the box. Navigate back to where we cloned the Git repo, then tell Vagrant to start the VM.
cd ~/Developer/Virtual/Homestead
vagrant up
The first time you run this it will take a bit longer while it sets up folders and domains. You’ll have to re-run this whenever you restart your computer.