Creating Virtual Environments With Vagrant

Vagrant is a command line tool whose main objective is the creation of virtual machines in a

Vagrant is a command line tool whose main objective is the creation of virtual machines in a fast, flexible and reproducible manner. It is developed in Ruby and is open source and multiplatform. Vagrant allows us to easily create an environment with the settings you need (OS, libraries, applications, etc.)

General Applications:

  • Generate test environments to perform deploys
  • Unify development environments of the members of a team
  • Creating environments with different configurations to test the performance of our application.
  • Create a version of a web application running in a virtual machine and is served in a port on my local machine
How to install?

Download the appropriate version for your operating system and follow the normal installation process.

In addition, we install a virtualization tool. We use VirtualBox, since it is free and available in all major platforms.


We´re going to show an example of how we use Vagrant to create a test environment where installing a web application served from apache. In our case we’re working on Ubuntu 12.04, using Vagrant in version 1.5.4 and 4.3.18 VirtualBox.


Creating our configuration

We will create a folder where we will store the settings required to initiate our virtual machine. This will be the root directory of your configuration in which we execute commands from Vagrant.

The initial configuration of the virtual machine that will create will be defined in the Vagrantfile file. To create a sample Vagrantfile, with comments on the configuration that we can define and execute the command:

After running this command we must define the image base (base box) which will be used to clone our virtual machines. We can find images with different configurations in the following repositories:

If we find one with the settings you need (mainly the OS, the other applications can be installed later) we must define it within the Vagrantfile in the line:


In this case we select a 64-bit Debian Server 7.5 , which is found in the official repository of Vagrant (that´s why we identify it by its name alone) . If we select an image that is in another repository, we must specify the URL from where to download it from and the name we will identify it as:

Once we define this setting we can start our virtual machine with the command:

This command will first check if our system has discharged a version of the image that we define as a base. If not, it will download it and place it in a directory run by Vagrant (in Ubuntu ~ / .vagrant.d / boxes ). We can see the list of images downloaded on our system with the command:

After downloading the image, Vagrant will clone it to create our virtual machine. Then finally, it will start the virtual machine. If the command is executed successfully, we can connect to it by ssh, running the command:

Once we connected we can make the changes necessary to install our application. Note that by default, the root directory where we start our VM has been shared within the same in the / vagrant (this facilitates the task of copying files from our computer to the virtual machine we created). These directories are synchronized, so that any changes to the files or folders of the same type will be reflected on both machines.


As we saw, we can install the software we need to run our application by logging in through ssh and installing “by hand.” A better way is to use the Vagrant providers that allow us to automate the installation and configuration process of the necessary applications. The simplest way is to provide a shell script that will launch once it executes the vagrant up.

For example, to update the system and install apache, we can create the following bash script in the root folder of the project:

Configure their execution within Vagrantfile with the following line:

If we change this configuration on a machine that is running, we can restart the machine taking this configuration with:

Besides shell we can use some other, more sophisticated tools such as: Ansible, Chef or Puppet.


So far, we have a virtual machine with which we can interact, share files and install the applications you need. The last thing we need to check if our web application works correctly is to be able to access it from our host machine.
The simplest way to do this is via Port Forwarding, to connect and relay the data to be sent from a port of our host machine to a port of our virtual machine.
If we continue to work with the newly created machine, we see that Apache is up and running by default on port 80. We can connect this port to port 8080 of our host machine with the following configuration in the Vagranfile:

Then, to take this new setting use vagrant up (or vagrant reload if the vm vagrant is already picked up). Then, we can access our host machine through http : // localhost : 8080 and we will see the sample page apache, which is being served from the vm .

The complete configuration of this example machine would be:

Turning it off

Finally, we can turn our virtual machine, keeping the changes that have been made in it, with the command: vagrant halt. To start it up again we run vagrant up.
In case you want to stop our VM, saving the current state of implementation so that it can be summarized later and continue from that exact spot, use the commands: vagrant suspend y vagrant resume (respectively)

Finally, if we know that we will no longer work with this machine we can remove it with the command: vagrant destroy. This stops the virtual machine and destroy all the changes we have made, but not its configuration file (Vagrantfile), allowing us to later execute a vagrant up to start a new VM from a completely clean image.

We can check the state of our virtual machine with the command vagrant status.

Múltiples VMs

In the context of a web application, it is common for one to be integrated by different processes, deployed on different servers and communicate in itself, within a network. Vagrant allows you to easily manage and define a configuration of this type. We discuss below an example with two virtual machines, one used to run the application and one for the server database.

The config.vm.define option allows us to define a setting within the general configuration. This command takes as a parameter the name of a variable ( in this case ” web ” and ” db” ) . From these variables we can define the particular configuration that applies to each machine.

The directives that are outside of the particular configuration of each machine are shared in both instances. In this case both use the base “puphpet/debian75-x64″ image.

In addition to the configuration that we had set for the web server, we add both settings to create a private network between the host machine and the two virtual machines so they can communicate. To make a demand of the Apache server that is running web in this instance, we can access the url

How Do We Use It?

In the FOP-ARSAT project, we had to develop and deploy a composite of different processes that would communicate with each other and could be installed and run on multiple servers with different application configurations. Each of these processes require different environments and applications to run. To test the proper functioning of the application, in addition to the unit testing and integration testing, we needed a way to test the proper installation and communication processes.

Vagrant allowed us to quickly and easily create, similar environments to those used by the customer for installation. These environments would be very useful for testing and debugging scripts in the installation and upgrade of our application.

Another requirement of the project was to provide the customer with a virtual machine in which he could run the application, which we can connect to, so as to be able to run it locally without internet access. We use Vagrant to unify and automate the process of creating these virtual machines.

Finally, because the client does not have a Unix environment to easily work on application development, we provided a virtual machine with the tools and skills necessary to perform this task applications. The process of creating these virtual machines was also implemented using Vagrant.

Useful Links
Share this articleShare on LinkedInTweet about this on TwitterShare on FacebookShare on Google+Email this to someone
Go Back