Tuesday 20 January 2015

Vagrant up and running on ubuntu trusty

What is vagrant?

Vagrant is a software for creating and configuring complete development environment. The environment is sandboxed in a virtual machine. The virtual machine is create by a virtualization software such as VirtualBox, VMware.
Vagrant works as a wrapper around a virtualization software and around configuration management software such as Ansible, Chef, Salt or Puppet.

Why and when use vagrant?

In software development process it's a common scenario that some software works on one developers machine and simply does not work in some other programmers machine for some dependency or some other issue. Vagrant is the answer here. It will create an identical development environment for everyone who intend to run/develop the software. Also running provisioning software like puppet, chef or ansible is a nice feature to have.

Installation

Vagrant needs virtualbox. Virtualbox is a virtualization software by oracle and its free. It can be downloaded from here. After downloading .deb file for specific ubuntu version, it can be installed by this command in the shell.

$ sudo dpkg -i <location/of/the/file>

Next step is installing vagrant. .deb file of it can be downloaded from here. Installation process is same as the virtualbox. Once vagrant get installed it will seem like noting has happened but we get some command line options for vagrant.


Up and running

I am going to create a virtual environment of a ubuntu trusty 32 bit system in my ubuntu trusty computer. Reason of choosing 32 bit system is, it takes less RAM than its 64 bit counterpart in the host machine. The host machine is my personal computer and the guest machine is the virtual machine I am going to create.

Create a directory trusty32 and cd to that directory from terminal. Directory name can be anything.

$ mkdir trusty32
$ cd trusty32

Now we add vagrant box. A box is a base image to quickly clone a virtual machine. The easiest way to download boxes is downloading it from HashiCorp's Atlas using this command,

$ vagrant box add ubuntu/trusty32

Another way is to add a box from vagrantbox.es site. In this case we follow the site's instruction and add the box like this,

$ vagrant box add trusty32 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-i386-vagrant-disk1.box

vagrant box add command will download the specified box. This will take some time. Once downloaded it can be reused in multiple projects.

The next command is,

$ vagrant init trusty32

The command above will place a Vagrantfile in trusty32 directory. Vagrantfile describes type of machine required for a project, and how to configure and provision these machines. For now it will be not changed.
To boot up our virtual machine,

$ vagrant up

Above command creates and configures guest machines according to our Vagrantfile. To check if a guest machine is running,

$ vagrant status

We can ssh to the machine and gain access to the guest machine shell,

$ vagrant ssh

To shut down a running machine we have to exit from the ssh session  and initiate the command,

$ vagrant halt

If we want to destroy the virtual machine and it's resource,

$ vagrant destroy

No comments :

Post a Comment