Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

Vagrant up And Beyond!

When you are working on a project with a team, a big question is how to make them work on same environment? One solution is to make them work on same machine, but this gets messy as everyone is making changes. The other solution is to provide everyone with same environment. And that’s where Vagrant comes! In this tutorial we will learn how to use Vagrant.

Vagrant gives you portable work environments which can be controlled by a pre-defined workflow. The idea is when you are working on a project and there are a lot of dependencies, configuration settings, scripts involved, you can combine them all up with vagrant and pass it around to be used by everyone. Let’s start with getting a VM up and running.

Though Vagrant comes with default support for VirtualBox, it can be used with other providers too like VMware, AWS etc. Here I am using VirtualBox on Fedora 19 as provider. You can download and install VirtualBox from here. Next step is to install Vagrant, which you can get from here.

Now, after installing VirtualBox and Vagrant, you can add the box and run the machine. We will learn more about boxes, first run these two commands:

$ vagrant init precise32 http://files.vagrantup.com/precise32.box
$ vagrant up

It will take some time to set everything up and running the virtual machine. Now you can simply do:

$ vagrant ssh

and you will be able to login in your virtual machine. Coming back to boxes, they are nothing but the base images, which are used to clone a virtual machine. This makes the process faster instead of running a machine from scratch.  You can also add new boxes to the vagrant environment.

$ vagrant box add precise32 http://files.vagrantup.com/precise32.box

So, how do you control the whole environment? Well, that’s why we have a Vagrantfile! The primary objective of a Vagrantfile is to describe the type, attributes etc. of the virtual machine. There is one Vagrantfile per project and it takes care of all the installation, script execution etc. When you do “vagrant init” in a directory, a Vagrantfile is placed there. Now, you can use this Vagrantfile to launch virtual machines from this directory. If you want to do it from another directory, you will first have to do “vagrant init” there or you would have to copy Vagrantfile in that directory.

Vagrantfile can be ported from one platform to another. It is written in Ruby, but you don’t need to learn Ruby to specify attributes through it. Let’s say, after launching a machine, you want to run a shell script on it. The content of the Vagrantfile would be:

Screenshot from 2013-12-23 15:23:11

As you can see here, the name of the box and machine is fedora1 and I have given the provisioner as shell and the script that is going to run (after the virtual machine is booted) is bootstrap.sh. Here bootstrap.sh is present in the same directory, you can also give the absolute./relative path to you shell script. You can modify the Vagrant file and use

$ vagrant reload

to reflect the changes in already running virtual machine.You only need to learn how to assign values to different variables to make new Vagrantfiles. This should be enough to get you started. To check out their documentation, please visit the official website here.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy