Vagrant

Introducing Vagrant

8 September, 2014

I’d like to quickly introduce you to the use of Vagrant software in our local computer. But first let’s start by explaining what is Vagrant. As described in its website: Create and configure lightweight, reproducible, and portable development environments. It is a software that allows us to create portable and shareable virtual machines (VM) with the environment we need. Each environment is composed of various packages and configurations creating our development stack:

  • Ubuntu, Apache, PHP, MySQL, …
  • CentOS, NGINX, Ruby on Rails, MondoDB, …
  • …and others.

Vagrant allows us to have one of this environments (Boxes) in our local machine. This environment can be copied from the staging and production servers of our project so changes we made will be following the real ones. As it is a fully working VM in our personal computer, it can too follow our sysadmin/operations decisions, so changes made in staging or production server will be reproduced in our local VM. It can be considered a wrapper around virtualization software such as VirtualBox and configuration management software such as Chef, Salt and Puppet. For more information please consult Vagrant’s website or do a Google search. There is plenty information and tutorials available.

Let’s continue by installing the required software in our personal computer:

  1. MacOSX, Windows or Linux
  2. VirtualBox
  3. VirtualBox Extension Pack
  4. Vagrant

I am assuming we already have a Box (Ubuntu) ready to use but we can create our own following this documentation. Working in a development team, this box could be provided by our sysadmin/operations team. For more Boxes here.

Open your terminal or command line application and go to your desired working directory. Then create your development folder and enter it. Place in here the Box image:

$HOME/Development/packaged.box

Create a new folder called “Box” and enter it (cd):

$HOME/Development/Box/

Lets install our Box into VirtualBox:

$ vagrant box add {title} {url} ## url can be a HTTP, HTTPS, local file or from the public Box site.
$ vagrant init {title}
$ vagrant up

Example:

$ vagrant box add myFirstBox ../packaged.box
$ vagrant init myFirstBox
$ vagrant up

This last command starts our VM. In this working folder we should see a new file plus a hidden folder:

drwxr-xr-x  .vagrant
-rw-r--r--  Vagrantfile

Vagrant command uses those files for the folder/environment where it is called plus the VirtualBox image. I will later explain the importance of “Vagrantfile”.

I am assuming we are using a Ubuntu Box, so to enter it just type in:

$ vagrant ssh

Now we should we inside our VM Box ready to use. We should see something like:

Welcome to Ubuntu 13.10 (GNU/Linux 3.11.0-12-generic i686)
 * Documentation:  https://help.ubuntu.com/
Last login: Sat Sep  6 11:02:42 2014 from 10.0.2.2
vagrant@vagrant:~$

To leave the Box just type in “exit”.

From here we could install (apt-get …) and configure (/etc …) any Ubuntu parameters but I am going to give you a few recommendations to automate the Box flow ready for our sysadmin/operations team.

We can edit our Vagrantfile and uncomment/add these lines:

config.vm.provision :shell, :path => "vagrant-bootstrap.sh" ## provisioning file with instructions

Next create/edit the “vagrant-bootstrap.sh” with:

#!/usr/bin/env bash
apt-get update
apt-get -y upgrade

apt-get install -y puppet
apt-get install -y git subversion curl vim screen 

Once these two files are edited, we can call our Box with next command:

$ vagrant reload --provision

You should see the Box activating/reloading and processing the instructions from the “vagrant-bootstrap.sh”.

Once steps are processed you can continue using and configuring this Box. See that I added a line to install Puppet, so we can set up this Box to point to the Puppet master of our company and get changes, updates, … from the sysadmin/operations team.

Soon I will post the Vagrant LAMP configuration steps.


Cheat sheet: Simple bootstrap configuration (Download)

## 1. vagrant: add image
$ vagrant box add {title} {url}  ## vagrant box add myFirstBox ../packaged.box
$ vagrant init {title}  ## vagrant init myFirstBox
$ vagrant up

## 2. vagrant: connect
$ vagrant ssh

## 3. open Vagrantfile and add…
config.vm.provision :shell, :path => "vagrant-bootstrap.sh"

## 4. open/create vagrant-bootstrap.sh and add…
#!/usr/bin/env bash
apt-get update
apt-get -y upgrade

apt-get install -y puppet
apt-get install -y git subversion curl vim screen 

## 5. vagrant: provision
$ vagrant reload --provision

## 6. vagrant: remove
$ vagrant destroy

    Tags: , , , ,

    Leave a Comment