Vagrant - Getting Started

Vagrant Logo

Vagrant is a tool for managing virtual machines. It provides easy to configure, reproducible and portable work environments.

Machines are provisioned on top of :

  • VirtualBox
  • VMWare
  • AWS etc.

Vagrant helps to isolate dependencies and their configuration within a single disposable, consistent environment.

Up and Running

mkdir vagrant_getting_started
cd vagrant_getting_started

#Create Vagrant File
vagrant init <boxname>

#Start Vagrant Box
vagrant up

#Connecting to Vagrant Box
vagrant ssh

Using Boxes

Vagrant.configure("2") do |config| = "hashicorp/precise32"

List of Boxes -

Provisioning with Vagrant

Vagrant has built-in support for automated provision. Using this feature, it allows to automatically install software when you do vagrant up

You can also provision using vagrant provision

Below example will run a script file during the provision process.

Vagrant.configure("2") do |config| = "hashicorp/precise32"
  config.vm.provision :shell, path: ""

echo "Adding Necessary Stuff"

sudo apt-get update
sudo apt-get upgrade
sudo apt-get -y install build-essential
sudo apt-get -y install nodejs npm node-semver
sudo apt-get -y install git
sudo apt-get -y install openjdk-7-jdk

Networking with Vagrant

Vagrant.configure("2") do |config| = "hashicorp/precise32"
  config.vm.provision :shell, path: "" :forwarded_port, guest: 80, host: 4567 :forwarded_port, guest: 5000, host: 5000


vagrant suspend
vagrant halt
vagrant destroy

Packaging a Customized Vagrant Box

vagrant package --vagrantfile Vagrantfile.pkg --output
vagrant box add mynewbox

mkdir newBox
cd newBox
vagrant init mynewbox