Starting with Vagrant On Linux

Vagrant is a tool that creates and configures virtual development environments. It can be seen as a higher-level wrapper around virtualization software such as VirtualBox, VMware, KVM and Linux Containers and around configuration management software such as Ansible, Chef, Salt, and Puppet.

It was originally tied to VirtualBox, but version 1.1 added support for VMWare, Amazon EC2 and KVM. I must say that since version 1.6, Vagrant supports Docker containers.

If you’re a developer, Vagrant will isolate dependencies and their configuration within a single environment, without sacrificing any of the tools you’re used to working with (editors, browsers, debuggers, etc.). Once you or someone else creates a single Vagrantfile, you just need to vagrant up and everything is installed and configured for you to work.

So if you are working with Linux, Mac OS X or Windows, all your team members are running code in the same environment.

How To Install Vagrant

You can get Vagrant directly from its download page. After you download the package, open the Terminal and go to your package directory, in my case in Downloads folder:

$ cd Downloads

Then run the Vagrant package you downloaded from Vagrant’s download page. The name of package I just download is vagrant_1.7.4_x86_64.deb. Now let’s run it in the Terminal:

$ sudo dpkg -i vagrant_1.7.4_x86_64.deb

In this article I will use Vagrant with VirtualBox.

The first step in configuring any Vagrant project is to create a Vagrantfile. Two purposes of the Vagrantfile are:

  1. 1. Mark the root directory of the project.
  2. 2. Describe the kind of machine and resources you need to run your project.

Let’s create a folder test_Vagrant and place there a Vagrantfile.


$ mkdir vagrant_getting_started
$ cd vagrant_getting_started
$ vagrant init

Boxes are very important to Vagrant. Instead of building a virtual machine from scratch, Vagrant uses a base image to quickly clone a virtual machine.
To add a box in Vagrant, you must type in Terminal vagrant box add alongside with the name of the box.

For example to add Ubuntu box (32 bit), we run on Terminal the following command:

$ vagrant box add hashicorp/precise32

You can find more boxes here.

To use the box, open the Vagrantfile and change the contents to the following:


Vagrant.configure(“2”) do |config|
config.vm.box = “hashicorp/precise32”
end

Now, it’s time to boot your boot your first Vagrant environment. Now, run the following commmand:

$ vagrant up

You won’t actually see anything, though, since Vagrant runs the virtual machine without a UI. To prove that it is running, you can SSH into the machine:

$ vagrant ssh

To remove the traces of the virtual machine just run:

$ vagrant destroy

I hope this tutorial will give a general idea about Vagrant and how to start using this powerful tool in your Linux machines.