Vagrant How-to


Vagrant is a great devops tool used to uniformly start, provision and cleanup one or more virtual machines run locally.  It provides a uniform command line syntax to manage VMs which remains the same irregardless of platform (Windows, MacOS, Linux) and is provider agnostic (VMWare, Xen, etc).

The other useful feature is when you have a scenario that requires more than one virtual machine (in my case I was kicking the tires of Mesos, a distributed system of masters and slaves). With a single “vagrant up” you can launch multiple VMs coordinated through the same configuration file. 

The catch is that the configuration, called the Vagrantfile is written in Ruby.  I’ve never used Ruby before, but it’s easy enough to pick up to create an effective provisioning script. My approach is to put the actual provisioning instructions into a shell script, and using the Ruby Vagrantfile to bootstrap the launch of the shell scripts.


Details for all these instructions can be found in the Vagrant docs.

1 Install Vagrant

2 Install VirtualBox?

3 Create directory for your virtual environment

mkdir my-env

4 Initialize Vagrant

$ cd my-env
$ vagrant init

This will create a Vagrant config file: my-env/Vagrantfile

5 Look for a base virtual image to use. Note the name (“chef/centos-7.0”)

6 Add that base image name to your local vagrant registry

$ vagrant box add chef/centos-7.0

7 Add a custom image to your local vagrant registry (see #13)

$ vagrant box add  --name cxv/mybox

(name is what is referenced in #9a)

8 View local box registry

$ vagrant box list

9 Modify the Vagrant config to use that virtual image

edit Vagrantfile. Contents should be:

Vagrant.configure("2") do |config|
9a = "chef/centos-7.0"
config.vm.provider "virtualbox" do |v|
9b = "my_vm"
 v.memory = 4096
 v.cpus = 2
9c v.customize ["modifyvm", :id, "--cpuexecutioncap", "75"]

If a VM is running, any changes to the Vagrantfile only take effect after

$ vagrant reload

This is the same as halt+up

9c For custom VM configuration for VirtualBox see

10 Start VM (from directory containing Vagrantfile)

$ vagrant up
$ vagrant up --provider=aws|vmware_fusion|virtualbox ... more

virtualbox supported out of the box; need to install plugin for other providers

11 Access VM

$ vagrant ssh

note that /vagrant dir in guest vm is mapped to my-env on host

12 Shutdown

$ vagrant suspend -> all changes saved
$ vagrant halt -> graceful shutdown with all changes saved
$ vagrant destroy -> removes all traces but requires image to be reimported and reprovisioned

13 Saving Modifications to a New Image

VM must be running

 $ vagrant package --base  --output .box

Where is from #9b. Otherwise you need to enable the VirtualBox GUI and learn name from there since it’s generated if not specified.

Note: This will power down the VM. A simple “vagrant up” without any changes to the Vagrantfile will boot it up again with all previous changes in place.
Once a box is created, you can “vagrant destroy” it, add the new box to your local registry (#7), modify the Vagrantfile to refer to this new box, and “vagrant up”.

$ vagrant package --base mesosphere_default_1430403311486_55015 --output

(after creating a box see #7)


One comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s