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:
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:
mkdir my-env cd my-env vagrant init precise32 http://files.vagrantup.com/precise32.box
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:
Vagrant.configure("2") do |config| config.vm.box = "precise32" config.vm.box_url = "http://files.vagrantup.com/precise32.box" end
Now you’re ready to see your first piece of Vagrant magic, by using the ‘up’ command:
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:
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:
You can now see the contents of the file you created in the VM:
On a Unix terminal:
On a Windows command prompt:
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:
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!