What is Vagrant and How to provision a Ubuntu using it

What is Vagrant and How to provision a Ubuntu using it


Vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the “works on my machine” excuse a relic of the past.

Why do you need Vagrant?

While at its core, Vagrant provides a rather simple function, it may be useful to a wide range of people working on different kinds of tasks.

For developers, Vagrant makes it easy to create a local environment which mimics the environment upon which your code will eventually be deployed. You can make sure you have the same libraries and dependencies installed, same processes installed, same operating system and version, and many other details without having to sacrifice the way your local machine is set up, and without the lag or cost of creating an external development environment and connecting to it.

How does it benefits?

Vagrant has benefits to developers, operations engineer, designers and more. Here the key is that Vagrant makes it really easy with the fact that there is no complication or using vim and loads of annoying command line stuff easy to run a development environment. Getting your first development virtual machine ready will take minutes.

Once you have finished developing, you can check in your changes, ask your colleague to check them out, and then they run the code on the exact same machine. This even works if they are on any part of world and is platform independent i.e. regardless of whether they are on Windows, Linux or Apple OS X. It is safe to say goodbye to “works on my machine” bugs after using Vagrant.

You’ll surely get to know the benefits better once you start using it. You can read more about benefits here.

Getting Started

Step 1 – Installing VirtualBox

First Download VirtualBox and install it. On *nix systems (Mac OSX, Linux, etc), you will need to modify your .bash_profile (or .zsh_profile) to extend your $PATH variable:

export PATH

This will allow Vagrant to know where VirtualBox is installed, and will, of course, vary for different operating systems.

Step 2 – Install Vagrant

You can download a vagrant build for your operating system, or install it as a gem if one is not available:

# gem install vagrant

Here are some terms which you must understand before we run our first vagrant box:

Vagrant Box

A box is basically a package containing a representation of a virtual machine running a specific operating system. To be more simple, it is a base image of any Operating System or Kernel. It may be for a specific Provider.


The Provider is the piece of software responsible for creating and managing the virtual machines used by Vagrant. The main providers are Virtualbox and VMware, but the default one is VirtualBox, since it’s free and open source.


Provisioner will do some task(s) using the vm instance already provided. The provisioners are used to set up the virtual server, installing all necessary software and executing different tasks. The most used provisioners are: Puppet, Chef and Ansible. Shell Script is also a very common option. You can find more information about vagrant provisioners here.

The Vagrantfile

The basic vagrant configuration is based in one file, the Vagrantfile. It shall be placed in your repository root. In this file you will define which base box you want – a box is, basically, a package with an operational system to be run in your virtual machine.

Creation of Instance

Create a test directory where we would be creating our first instance.

# mkdir -p ~/Vagrant/test
# cd  ~/Vagrant/test

We’ll be using Ubuntu 16.04 LTS , which already has a “box” set up.

vagrant box add reconz/xenial-16.04

You see here the argument reconz/xenial-16.04 which is a nickname for the URL. The box is downloaded at ~/.vagrant.d/boxes. You can now create an instance:

vagrant init reconz/xenial-16.04
vagrant up

If you want to get into this instance, via SSH, use this command:

vagrant ssh

Provision an instance using VagrantFile

The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines. Vagrantfiles are called Vagrantfiles because the actual literal filename for the file is Vagrantfile (casing does not matter unless your file system is running in a strict case sensitive mode).

Vagrant is meant to run with one Vagrantfile per project, and the Vagrantfile is supposed to be committed to version control. This allows other developers involved in the project to check out the code, run vagrant up, and be on their way. Vagrantfiles are portable across every platform Vagrant supports.

The syntax of Vagrantfiles is Ruby, but knowledge of the Ruby programming language is not necessary to make modifications to the Vagrantfile, since it is mostly simple variable assignment. In fact, Ruby is not even the most popular community Vagrant is used within, which should help show you that despite not having Ruby knowledge, people are very successful with Vagrant.

Here will see some sample vagrantfile to install Ubuntu 16.04 along with apache package,

# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "reconz/xenial-16.04"
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false
  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  config.vm.network "forwarded_port", 80, 8080
  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: ""
  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"
  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"
  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  # config.vm.provider "virtualbox" do |vb|
  # # Display the VirtualBox GUI when booting the machine
  # vb.gui = true
  # # Customize the amount of memory on the VM:
  # vb.memory = "1024"
  # end
  # View the documentation for the provider you are using for more
  # information on available options.
  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  # end
  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
    config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y apache2

Read More: Create Multiple Virtual Machines Using Vagrant