Introduction to Vagrant - Part 1


Bhaskar S 01/14/2017


Overview

In a typical Enterprise, when a software developer is hired into a development team, there are two hurdles to overcome. One, the developer spends a significant amount of time setting up their development environment and two, they may have a development environment that may not be a replica of what is in production. Sounds familiar ???

Wouldn't it be great if there was a way to automate the creation and setup of a sandboxed development environment that is a replica of what is in production with just one command ?

Enter Vagrant !!!

Vagrant is an automation tool for creating and configuring a reproducible development sandbox using a virtual environment, such as, Hyper-V, VirtualBox, VMWare, or Docker.

Vagrant is configured per virtual environment. Each virtual environment has an associated configuration file that is named Vagrantfile. In the Vagrantfile one specifies the various configuration aspects of the virtual environment, such as the amount of memory, no. of cpus, shared folders, etc.

Vagrant leverages a base image for a desired virtual environment, such as a ubuntu xenial64 VirtualBox image, etc. This base image is called a Box in the Vagrant parlance.

Vagrant ships with an out-of-the-box support for the popular virtual environments, namely, Hyper-V, VirtualBox, VMWare, or Docker, etc. In the Vagrant parlance, these virtual environments are called Providers.

Installation

The installation is on a Ubuntu 16.04 LTS based Linux desktop.

Make sure to install VirtualBox before installing Vagrant.

$ sudo apt-get update

$ sudo apt-get install virtualbox

$ sudo apt-get install vde2 virtualbox-guest-additions-iso

$ sudo apt-get install vagrant

Once the commands complete, execute the following command to check everything is ok:

$ vagrant version

The following would be a typical output:

Output.1

Installed Version: 1.8.1

Vagrant was unable to check for the latest version of Vagrant.
Please check manually at http://www.vagrantup.com

Hands-on with Vagrant

Create a directory called Vagrant if not already present and change to that directory.

To create an initial Vagrantfile, execute the following command:

$ vagrant init

The following would be a typical output:

Output.2

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Search for a desired Box image for VirtualBox from Atlas by HashiCorp. For our test, we will use the pre-built Box for Ubuntu 14.04, called ubuntu/trusty64.

Edit the generated Vagrantfile in the current directory to have the following contents:

Vagrant.configure(2) do |config|

  config.vm.box = "ubuntu/trusty64"

  config.vm.provider "virtualbox" do |vb|

    vb.memory = "1024"

  end

end

This will configure a VirtualBox guest machine running Ubuntu 14.04 in headless (no GUI) mode with 1024MB of memory.

To create and configure a virtual environment, execute the following command:

$ vagrant up

The following would be a typical output:

Output.3

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'ubuntu/trusty64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'ubuntu/trusty64'
    default: URL: https://atlas.hashicorp.com/ubuntu/trusty64
==> default: Adding box 'ubuntu/trusty64' (v20170110.0.0) for provider: virtualbox
    default: Downloading: https://atlas.hashicorp.com/ubuntu/boxes/trusty64/versions/20170110.0.0/providers/virtualbox.box
==> default: Successfully added box 'ubuntu/trusty64' (v20170110.0.0) for 'virtualbox'!
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: Vagrant_default_1484445597151_33976
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (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: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.36
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => /home/vagrant/Vagrant

To check the status, execute the following command:

$ vagrant status

The following would be a typical output:

Output.4

Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

To login to the running VirtualBox guest machine, execute the following command:

$ vagrant ssh

The following would be a typical output:

Output.5

Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-107-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sun Jan 15 02:00:20 UTC 2017

  System load:  0.97              Processes:           83
  Usage of /:   3.6% of 39.34GB   Users logged in:     0
  Memory usage: 12%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

New release '16.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


vagrant@vagrant-ubuntu-trusty-64:~$

In the ssh session, execute the following command:

vagrant@vagrant-ubuntu-trusty-64:~$ free -m

The following would be a typical output:

Output.6

             total       used       free     shared    buffers     cached
Mem:           993        358        635          0         12        230
-/+ buffers/cache:        115        878
Swap:            0          0          0

To exit the ssh session, execute the following command:

vagrant@vagrant-ubuntu-trusty-64:~$ exit

The following would be a typical output:

Output.7

logout
Connection to 127.0.0.1 closed.

Hooray !!! we have successfully created and launched our first VirtualBox guest machine using Vagrant.

To shutdown the running VirtualBox guest machine, execute the following command:

$ vagrant halt

The following would be a typical output:

Output.8

==> default: Attempting graceful shutdown of VM...

To check the status, execute the following command:

$ vagrant status

The following would be a typical output:

Output.9

Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

To destroy and clean up all the resources created as a result of running the VirtualBox guest machine, execute the following command:

$ vagrant destroy

The following would be a typical output:

Output.10

    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...

To list all the available Boxes, execute the following command:

$ vagrant box list

The following would be a typical output:

Output.11

ubuntu/trusty64 (virtualbox, 20170110.0.0)

To add a new CentOS 7 Box image called centos/7, execute the following command:

$ vagrant box add centos/7

The following would be a typical output:

Output.12

==> box: Loading metadata for box 'centos/7'
    box: URL: https://atlas.hashicorp.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) libvirt
2) virtualbox
3) vmware_fusion
4) vmware_workstation

Enter your choice: 2
==> box: Adding box 'centos/7' (v1611.01) for provider: virtualbox
    box: Downloading: https://atlas.hashicorp.com/centos/boxes/7/versions/1611.01/providers/virtualbox.box
==> box: Successfully added box 'centos/7' (v1611.01) for 'virtualbox'!

Again, list all the available Boxes by executing the following command:

$ vagrant box list

The following would be a typical output:

Output.13

centos/7        (virtualbox, 1611.01)
ubuntu/trusty64 (virtualbox, 20170110.0.0)

From the above, we see that the CentOS 7 Box image for VirtualBox has been added.

To remove the the CentOS 7 Box image, execute the following command:

$ vagrant box remove centos/7

The following would be a typical output:

Output.14

Removing box 'centos/7' (v1611.01) with provider 'virtualbox'...

That is it for now ... More in the future part(s).

References

Official Vagrant Documentation