Creating Virtual Machines using Vagrant – 2

Reading Time: 3 minutes

Hi Readers, In previous blog we learned about the WHAT and WHY of Virtual Machines and Vagrant. We understood the Virtual Machine life cycle, problems while manual setup of Virtual Machine. Apart from this we also saw benefits of Vagrant and vagrant architecture.

Now in this blog we will try to setup virtual machines automatically using Vagrant.


  • Vitualization Technology Enabled in BIOS
  • Vagrant tool
  • Hypervisor like Oracle Virtualbox
  • CLI (GIT bash/CMD prompt/Terminal)

Script to Install Virtualbox

First of all we will need a Virtualization tool  which allows users and administrators to easily run multiple guest operating systems on a single host. We are going to use virtualbox. You can install it from here or can follow below script.

sudo apt-get -y update
sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-6.1.8.vbox-extpack
echo "============= Virtual box installed ==============="
sudo apt-get -y update

Directory Structure

We will create following directory structure for setting up various virtual machine. In this blog we are going to spin up virtual machine using Ubuntu box from vagrant cloud.

Script to Setup workspace

Let us setup workspace for our work. In below script we are going to,

  • Create two directories, one for ubuntu and another for centos inside workspace folder.
  • Move to ubuntu directory and initialize ubuntu virtual machine using ubuntu box.
  • Start the ubuntu virtual machine.
# Create a workspace, and inside that create two sub directories(it could be n as well)
for i in centOS ubuntu; do mkdir -p workspace/$i; done
echo "============= workspace created =============="
cd workspace/ubuntu
#initialize your VM by mentioning box name
echo "============= Initializing vm using boxName ==============="
# replace boxname with box from vagrant-cloud
vagrant init ubuntu/trusty64
#run the initialized box.
echo "============= start and provision the vagrant environment ==============="
vagrant up
echo "=========== Current Vagrant version =============="
vagrant status --version

You can get various boxes from vagrant cloud.

Script to setup application

Now we need to create index.html which will contain a small HTML script and which will install apache2 in our ubuntu virtual machine and run index.html.

mkdir html
cat > ./html/index.html << EOF
<!DOCTYPE html>
    <h1>Hello Knolders!</h1>
echo "============= index.html created =============="
cat > ./ << EOF
#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
  rm -rf /var/www
  ln -fs /vagrant /var/www
echo "============= created =============="

Provisioning Virtual Machine in Vagrantfile

After that, we need to provision our ubuntu VM by providing reference to inside our Vagrantfile. And then we need to reload our VM.

#Provision the VM by adding script to Vagrantfile
sed -i '62 i config.vm.provision :shell, path: ""' Vagrantfile > sed_out
echo "============= VM Provisioned with  =============="

# Reload Provisioning
vagrant reload --provision
echo "============= reloaded VM =============="

SSH to Virtual machine

Now we can enter to our VM and hit the localhost URL to see index.html loaded.

# ssh to your vm
echo "============= connect to your VM =================="
vagrant ssh -c 'wget -qO-'

Destroying Virtual machine

After that, when we are done with our work we can destroy our virtual machine.

#destroy VM
vagrant destroy

That’s all for this blog. Hope you found some information from this. You can find the source code and other informations related to VM and Vagrant from this github repository. In case of any queries you can contact me over my email id

Written by 

Nitin Mishra is a Software Consultant at Knoldus Software LLP. He has done MCA from GGSIPU and completed Bachelor of Science in Computer Science from Delhi University. He is a tech enthusiast with good knowledge of Java. He is majorly focused in DevOps practice. On personal front he loves to travel mountains and writes poetry.