Large hard drives and spare CPU cycles have made running multiple virtual machines off your MacBook feasible, and VirtualBox + Linux have made it free. But actually setting up a VM for each development project always seemed like a bit too much trouble – until I met Vagrant.
The Vagrantfile is a declaration of how the virtual machine should be configured. Here’s a simple but complete example, which sets up an Ubuntu 12.04 system with port forwarding so I can reach the lolcats website at http://localhost:8000; a static private IP address which obviates the need for port forwarding; and a synced folder so that my development machine’s source code is available in the VM.
# -*- mode: ruby -*-
# Vagrant file for the lolcats website
Vagrant.configure("2") do |config|
config.vm.hostname = "lolcats"
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "precise64"
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
# Forward a port from the guest to the host, which allows for outside
# computers to access the VM, whereas host only networking does not.
config.vm.network :forwarded_port, guest: 80, host: 8000 # web server
# Private network address. If this isn't specified, the machine will
# be assigned a private IP address.
config.vm.network :private_network, ip: "172.16.147.200"
# Share an additional folder to the guest VM. The first argument is
# an identifier, the second is the path on the guest to mount the
# folder, and the third is the path on the host to the actual folder.
config.vm.synced_folder "../lolcats/source", "/home/vagrant/lolcats"
The Vagrantfile can also be used to script configuration of the machine to do basic or complex setup, like installing apt or configuring PostgreSQL. Vagrant can use shell scripts as well as Chef and Puppet, both of which provide easily configurable recipes for common tasks. See Vagrant’s documentation on provisioning or this handy guide by another Vagrant fan that covers Chef.
- Download and install VirtualBox. (VMware Fusion can also be used, but requires a paid license).
- Download and install Vagrant.
- Download the base virtual machine you’d like to use (specified in
$ vagrant box add precise64 http://files.vagrantup.com/precise64.box
Create and run the VM
Create your Vagrant file. Either create one from your favorite text editor, or with vagrant, specifying the
$ vagrant init precise32 http://files.vagrantup.com/precise32.box
cd to the directory with the Vagrantfile and boot the VM:
$ vagrant up
SSH into the VM:
$ vagrant ssh
Shutdown the VM when you’re done with it for the day:
$ vagrant halt
Delete the VM file entirely when you realize that a lolcats website has already been done:
$ vagrant destroy
Share your VM configuration
A good Vagrantfile makes it easy to spin up a copy of the virtual machine when a new developer joins your project. Use Chef commands in your Vagrantfile to install and configure all the dependencies and commit your Vagrantfile to source control.
Then, rather than giving the newbie a daunting and crusty installation guide with 43 different steps, just give them a git URL and a pointer to this page. You’ll save everyone time and let the new developer focus on adding new features (lolpenguins) rather than slogging through system setup.