How I setup development environments with Vagrant

Having test environment as similar as possible to your production setup is a must to prevent problems caused by different software versions or configuration. I also like the freedom to try out different web development frameworks, database servers, etc without cluttering up my actual desktop system.

It’s possible to achieve all these by running development environment in virtual machine and Vagrant is an application which makes setting up virtual machines and sharing them with coworkers easy. Additionally it will be simple to re-create your setup when re-installing or changing your OS or hardware with the help of Vagrant.

First time setup for a project is described here and if you are participating in a project where Vagrantfile is already present first two sections here contains steps for getting started with it.

Installing Vagrant and dependencies

  1. Install VirtualBox from official site or your system repositories. In Fedora this could be achieved by:

    # yum install VirtualBox
  2. Download and install Vagrant from the official site. There are packages available for all popular systems.

Setting up Vagrant for your project

Following command line is used to initialize Vagrantfile for your project.

$ vagrant init {box-name} {box-url}

For example when using default Ubuntu Precise Pangolin box provided by Vagrant project, it is initialized with following:

$ vagrant init precise32

I have created my own minimal Debian Wheezy based box, which is available at:

Next start virtual machine:

$ vagrant up

This will download base box from given url if it’s not already available locally and configure it according to the Vagrantfile. Directory containing the Vagrantfile will be mounted to /vagrant in the virtual machine using NFS.

Accessing virtual machine using SSH is possible with command:

$ vagrant ssh

Creating custom base images

It’s quite simple to bake your own base box for vagrant virtual machines. Basically it’s possible to package any VirtualBox machine as a Vagrant base box with command:

$ vagrant package --base {machine-name-in-vbox}

However there should be user vagrant with sudo rights present and all non-necessary services and applications should be pruned to minimize base box size.

Mike Griffin has written a great article about creating minimal Debian Wheezy base box which I recommend to get more detailed information.

Of course there are images provided by Vagrant itself so it’s not always necessary to bake you own.


To be able share set of installed apps and configuration without including them to the base box it is necessary to provide script for installing them with you Vagrantfile.

Vagrant supports number of configuration management systems like Chef and Puppet but I have been using simple shell scripts for now.

Here’s simple example about installing basic LAMP setup in my wheezy base box.

#! /bin/bash
# Simple install script to run as shell provisioner for vagrant
export DEBIAN_FRONTEND=noninteractive
# Refresh apt repositories and install updates
apt-get update -q
apt-get upgrade -yq
# Stuff for LAMP environment
apt-get install -yq mysql-server apache2 php5
# Initialize test database
echo "CREATE DATABASE IF NOT EXISTS testing_db;" | mysql -u root
echo "GRANT ALL ON testing_db.* TO 'test_user'@'localhost' IDENTIFIED BY 'password';" | mysql -u root

If file above is saved as following should be included in the Vagrantfile.

config.vm.provision "shell", path: ""

Now that script will be run when vagrant up is called first time or vagrant provision is run.

comments powered by Disqus