Vagrant: Development Environments Made Easy

Vagrant is a tool that allows the easy creation of virtual machines. It was originally developed for use with VirtualBox, but it has been extended to allow integration with other virtualisation tools. Using Vagrant you can create a particular setup that you can then share with other people without having to give them large virtual disk images.

I have to admit, when I first heard of Vagrant I didn't 'get it' and wasn't sure why I should be using it at all. Since then I have done lots of reading and talking with other developers and I am now wondering how I managed to work without it.

The problem with working on local development environments is that although it works in most situations you tend to find that things get a bit messy when you want to revert to a previous project. Many web applications are now starting to incorporate more and varied technologies into their setups and so ensuring that you have all of the correct servers (include the correct versions) installed can be more work than actually working on the application itself.

Vagrant solves this by allowing you to create a virtual environment that contains all of the configuration for the project in question. This configuration also allows you to pass on these environments to other people working on the project so that they can work with exactly the same setup without having to ensure they have the correct software installed locally.

Installing

Vagrant is pretty easy to install. Just go to the Vagrant website, download the relevant package for your system and run through the installer. Once installed you can run the vagrant command with the --version flag to see if everything is installed correctly.

$ vagrant --version
Vagrant 1.4.1

Vagrant Boxes

Vagrant accepts box files as input. Box files are skeletons that virtual machines are generated from and contain some minimal configuration options. There are lots of Vagrant boxes available, but you need to make sure you get one that is compatable with the virtual machine provider you are using. Take a look at Vagrantbox.es and pick a box that you want to work with. Try to pick one from a reputable source as many people have added box files from Google Drive and Dropbox that have no security vetting at all.

To use a box just use the 'vagrant box add' command like this.

vagrant box add [box name] [box file path]

To use a recent version of Ubuntu to use with Vagrant you need to download the relevant file and then you can do the following.

vagrant box add precise precise-server-cloudimg-amd64-vagrant-disk1.box

Installing Vagrant creates a .vagrant.d directory in your home directory. Adding a box creates a directory inside the boxes directory that contains all of the files needed for that box. The box directory may contain one or more of the following files.

  • vmdk : A virtualisation standard file format that describes the machine in detail.
  • ovf : A file that describes the contents of the box.
  • matadata.json : Vagrant uses this file to let it know what kind of virtual machine it has.
  • Vagrantfile : This file contains a little bit more information about the setup of the box. Usually used to set network configuration.

You can see which boxes are available on your system use the 'vagrant box' command with the 'list' option.

vagrant box list

Running Vagrant

To use a vagrant box you first need to initialise it. This is done by using the 'vagrant init' command, which can also be passed the name of the box you have previously imported.

vagrant init [box name]

The directory will now contain a .vagrant file, this contains some details of the virtual machine. A Vagrantfile is also created that contains some default configuration options. The Vagrantfile is kept as a Ruby configuration file and so a small amount of Ruby syntax is required in order to understand and change it. There are lots of options to tweak, but as a quick example you can change the IP address of the virtual box by setting the 'config.vm.network' setting.

config.vm.network :private_network, ip: "192.168.33.10”

You can now run the virtual machine by typing 'vagrant up'.

vagrant up

You will see Vagrant run through the steps of importing and running the virtual machine. Once it has completed it's task you will have a virtual machine ready to use.

Using Vagrant

With your virtual machine running you can access it by using the 'vagrant ssh' command. This will automatically log you into the virtual machine you have just started with a user called 'vagrant'.

vagrant ssh

This ssh connection is made using a ssh key that has been part of the Vagrant source code for years. This means that it is essentially insecure and if you have a virtual machine running on your machine it will quite likely be available to everyone within your network. For this reason you shouldn’t use a vagrant machine for production purposes.

Shutting Down

To shutdown the machine just use the command 'vagrant halt', this will gracefully power down the machine. The next time you run the machine you will see the same machine in the same state.

vagrant halt

You can also issue the command 'vagrant destroy' to shutdown and delete the machine. This effectively loses all of the information and configuration you had on the machine. The box itself hasn't been lost as it still exists in Vagrant so the next time you start the machine you will see the default virtual machine that the box contains.

vagrant destory

If you are running Vagrant using the VirtualBox provisioner you can see which virtual machines are currently running using the following command.

VBoxManage list runningvms

NFS Drive

One thing that might complicate getting up and running using Vagrant is getting the files you want to work with onto the new virtual machine. To solve this we use a NFS drive so that a directory on the server is linked with a local directory. The following will link a directory called 'docroot' (local to the Vagrantfile) with a directory on the virtual machine located at '/var/www/docroot'.

Place the following into your Vagrantfile configuration file, anywhere before the final 'end' statement.

# Set up NFS drive.
nfs_setting = RUBY_PLATFORM =~ /darwin/ || RUBY_PLATFORM =~ /linux/
config.vm.synced_folder "./docroot",
  “/var/www/docroot, 
  id: "vagrant-root",
  :nfs => nfs_setting

Using NFS drives makes it easy to drop your source code into the docroot directory and work on it locally, whilst serving it from the Virtual machine. This also means that you can use source control to keep track of your files and Vagrant configuration without having to worry about pulling your source code onto your virtual machine every time your create it.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
12 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.