Monday, November 28, 2011

Using Vagrant

Vagrant is a great tool for creating vm at whim and tearing it down so that you could start all over again. It helps to start from a clean state, when you are trying to test deployment and setups. Vagrant requires VirtualBox and is written in Ruby.

Following is a step by step take down on how to setup and use vagrant on Mac

 Requires Ruby 

Using "RVM" is a great way to use ruby and ruby apps

curl -s -o rvm-installer
chmod +x rvm-installer
./rvm-installer --version latest

Add the following in bash profile:

# This loads RVM into a shell session
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

Install Ruby

rvm install ruby-1.9.2
rvm --default use 1.9.2

Install virtualbox 


Install Vagrant

gem install vagrant

Using vbox to create a VM

Create a directory for project:

mkdir dummyproject
cd dummyproject

Get a Ubuntu box:

vagrant box add lucid64

Initialize current folder for Vagrant usage:

vagrant init lucid64

Creates the vagrant environment & VM:

vagrant up

Various vagrant commands:

SSH into the instance: vagrant ssh ; sudo su - root
Reboot:                vagrant reload
Save and suspend:      vagrant suspend
Destroy:               vagrant destroy


Where is the information on added boxes stored?

In folder ".vagrant.d" in your home directory:
ls -la ~/.vagrant.d/boxes/
total 0
drwxr-xr-x  4 user1  staff  136 Oct 25 14:10 .
drwxr-xr-x  5 user1  staff  170 Oct 25 14:02 ..
drwxr-xr-x  6 user1  staff  204 Oct 25 14:04 lucid64
drwxr-xr-x  7 user1  staff  238 Oct 25 14:11 puppet-rhel-6-64

Where to get flavors of OS boxes?


CentOS, Ubuntu, Debian, Solaris, Redhat

Just do the following:

vagrant box add puppetrhel60-64
vagrant init puppetrhel60-64
vagrant up


In the directory, where you did "vagrant init ..", a file called "Vagrantfile" is created.

Contents of Vagrantfile: do |config|
  # All Vagrant configuration is done here.
  # Every Vagrant virtual environment requires a box to build off of. = "lucid64"
  # Url from where the '' box will be fetched, if not in system
  config.vm.box_url = ""
  # Boot with a GUI so you can see the screen. (Default is headless)
  # config.vm.boot_mode = :gui

This explains the "Three important things":

  • The box name to bootup in VirtualBox. 
  • This should be available via "Vagrant box add .." 
  • Or you could make this available via the url from where it could be fetched 
  • Do you want the GUI or you could do with just SSH 

You could do more with vagrantfile: do |config| ""
 config.vm.forward_port("http", 80, 8080)
 config.vm.share_folder("v-data", "/vagrant_data", "../data")

  • Normally you ssh into this VM via vagrant ssh 
    • Sometime it makes sense to access it through an IP address. 
    • This can be achieved through setting of 
  • Also you want to access, say "httpd" running on port 80 on VM, from another machines 
    • You could forward the "port 80" on the vm to your machine on some port say "8080"
  • Last configuration is merely for ease. 
    • You already have suitable editors etc installed on your machine. You could easily edit files on your VM via tools on your machine through this "shared folder" mapping

No comments:

Post a Comment