Not everyone is familiar with Vagrant, therefore here is a 101 introduction into Vagrant.
Vagrant is an open source automation tool, initially developed by Mitchell Hashimoto in early 2010. Since the end of 2012 is Vagrant backed by a company called HashiCorp, which was formed by Mitchell to better support the growing demand.
Vagrant allows the user to create and share development environments, which massively reduces the time and increases quality for deployments.
Vagrant is available for Windows, Mac OS X and Linux and does support automating Linux and Windows based VMs. Since a while does Vagrant also support automating Docker environments. With this great support for heterogeneous host systems a developer can create an environment once and share it with a greater community.
To better understand Vagrant I will explain the most important parts of Vagrant.
Vagrant itself does not include any hypervisor but does have out of the box a provider to automate VirtualBox and Microsoft’s Hyper-V. There is also a provider for Docker container technology. Great about using VirtualBox as your hypervisor is that it also supports multiple operating systems and is Open Source, which makes it to the default provider for Vagrant.
To fund the development of Vagrant does HashiCorp also offer a commercial provider for VMware Workstation and Fusion.
Beside these “official” providers does the open source community extend Vagrant by numerous providers. (AWS, OpenStack, VMware AppCatalyst, …)
Vagrant does not install any operating systems. It uses boxes, you could also say templates, to deploy VMs. There are boxes available which support Virtualbox, VMware, etc. These boxes are most of the time a minimal installation of an operating system with VM tools and necessary modifications. While provisioning a Vagrant environment, the required packages and software will be installed on the fly. Therefore most of the time an online connection is necessary.
Boxes are either distributed via simple download links or via HashiCorp’s Atlas. There is already a huge amount of boxes available which makes it most of the time unnecessary to create your own. You can also create your own boxes with a tool called packer if you can’t find a suitable one publicly. Boxes are cached locally therefore for consecutive deployments you don’t have to download it again.
Knowing about boxes and providers is nice but this only provisions you a minimal operating systems using the provider of your choice. We want more!
Provisioners allows you to customize your box using different approaches like: bash and powershell scripts, puppet, chef and more.
These techniques enable you to create your individual development environment fully automatic, reproducible and distributable.
A Vagrant environment is described by a simple text file called “Vagrantfile” (no file extension). This file is written in Ruby, a programming language, though you don’t need extensive Ruby knowledge to work with Vagrant. Vagrant itself is also written in Ruby. In this file you can specify which box to download, VM modifications, scripts to run and much more.
The following is an example for a simple Vagrantfile that does provision a Ubuntu box using Virtualbox, install Apache and make the webserver available locally on port 8080:
Vagrant.configure(2) do |config|
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "hashicorp/precise32"
# 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", guest: 80, host: 8080
# 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"
# Enable provisioning with a shell script.
config.vm.provision "shell", inline: "sudo apt-get install -y apache2"
Putting all together
Now that you are aware of the concept and the main components of Vagrant, let’s take the above example and deploy it. You need to have installed Vagrant and Virtualbox.
Create a file called “Vagrantfile” with the above content and save it into and empty directory . Finally issue vagrant up in a shell in this folder to start the deployment.
The output will look similar to this:
After a few minutes, depending of your internet bandwidth, you will be able to test this environment by browsing to http://127.0.0.1:8080 .
With ONE command, “vagrant up”, you get a consistent development environment deployed to your host, be it Windows, Linux or Mac without requiring knowledge about Apache, Linux or more.
Using vagrant destroy will clean up the environment.
This is the power of Vagrant and one reason why it is so popular and leveraged for many many projects.