// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleBuild New

Spin Up That Development VM with Vagrant

Spin up a development VM for each project with Vagrant and VirtualBox. A simple Vagrantfile with port forwarding, a private IP, and synced folders.

Art+Logic Sep 16, 20133 min read

From the Art+Logic archive · written in 2013. The tools may have moved on; the thinking still holds.

Spin Up That Development VM with Vagrant

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.

Vagrant is a configuration file DSL, a command line tool, and a set of great documentation for quickly spinning up a new VM.

Vagrantfile

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"
end

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.

Initial setup

  1. Download and install VirtualBox.  (VMware Fusion can also be used, but requires a paid license).
  2. Download and install Vagrant.
  3. Download the base virtual machine you’d like to use (specified in config.vm.box_url), e.g.:
$ 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 box and box_url:

$ 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.

keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…