How to install and use Vagrant
Before we write any code, we will set up a local development environment where we can run it and test it. Vagrant will help us do just that.
Vagrant is a tool that lets you easily spin up pre-configured development virtual machines. There are many reasons to use Vagrant but for the purpose of this course let’s just say that it is the recommended Laravel way. There is an official Vagrant box made specifically for Laravel development called Homestead. It integrates extremely well into the workflow and it has no lasting impact on your computer.
Notice
These instructions are specific to OS X. However, these concepts can be applied to other operating systems like Windows or Linux.
Vagrant depends on VirtualBox so we will install that first. Go to virtualbox.org and download a binary for your system. Once VirtualBox is downloaded go ahead and install it. Installation is very straightforward and does not require any special instructions.
Now it's time to install Vagrant. Go to vagrantup.com and download version for your system. Vagrant installation is just as simple so you should be able to handle it yourself.
That’s it. Let’s try to spin up a box. Go to vagrantcloud.com and find a box that you want to use. For this example lets just use ubuntu/trusty64. Open up terminal and type this in:
vagrant init ubuntu/trusty64
Vagrant will create a file in the current directory called Vagrantfile. This file controls a lot of aspects of the box like networking and shared folders with the host system. We don’t really need to configure it in any way for our purpose. Start the box with this command:
vagrant up
At this point, Vagrant will download the base box we initialized so the first time you try this it will take a while. All subsequent requests will be quick.
You will need to authenticate to Vagrant box using SSH key. If you have ever used them before you might already have a set generated on your computer. Don’t worry if you don’t. Simply run this command:
ssh-keygen
You will be asked few questions but for Vagrant use you can simply hit enter few times to accept the defaults. If you plan to use this key for real world authentication it is a good idea to choose a passphrase.
Our Vagrant box is running and we can SSH into it with this command:
vagrant ssh
You will not be prompted for any passwords since Vagrant uses ssh keys for authentication. In order to disconnect from the Vagrant SSH sessions simply type in:
exit
In order to shut down the box type in:
vagrant halt
Vagrant has created hidden directory in our current directory that stores any changes you have made to the box. Since we will not be using this box for anything we can clean it up with this command:
vagrant destroy
One thing to remember is that you have cleaned up your instance of the box but the base image remains on your machine. To see the list of base boxes stored on your computer run this command:
vagrant box list
In order to remove the ubuntu box you can type in this:
vagrant box remove ubuntu/trusty64
While working with Laravel we will not use Vagrant as described above. Laravel provides a custom management tool for Homestead. I just wanted to give you an insight into how Vagrant works and what is actually happening behind the scenes.