Vagrant is a set of tools aimed to create and configure lightweight and portable development environments. The approach is actually pretty simple: Vagrant offers you a way to create config files which are executed later to set up a virtual machine. The interesting point here is that this machine can be (re)created with just one command and can be configured by changing a couple of Ruby lines in the config file.
What’s the point of this? You can have a development environment identical to the one you usually use in any machine with Vagrant installed. If for any reason you break the development environment (installing unstable packages, misconfiguring anything, etc) you can return to the start by running just one command. But it is not limited to development environment: you can deploy your application in a virtual machine with the same software than the production environment.
Setting up Vagrant and VirtualBox
To use Vagrant you’ll need an external software to actually execute the virtual machines. Vagrant works very well with VirtualBox. Although it also works with VMWare and Parallels (via plugins) we’ll use VirtualBox as it is free and good enough. So first download and install VirtualBox and then download and install Vagrant. Installation process is pretty simple in both cases.
Once Vagrant and VirtualBox are installed we can create our first virtual machine by simply running:
In the folder where we want to store the configuration file (called Vagrantfile).
Setting up Vagrantfile
Before being able to run our virtual machine we have to define some parameters, specifically the name of the box and its URL but before that, what’s a box? A box is basically a base image: an image of a virtual machine that has installed an operating system (and many times the required tools to integrate it with a provider – VirtualBox, VMWare and Parallels are usually referred as providers as they are the applications that actually run the virtual machine: Vagrant just takes care of setting them up). Having a box makes having a virtual machine ready faster as you don’t have to install the operating system.
Boxes have a name and a URL but these parameters only affect the local settings of Vagrant: you can choose the name you want for your boxes, as the URL – name mapping is stored only in your computer.
Open Vagrantfile and look for this line:
config.vm.box = "base"
There you specify that this virtual machine will use the box “base” as starting point. As Vagrant doesn’t now which box it is you have to specify the box URL so that Vagrant can download it. If you create later a new virtual machine and set up the box name to “base” then Vagrant won’t download the box as it would have downloaded it before.
config.vm.box_url = "http://domain.com/path/to/above.box"
This line will specify the URL of the box. You can find a list of free boxes in vagrantbox.es. You can also create your own boxes, but that’s beyond this article.
You can also tell Vagrant that you want a certain URL associated with a certain box name by running the command vagrant box add <name> <url>:
vagrant box add precise32 http://files.vagrantup.com/precise32.box
Take into account that Vagrantfile is just a Ruby file, so you can modify, read other files, take decisions on the fly and much more. In this file you can set up other settings which are interesting but not essential to run the virtual machine, like port forwarding, folders synced with the host, software to be installed and so on.
Accessing the virtual machine
Once you have chosen a box name and URL you can run it with the command: vagrant up.
This command will start the virtual machine, set up port forwarding, etc. You won’t see any new screen as the virtual machine is run in background. You’ll need SSH to access it, but don’t worry, Vagrant will take care of authentication:
Once you are done with the virtual machine you can stop it with:
If for any reason you want to return to the initial state, removing any change you have done to the virtual machine, you can do it by deleting the virtual machine and recreating it, as easily as:
vagrant destroy vagrant up
vagrant destroy won’t delete the config file (it’s just a couple of KB) but will remove the image of the machine. So you actually don’t lose your configuration when running vagrant destroy.
If you want to completely remove the virtual machine, after running vagrant destroy just delete the folder with the config file.