← Back to the notebook

· 4 min read

Introduction to Vagrant, the best way to set up a web project. Part I

An introduction to Vagrant for building consistent, shareable web development environments with virtual machines.

#vagrant#virtualizacion#devops

Episode rescued from the SeViR Notes archive and translated from Spanish. Originally published on August 17, 2015.

After a couple of years of maturing, Vagrant has become one of the most widely used tools among web developers, sysadmins and any IT professional who wants to test a server environment, whether Linux or Windows (though mostly Linux).

For those who haven’t come across it, Vagrant is a tool that lets you spin up a preconfigured virtual machine (for VirtualBox, VirtualPC or VMware) with remote access in just a few minutes, and share it easily.

What’s it for? Well, imagine we’re building a website in PHP, so we’ll need a web server with PHP, and most likely MySQL, on our own computer. And that’s the best-case scenario, because we could just as easily run into environments with Django, Ruby on Rails, NodeJS and any of their countless web frameworks. Installing all of this on your work machine is not only tedious, it ends up being unbearable because of the differences between Windows, Linux and OS X, when in the end everything has to run on a Unix server at some hosting company.

Until very recently, most of us turned to some kind of LAMP stack (if we’re talking Apache+PHP+MySQL), a WAMP (on Windows) or perhaps a MAMP (on OS X). That was easy enough, but as soon as your project started demanding more, in the form of new PHP modules, indexers like ElasticSearch, Sphinx, etc., installing and compiling those modules and their dependencies got more and more complicated.

Add to this lovely picture the fact that, of the colleagues working with us on the same project, one has a Mac, another has Windows and another maybe Linux. Helping a teammate by explaining how you set up your environment was impossible, since everyone had done it on their own operating system.

Vagrant gets rid of all these problems, and on top of that lets us build a consistent environment for the whole team, one that can be a clone of what we’ll have on our server later on when we release the project to production. We can hand over our Vagrant machine’s configuration along with a setup process that installs the entire environment we need.

The first thing to know is that Vagrant can be used in many ways. The first is to install a base system, for example Ubuntu 14.04, by typing:

mkdir ubuntu
cd ubuntu
vagrant init precise32

This creates a Vagrantfile, which we can edit with any text editor. From this file we can change the amount of RAM assigned, set up shared folders, etc.

Once it’s configured, we can run:

vagrant up

All vagrant commands must be run from the folder containing the Vagrantfile; otherwise Vagrant won’t know which virtual machine it’s working with. This downloads Ubuntu and creates a virtual machine with that OS already installed. We can log into the virtual machine over SSH from the command line, although if we’ve never used Linux before, it might be a bit hard to swallow.

vagrant ssh

There’s a slight difference here: if we’re on Windows, there’s no SSH client out of the box, so we’ll need to install either Cygwin, Git for Windows or, better still, Babun (a tweaked, improved version of Cygwin for Windows that I’ll talk about soon).

Once Babun is installed on Windows, we can work as usual.

The interesting part is being able to build a virtual machine that frees us from working on the command line all the time, and lets us configure the server easily from a web interface.

We can configure this base system and then export it as a .box file to share with the rest of the team. Once the virtual machine has been exported, we can also share it with the community, for free, on the website of HashiCorp, the company behind Vagrant.

I’ve shared a Vagrant machine that has everything you need to work with Apache, Nginx, PHP 5.5, MySQL (MariaDB 5.5), NodeJS, Composer and Bower.

You can download it at https://github.com/sevir/vagrant-webserver. Once downloaded, go into the folder and run vagrant up.

Besides coming with most of the system preinstalled, this virtual machine updates and installs certain components when a new virtual machine is created. This is called provisioning. Vagrant can provision a server in several ways: the simplest is to run shell scripts with whatever commands we want; another is to use server provisioning software such as Puppet, Chef or Ansible, which we’ll talk about soon.

Update: This morning at work someone told me about a Vagrant plugin you can install to always keep the VBox Guest Additions up to date; these add better VirtualBox support inside the virtual machine (in short, they make it faster). Just run this before vagrant up:

vagrant plugin install vagrant-vbguest