Monthly Archives: September 2015

Getting started with Vagrant


I recently had to look deeper into Vagrant. Sure I heard about it, but I never got my hands dirty with it. As usual, I try to document most things of what I learn. Otherwise, I will have forgotten about it by next month. So, let’s get started.

What is Vagrant?

Vagrant allows you to create virtual machines on top of VirtualBox (by default), VMWare, AWS or other providers. It provides a very intuitive CLI to control these instances. To configure the virtual machines, standard provisioning tools such as shell scripts are available, but also tools like Chef, Puppet (and some others) could be used to install and configure software on the machine.

With some simple commands, Vagrant allows you to:

  • Create virtual machine using your preferred OS system
  • Modify physical properties (memory, hard disk size…)
  • Set up network interfaces for your virtual machine
  • Set up port forwarding
  • Boot and destroy your virtual machines
  • Provision software on your virtual machines
  • ….

Why Vagrant?

Obvious question indeed is why would you want to use Vagrant in the first place. Basically, prior to Vagrant, the standard way of working was to install all development tools and software on your local machine. This used to be OK in a time when there was just a few technologies around (e.g. in web development, most people were only using PHP and MySQL). Nowadays, there are way more options available, not only programming languages, but also databases, webservers, application services…It becomes quite tricky to maintain and update all these installs manually.

The basics

Let’s start to do some practical stuff. I’m going to assume you have already installed vagrant for your OS. So you should be ready to create your first virtual machine. We’ll call it ‘my-first-machine’ and will make it available on Virtualbox.

First, create a working directory on your host PC.

macbook-wim:Vagrant wim$ mkdir FirstVagrantTest
macbook-wim:Vagrant wim$ cd FirstVagrantTest/
macbook-wim:FirstVagrantTest wim$

Then issue the following command to initialize vagrant:

macbook-wim:FirstVagrantTest wim$ vagrant init precise64

You will see now that a Vagrantfile was created in your newly created folder on your host PC. That Vagrantfile can be used to configure your virtual machine.

Open the Vagrantfile in your favorite text editor and change the following lines so it looks like the below snippet. Note that this does not do a lot, but I said before I wanted to call it ‘my-first-machine’ so just keeping up promises here 🙂

  config.vm.provider "virtualbox" do |vb| ="my-first-machine"

Starting, accessing, suspending and deleting the virtual machine

Then, issue the ‘vagrant up’ command and the virtual machine will become available on your Virtualbox environment.

macbook-wim:FirstVagrantTest wim$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: my-first-machine
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address:

You will see in below picture that a VM was created with name ‘my-first-machine.

Now that the virtual machine is running, we can also SSH into it. Indeed, Vagrant took care of setting up a port forwarding between port 22 and port 2222. Issue the following command to get into your virtual machine:

macbook-wim:FirstVagrantTest wim$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:
New release '14.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from

You are now inside your virtual machine environment and you can do whatever you want (e.g. install your preferred software….). In order to exit again, just type ‘exit’ at the prompt.

In case you want to suspend the virtual machine (the state will be saved), issue the following command:

macbook-wim:FirstVagrantTest wim$ vagrant suspend
==> default: Saving VM state and suspending execution...

You’ll see in Virtualbox that your machine is in state ‘saved’. In order to bring it up again, run again the ‘vagrant up’ command.
Finally, to destroy the machine, do the following:

macbook-wim:FirstVagrantTest wauterw$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...

OK, so this was a first small tutorial on Vagrant. Probably I’ll add some more advanced Vagrant tutorials to this blog if time allows.

Thanks for reading!