Vagrant Getting Started Create and Share Virtual Environments

  Reading Time:

Vagrant is an easy virtual environment setup tool built to use some of the best Virtual Machine technologies like Virtual Box, VMWare and others to manage and configure various virtual environments. Using Vagrant we can build environment which contains all the dependencies for a project and share it with others using just the single “Vagrantfile” containing all the provisions required for that project so that all others would have the same setup. You may be a Developer, OPS Engineer, Designer, all can share the same environment with just the “Vagrantfile” config file for Vagrant.


The official Vagrant docs states it as being a, “Easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.


Vagrant uses the Base Image and applies the configuration options present in the Vagrantfile and creates a new Virtual Machine which can then be provisioned to our need.

Setting up Vagrant

Here VirtualBox is used with Vagrant since it is the best free VM available.

First get Vagrant and Virtual Box

Once you download and install the above packages you will have a vagrant command in your system which will be added to your system path. Check the version of Vagrant by typing vagrant -v.

Once a vagrant machine is created, provisioning tools such as shell scripts and Chef can be used to install and configure software packages into the machine.

Initializing Vagrant

When everything is in place, run vagrant init in a directory where you need to initialize the vagrant. This will create a Vagrantfile inside that directory. This is the config file that vagrant uses to configure the Base Image.

vagrant init

Config using Vagrantfile

Vagrant uses Base Images to clone and create a new Virtual Machine. These Base Images are called as Boxes. Boxes are available at HashiCorp’s Atlas Box Catelogue. When found the right box required use the following command to add it.


vagrant box add [options] <name, url, or path>


vagrant box add ubuntu/trusty64

Here we are using the ubuntu/trusty64 for example.

Once it has finished downloading the Base Image, modify the Vagrantfile to inform Vagrant on which base image to use.

Vagrant.configure("2") do |config| = "ubuntu/trusty64"

Running and Interacting

Vagrant Up

To initiate a vagrant machine run the vagrant up command. This will setup up a new Virtual Machine with the configuration options applied from the Vagrantfile and will be running.

vagrant up

Vagrant SSH

Once the VM is up and running, you can access it using SSH by running vagrant ssh command. This will SSH you into the VM that is running.

vagrant ssh

Synced Folders

With Sync Folders it is easy to access files to and from the Host and Guest machines. By default the Guest machine shares the folder /vagrant to the host machine, which in host machine is the folder where we created the Vagrant i.e where the Vagrantfile is present. This folder is in sync with the /vagrant in the guest machine.

Create a directory name www in the directory where the Vagrantfile file is present as shown below.

sync host view

When viewed from inside the guest machine, the folder that we created will be available inside the guest’s /vagrant folder.

sync guest view

You can also configure the synced_folder as shown below.

Note: There are also options that can passed to modify the behaviour of synced_folder like create option, which is a boolean that creates the host path if it does not exists.

Vagrant.configure("2") do |config| = "ubuntu/trusty64"
  config.vm.synced_folder "src/", "/srv/website", create: true


We have setup an environment with Ubuntu and have a folder synced to the host machine. But what good does it do if there are not other software packages. Even if we install the software packages by SSHing into the VM, it won’t do any good if we wanted to share it with others as they would have to do the same process and must have the same version that we used to develop.

This is where provisioning comes into play. You can specify a way for Vagrant to provision the list of software packages to be installed while running vagrant up and vagrant reload --provision commands.

Create a new shell script inside vagrant/ and add the following script.

#!/usr/bin/env bash

apt-get update
# Install apache, php - Redirect output and error into setup.log
apt-get install -y php5 apache2 libapache2-mod-php5 2>&1> setup.log

if ! [ -L /var/www ]; then
  rm -rf /var/www/
  # Created symbolic link.
  ln -fs /vagrant/www /var/www/html

Then modify the Vagrantfile by adding the following config option.

Vagrant.configure("2") do |config| = "ubuntu/trusty64"
  config.vm.synced_folder "src/", "/srv/website", create: true
  config.vm.provision :shell, path: ""

This config tells Vagrant to run this setup provision when it loads the VM or it can be forced to just run the provision.

Now run vagrant reload --provision if you have you VM running already or vagrant up to provision the softwares and start the machine. The above script will install php5 and apache2 inside the guest machine.

Create a new index.php file inside a folder www within vagrant/.

Run the following code which will output Hello world from the index.php.

wget -qO- index.php

Guest to Host Networking

So we have a web server running inside the guest machine, but it’s now use until we see it in a browser right. Let’s get it accessible in the Host machine’s browser using the Networking feature in Vagrant.

Port Forwarding

To access the guest machines server(apache2) which is running in a specific port(80) inside the guest, we use port forwarding. Which is basically saying, forward all the request to a port(3333) in the Host machine to the port(80) on the guest machine.

So a request to is forwarded to the guest machines port 80 where apache is running.

Configure the Vagrantfile for Port Forwarding

Add the following line into the Vagrantfile. :forwarded_port, guest: 80, host: 3333

You get the idea right. Let’s run vagrant up if the machine is not running and vagrant reload if it’s not running.

In a browser in the Host machine visit the url and you will see the following output.

Port Forwarding

Vagrant Share

It’s was really easy to setup port forwarding and access the server running inside the guest machine. What if someone else wants to access the VM Guest machine. It can be done using Vagrant share. The first step to achieve this is to create an account at HashiCorp’s Atlas.

After you have signed-up login into Hashicorp using vagrant login and providing your credentials.

Now run vagrant share which provides you an URL which you can use in any system connected to the internet to access the apache server running inside the guest machine.

$ vagrant share
==> default: Your Vagrant Share is running!
==> default: URL:

Of Course apart from just accessing the http server, you can connect to the guest using ssh.

$ vagrant share --ssh

The above command gives you an Name which can be used in connecting to that machine using ssh by using that name vagrant share --ssh Name.

Teardown – Save Stop Kill

There are 3 ways you can clean up the vagrant that is running,

  • suspend – Save the current state and stop the machine. Pros is faster start/stop. Cons is requires more resource(RAM, Disk Space).
  • halt – Clean shutdown of the machine. Pros no resource required to store state. Cons require more time/slower.
  • destroy – Removes all traces of the machine. Pros no RAM or Disk space is used, every thing is erased. Cons require lots more time to get the machine running again.

When you decide as to run which command use it with vagrant.

vagrant [,suspend, halt, destroy]

Using vagrant is a lot of ease in development and could be used in a lot of situations to make the development process better. To know more view the Vagrant Documentation

Writing Reusable Code using Traits in PHP

Traits are way for code reuse which makes it easier to include the same code(trait) in different locations(classes). PHP is a single inheritance language,...

Facebook Style Search Feature using Twitter Typeahead.js and PHP

Search is one of the interesting feature Facebook has invented. It is way to search the various connections inside of Facebook for a user. The style...