After reading through our previous introductory article on Pragmatic DevOps, you most certainly want to dive right in and try Vagrant out for yourself. No need to hunt around the web to collect the instructions, just follow the few simple steps below and you’ll get a taste of Vagrant in no time.

Provide Me Some Virtual Goodness

Vagrant automates the use of virtual machines (VM) to create reproducible environments on-the-fly. The core piece of software here is the VM, but Vagrant doesn’t actually include any virtualization technology. Instead, it integrates with third-party software, which are aptly called ‘providers’.

To get started, it’s easiest to download Oracle’s VirtualBox, an open-source cross-platform consumer VM provider. Just look for your operating system as the host option, download VirtualBox and install the software. Note that Vagrant supports other providers like VMware or Amazon EC2, but that’s a topic for later.

At this point, you could start the VirtualBox application, create a new VM through the GUI, fine-tune the settings, download an installation disc image, start the machine, install the operating system, install software on the newly created VM, configure networking, set up a file-sharing infrastructure with your host OS, … but that would take you a few hours to complete and it’s tedious work!

So, let’s not do this manually, and install Vagrant instead!

Your First Taste of Vagrant

Vagrant is just as easy to install as VirtualBox, so just head over to the site, download the latest version for your operating system and let the installer do its job.

Now, open a terminal or command prompt and type:

vagrant -v

It should report back with the version of Vagrant that you just installed, meaning that it’s time to move on to the really cool stuff.

To make your life easy, many people are providing VM starting points that are called ‘base boxes’. They usually contain the bare minimum of an operating system, so that you don’t have to sit through the installation process yourself. The site vagrantbox.es provides a handy list of freely available base boxes. One of my favorite features of Vagrant is that you can very easily create fuller-featured base boxes yourself afterwards, but we’re getting ahead of ourselves.

Imagine that you want to start a machine with Ubuntu Linux 12.04, all you have to do is create a new working directory for Vagrant and use the ‘init’ command, like so:

This will create a Vagrantfile in your current directory that contains the configuration of your virtual environment. Most entries are commented out with detailed instructions about their purpose, but the few that are active basically tell Vagrant that you want to start that Ubuntu machine we mentioned before:

Now you’re ready to see your first piece of Vagrant magic, by using the ‘up’ command:

vagrant up

This might take a while, as it automates all the tedious VM installation steps that we mentioned in the beginning of the article. Once the command prompt is ready again, your virtual machine is up and running, ready to do your bidding.

Note that the downloaded box file is also stored locally and added to the list of known boxes, so that you can easily use it for other VMs later.

Host and VM, Sitting In A Tree, …

This is all child’s play, so let’s take a look at how your host machine and the VM can conveniently work together, hand in hand.

Connecting to the Ubuntu VM is done through the ‘ssh’ command:

vagrant ssh

This logs you into the virtual machine and gives you console access to it. The connection to your VM requires your host machine to have SSH installed, which is the case on all Unix-like machines. On Windows you’ll have to use a Unix-emulation layer like Cygwin or use a GUI SSH client like PuTTY . Vagrant provides you with detailed instructions when it can’t find a suitable SSH command on your system.

During VM startup, Vagrant has automatically created a ‘/vagrant’ directory in the VM that is linked to the working directory that you created above. Any files that you change in the working directory will become automatically available to the VM and vice-versa. This way, you can use your favorite tools and editors on your host machine and benefit from the software on the VM at the same time (for instance, using Apache Httpd with PHP as a server).

As an example, type this while you’re connected to your VM over SSH:

echo "vagrant rocks" > /vagrant/shared.txt

Now return back to your host’s terminal by typing ‘exit’, or by pressing ctrl+d on a blank line:

exit

You can now see the contents of the file you created in the VM:

On a Unix terminal:

cat shared.txt

On a Windows command prompt:

type shared.txt

Clean Up and Reproduce Yourself

You’re free to wreck as much havoc as you want on your virtual machine, it’s totally isolated from your host. When you’re done, all evidence can be eradicated with one simple command:

vagrant destroy

There will be no trace left on your host!

As a bonus, before cleaning up, try this out:

vagrant package --output=my-env.box

Can you guess what it does? Right! It neatly shuts down your VM and packages it with all your changes into a new box file. This can be used as a base for other virtual machines, all you have to do is to make Vagrant aware of its existence:

vagrant box add my-env my-env.box

Now you can create a new machine from that box, using the same ‘init’ command from before:

vagrant init my-env

Next up: Bootstrapping Chef & Your First Fully Automated Environment

You’ve seen how easy it is to bring virtual environments up and down with Vagrant. You can even create your own boxes and share them with colleagues. For more advanced use cases and practical examples of file sharing and port forwarding, stay tuned for our upcoming Pragmatic DevOps report from RebelLabs.

The next article in this series will help you get up and running with using Chef for provisioning. This means that the process of installing software on the VMs and configuring it, will also be completely automated. It really allows you to use one command to go from a blank slate to a full-blown all-cannons-blasting super-powered giant robot world-domination machine. After that, we’ll show you how to set up what could be your first fully-automated environment, and put you on the road to DevOps glory!

As always, I would enjoy seeing your comments below. To connect on a more personal level, join us on Google+, or tweet something @Rebel_Labs or @gbevin.

Geert is a developer with a passion for programming. He has steadily contributed to many open-source projects over the years (Gentoo Linux, RIFE, Terracotta, Ehcache, OpenLaszlo, Juce to name a few). Geert has spoken at many developer conferences, mostly about Java byte-code manipulation, continuations, his framework RIFE and scalability; he was also selected as one of the Java Champions. Geert's other passion is music and he has performed live and recorded a few albums; he composes, writes, plays the guitar, the Eigenharp, the LinnStrument and sings. Aside from that he likes to ride the motorbike when he gets the chance and recently picked up a keen interest in tabletop roleplaying games. Geert works at ZeroTurnaround as the XRebel product manager and also participates in advocacy and RebelLabs content creation.

Just wanted to say thanks for this series! I’m a noob running through Getting Started With Django and had no idea what any of this stuff actually did. This series of articles has been incredibly helpful in understanding why Vagrant/Chef are useful, so thank you!