Ashish Jaiswal Blog

A blog dedicated to community

How to Setup Vagrant

What is Vagrant ?

The big question, which I was thinking, when I first heard of it. So today finally got a chance to do some hands on. Basically it is a tools which gives you running VM, whenever you need it with all your environment set in place. So what was that huh? Consider that you are creating a VM or clonning a VM and starting it up and running some script to get the environment set in place. So why not automate this whole thing by vagrant.

Vagrant allows you to launch VMs with provisioning on top of it.

Image

Some Nomenclature

box : Base image of an OS, Read here

Provider : Hyperviosr in which VMs are going to run. These are Virtualbox, VMware and many more. Read here

Provising : Tools like Puppet, Chef, Ansibal and ofcourse Shell script. Read here

Working environment is Ubuntu 12.04

Install this packages

Terminal
1
sudo apt-get install dpkg-dev virtualbox linux-headers-generic linux-image-generic linux-generic vagrant

Know the Virtualbox and Vagrant is installed, we will add box in vagrant.

Terminal
1
vagrant box add precise32 http://files.vagrantup.com/precise32.box

It will take some time, and depends on your bandwidth. After the download is completed, there will be a Vagrantfile in the working directory.

Terminal
1
2
ajaiswal@c-0242:~$ ls -l
-rw-rw-r--  1 ajaiswal ajaiswal 3876 Jul  1 12:31 Vagrantfile

Open the Vagrantfile and change this line to

Terminal
1
config.vm.box = "precise32"

You can list the boxes, like this

Terminal
1
2
ajaiswal@c-0242:~$ vagrant box list
precise32

Now we can start the box with this command.

Terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ajaiswal@c-0242:~$ vagrant up
[default] Importing base box 'precise32'...
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.

Guest Additions Version: 4.2.0
VirtualBox Version: 4.1.12
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Mounting shared folders...
[default] -- v-root: /vagrant

Then we will login into the box.

Terminal
1
2
3
4
5
6
ajaiswal@c-0242:~$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2

Running some command under the box.

Terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vagrant@precise32:~$ ll
total 48
drwxr-xr-x 4 vagrant vagrant 4096 Sep 14  2012 ./
drwxr-xr-x 3 root    root    4096 Sep 14  2012 ../
-rw------- 1 vagrant vagrant  114 Sep 14  2012 .bash_history
-rw-r--r-- 1 vagrant vagrant  220 Sep 14  2012 .bash_logout
-rw-r--r-- 1 vagrant vagrant 3486 Sep 14  2012 .bashrc
drwx------ 2 vagrant vagrant 4096 Sep 14  2012 .cache/
-rwxr-xr-x 1 vagrant vagrant 6487 Sep 14  2012 postinstall.sh*
-rw-r--r-- 1 vagrant vagrant  675 Sep 14  2012 .profile
drwx------ 2 vagrant vagrant 4096 Sep 14  2012 .ssh/
-rw-r--r-- 1 vagrant vagrant    0 Sep 14  2012 .sudo_as_admin_successful
-rw------- 1 vagrant vagrant    6 Sep 14  2012 .vbox_version
-rw------- 1 vagrant vagrant   12 Sep 14  2012 .veewee_version

Hope this clear your basic doubts, Feel free if there is any question.

Comments