Skip to main content

An Introduction To Vagrant

I spent my Sunday afternoon familiarizing myself with a tool who's Getting Started page has been sitting in my Evernote tickle file for a couple weeks. This is one of those many projects that fall under the ever widening category of "Stuff I Wanted To Do, But Am Glad Someone Else Did It So I Can Just Use It And Get On To The Next Thing." If you use virtual machines as part of your development process, or want to, and especially if you already use the excellent VirtualBoxVirtualBox, then Vagrant is certainly worth looking at.

The Setup (for Vagrant 0.2)


Now, the docs might need some updating and they seem to assume you're already a Ruby user, so they're missing a few dependancies that such a person would just happen to alread have. This is what I did, as an Ubuntu user who didn't even have Ruby installed. I'm also adding Virtualbox's Karmic repository to provide VBox 3.1, which Vagrant requires.

sudo apt-get install rubygems libxslt-dev openssl-ruby
sudo gem install vagrant
sudo bash -c 'echo "deb http://download.virtualbox.org/virtualbox/debian karmic non-free" >> /etc/apt/sources.list'


My machine installed Vagrant to /var/lib/gems/1.8/ so I added /var/lib/gems/1.8/bin/ to $PATH.

Each Vagrant box you build should have its own directory for configuration and should be run from their, so you can create a test project now.

mkdir test-vagrant && cd test-vagrant

Also, there are reports of issues on some 64-bit machines and I couldn't get the base image to run, but the Ubuntu Karmic image is running fine for me, so this got me started with my first Vagrant box:
vagrant-box add karmic http://files.vagrantup.com/contrib/karmic.box
vagrant init
vagrant up

The Point, What Is?

Why do this? What is the value in being able to quickly build, run, and clone virtual machines? Here are a few ways I'm already using them and will use them (more) with a tool like Vagrant to make it nicer.

  • Keeping a definitive base of my development environment. I always have an image of a machine that I consider my minimum requirements for whatever project I might be working on. This is an Ubuntu image with all the tools I use, my vimrc and my virtualenv/pip shortcuts, etc. When I start a new project, I clone this image and add to it.
  • Making my specific environments reproducible. This one I have tried and can now start doing seriously with Vagrant. For any project, I can maintain a script to build a development environment on top of my base. The benefits are two part. First, I can keep a clean record of what is required to work with a project. Second, when a change is made to my base, I can rebuild my development environment for all of my projects instantly. (Well, I can issue the command instantly, but I'll probably each lunch before its done!)
  • VirtualBox images can be portable. It might even be possible to move suspended images, but I'm not completely sure about this, yet. If it turns out to be something I can do, I'll be able to suspend a project on my desktop, running the box off a USB key, and then resume it on my laptop in the park. Even if I can't do this, I can still build identical environments on multiple machines, for myself or for other developers.
  • Replicating production and building local staging setups, machine the setups I have at Linode and EC2, will become something I can do with a minimal effort. I'm going to save a lot of time deploying to clones of my production machines running right here under my desk.

UPDATES:
April 5, 2010 - Added links to Vagrant and VirtualBox websites. Added step to include repository for VirtualBox 3.1

Comments

YHVH said…
Does it also manage the copying of port forwarding ie :22 -> :2222 and :80 -> :8080. I have to set this up on every virtual machine I clone and it would be nice to be able to script
Anonymous said…
@YHVH: What's the port-forwarding for? Some reproduction of how your deployment servers will be?

Oh, or is it something to do with NAT mode? I usually run in bridged mode and just give each of my VMs its own IP (on my RFC 1918 network), so I'm a bit blind to the requirements of NAT mode. To be honest, I'm not really sure what the advantage of NAT mode is.
Marius Gedminas said…
Vagrant is not very googleable, a link would be nice.
Calvin Spealman said…
@YHVH Yes, port forwarding is managed by Vagrant and that is certainly one of the biggest boosts from it.

@Marius Thanks for pointing out my missing links. Fixed em'.
Calvin,

Thanks for the introduction to Vagrant. I've read your post and I've duly noted your documentation tips. I'll be sure to update the site with these changes before the next release.

And remember, I'm always in #vagrant (freenode IRC) if anyone needs any help.

Mitchell
Thank you for this helpful introduction. I didn't know about Vagrant before and I'm going to try it with Aeonic.

Popular posts from this blog

CARDIAC: The Cardboard Computer

I am just so excited about this. CARDIAC. The Cardboard Computer. How cool is that? This piece of history is amazing and better than that: it is extremely accessible. This fantastic design was built in 1969 by David Hagelbarger at Bell Labs to explain what computers were to those who would otherwise have no exposure to them. Miraculously, the CARDIAC (CARDboard Interactive Aid to Computation) was able to actually function as a slow and rudimentary computer.  One of the most fascinating aspects of this gem is that at the time of its publication the scope it was able to demonstrate was actually useful in explaining what a computer was. Could you imagine trying to explain computers today with anything close to the CARDIAC? It had 100 memory locations and only ten instructions. The memory held signed 3-digit numbers (-999 through 999) and instructions could be encoded such that the first digit was the instruction and the second two digits were the address of memory to operate on

Statement Functions

At a small suggestion in #python, I wrote up a simple module that allows the use of many python statements in places requiring statements. This post serves as the announcement and documentation. You can find the release here . The pattern is the statement's keyword appended with a single underscore, so the first, of course, is print_. The example writes 'some+text' to an IOString for a URL query string. This mostly follows what it seems the print function will be in py3k. print_("some", "text", outfile=query_iostring, sep="+", end="") An obvious second choice was to wrap if statements. They take a condition value, and expect a truth value or callback an an optional else value or callback. Values and callbacks are named if_true, cb_true, if_false, and cb_false. if_(raw_input("Continue?")=="Y", cb_true=play_game, cb_false=quit) Of course, often your else might be an error case, so raising an exception could be useful

How To Teach Software Development

How To Teach Software Development Introduction Developers Quality Control Motivation Execution Businesses Students Schools Education is broken. Education about software development is even more broken. It is a sad observation of the industry from my eyes. I come to see good developers from what should be great educations as survivors, more than anything. Do they get a headstart from their education or do they overcome it? This is the first part in a series on software education. I want to open a discussion here. Please comment if you have thoughts. Blog about it, yourself. Write about how you disagree with me. Write more if you don't. We have a troubled industry. We care enough to do something about it. We hark on the bad developers the way people used to point at freak shows, but we only hurt ourselves but not improving the situation. We have to deal with their bad code. We are the twenty percent and we can't talk to the eighty percent, by definition, so we need to impro