Wednesday, November 16, 2016

Playing with Vagrant

Vagrant is a way to handle virtual machines for development or other uses.

Here some practical notes about this tool and how to use it.


First of all your development  machine must have some program installed.
This document assumes to use a Linux box, based on Ubuntu 16.04 LTS 64 bit but is possible of course also to have Vagrant on a Windows or MacOS  machine.
The basic principles apply as well to the other environments.

It is necessary to have at least two program installed :

  • a 'provider'
    i.e. a program capable to handle the virtual machine.
    The typical choice is VirtualBox, is one of the most available VM handler and almost all the Vagrant images are supported by Virtualbox.

    A 'sudo apt-get install virtualbox' should be enough
  • Vagrant
    Of course is necessary to have Vagrant installed on the machine.

    sudo apt-get install vagrant


After installing the required programs, is possible to start to use Vagrant.

The first thing to do is to identify what operating system you want to install on the virtual machine.
The environments, operating system to be installed, are called "boxes", so you need to identify what box to install.
The question is, "where are the boxes" ?

There are some free public boxes available at the Hashicorp (the company who handle Vagrant) and is possible to set up/use other repo, public or private.
By default Vagrant is set up to use the free public space hosted by Hashicorp.

You need to create an account to do so, is free.

Let say we want to install a CentOs box, let identify the closest CentOs box in the free repo.
To do so, once logged in, select some search criteria, like what virtual environment to use (in my case Virtualbox) and the name of the distro (CentOs).

Then pick up a box that you think is the better choice.
Clicking on a box name will show detailed information or links about the box and also will show what vagrant command to use to install the box.

For example to install the first box in the list, open a terminal and execute :

vagrant init -f centos/7
vagrant up --provider virtualbox

This will create a configuration file in the terminal place (usually ~) called Vagrantfile and then the CentOS image will be downloaded and installed in VirtualBox.


At this point Virtualbox has ready a machine with CentOS (like in the example).
It is possible to start the installed machine from Virtualbox, as any installed machine, or via vagrant directly with the command vagrant up.
In this case nothing is indicating that the machine is running if not the status on virtualbox and it will be possible to access to the installed machine using the command vagrant ssh.

Usually by default any installed machine has set up a user called vagrant (password vagrant) or it can be accessed using root and the password vagrant.


There are some advantages to use Vagrant to create virtual machines.

  1. No need to keep a OS image on the local machine
  2. Easy to recreate the machine if something goes wrong
  3. Possibility to create your own machine image
The last point is the most important.
You can start to create a virtual machine using your preferred base image, then update it and install the programs you need to use, then store that image in a Vagrant server or a local one.
After that, you can recreate your virtual machine every time you need it, without the need to configure/update/install programs.
You end up to install in minutes an entire machine pre-configured for your needs.

No comments:

Post a Comment