How many of you have ever heard about Vagrant ? If you have ever used virtual machines, probably you used one of the virtual systems available on the market, something like VirtualBox or VMware. This is the starting point for understanding what Vagrant is and what we can do with it.
Vagrant is a system that builds and maintains machines. A virtual machine is an environment that emulates a computer or specific hardware resources like RAM, CPU Hard Disk and network interfaces. Vagrant automatically maintains configurations and resources through the virtuals system called providers.
Vagrant includes the following components:
VirtualBox is the most used provider by developers. It manages virtual environments with CPU, RAM and network interfaces and furthermore it allows to install an operative system (OS) on the created machines (VM).
Installing an OS into a VM needs the same slowly and tricky process as a normal installation on a real machine (Pc o server). For that reason, Vagrant provides a “box”, a pre-configured machine with a built-in operative system that we can start to work with.
The customization process of a vagrant machine is called provisioning. It allows to automatically configure the vagrant machine and make it portable thought a single file.
Why using Vagrant
In a work environment, it’s difficult to work with different projects, especially when those projects can involve multiple teams.
Moreover, in web development, we can face three problems:
- Create similar development and testing environments as the live one, in order to simplify the deployment processes.
- All developers should work with the same development environment.
- Each environment should be configured despite its machine where it has been installed for portability purposes.
Vagrant solves this issues with a configuration file and commands. It’s compatible with Windows, Mac OS or Linux and it requires that the provider is installed on the same machine.
How to use Vagrant
To install, Vagrant go to https://www.vagrantup.com/ and download the installation package. After installing it, it’s possible to check if it’ installed by using the vagrant command inside a terminal shell (Mac or Linux) or the command prompt (Windows).
With vagrant installed, we need to create the configuration file the vagrant. That file is called Vagrantfile (without any extensions) and insert it into the main folder of our project. This file is used by Vagrant to create a virtual machine using the box specified in that file.
Vagrant.configure(2) do |config|
# Set base box and provisioning
config.vm.box = "ubuntu/trusty64"
# Provider resources
config.vm.provider "virtualbox" do |v|
v.memory = 512
v.cpus = 4
config.vm.network :private_network, ip: '192.168.99.12'
# Shared folder
config.vm.synced_folder '.', '/vagrant'
In the Vagrantfile we specify the box with OS included on it (ubuntu/trusty64), RAM and CPUs and the IP address for accessing to it via the guest machine (our physical computer).
You can find all the boxes can be used with Vagrant at the official website: https://atlas.hashicorp.com/boxes
The last but not least is the synced_folder, the shared folder the ou computer shares with the box. This folder is the most useful thing because it will contain all the project file.
What I can do with it
A typical example of using vagrant is making the VM (Virtual Machine) like a web server and put the source code in the shared folder. This allows us to share, through a repository, the folder with project files and the Vagrantfile included. Then our colleagues can use the same environment that can be similar to our production server.
Vagrant can be used for multiple purposes even if we use it as data processing system rather than a web server. The limit, in this case, is our imagination.
In the next posts, we can see how Vagrant can be used for making websites.
Interested? Feel free to comment this post and let me know what you think about it.