Vagrant - Basic Setup of Virtual Development Environment

February 19, 2015 | By
| 1 Reply More

Vagrant is free and open source software for creating and configuring virtual development environments. Project was started in January 2010 by Mitchell Hashimot as a side-project, but in November 2012 Mitchell formed HashiCorp and started working on Vagrant full time.

So why would you want to use Vagrant? Vagrant will help you lower development environment setup time, and share those environments with the exact settings as on your own machine. It can run on Linux, OS X, and Windows. To use Vagrant, you will need a virtualization software such as VirtualBox, KVM, VMware, AWS, etc. We will use Linux and VirtualBox.

We assume that you have installed and configured VirtualBox. To get Vagrant, you can use your package manager, or if your distribution does not have vagrant in its repositories, you can download directly from the website.

Each machine you initialize with Vagrant will use the current directory for its files. Because of that, we will create a new directory for our machine. I personally have a directory named "vagrant", and then I have subdirectories for each machine.

# mkdir -p ~/vagrant/firstmachine && cd $_

We will now initialize a machine (Ubuntu Trusty Tahr x64) by using one of the "boxes" found in the official HashiCorp's Atlax box catalog - https://atlas.hashicorp.com/boxes/search

atlasboxes

"Box" in Vagrant world are packages for Vagrant environments.

# vagrant init ubuntu/trusty64

When you initialize a box for the first time, it will take some time because the box must be downloaded first. Next time you want to initialize that same box, Vagrant will use the already downloaded box, and it will create an instance of it (so that you always have a untouched version of that box).

You should see the following message at the end:

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

We will deal with this "Vagrantfile" later, but now let's run "vagrant up":

# vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: firstmachine_default_1419157392843_90130
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if its present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => /home/predmijat/vagrant/firstmachine

As you can see in the output, Vagrant already set up a SSH for us! Let's check it:

# vagrant ssh

Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-43-generic x86_64)

* Documentation: https://help.ubuntu.com/

System information as of Sun Dec 21 19:04:31 UTC 2014

System load: 0.46 Processes: 80
Usage of /: 2.8% of 39.34GB Users logged in: 0
Memory usage: 25% IP address for eth0: 10.0.2.15
Swap usage: 0%

Graph this data and manage this system at:
https://landscape.canonical.com/

Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

We are now inside our newly create machine! Easy, right?

vagrant@vagrant-ubuntu-trusty-64:~$ uptime
10:27:37 up 0 min, 1 user, load average: 0.27, 0.06, 0.02

Let's exit and destroy this machine, and inspect the "Vagrantfile":

vagrant@vagrant-ubuntu-trusty-64:~$ exit

# vagrant destroy

default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...

Open up a Vagrantfile in your text editor of choice, and browse through it - it has lots of comments which can help you understand what each configuration option means. We will go through some of them.

First configuration options tells you which configuration version is being used

Vagrant.configure(2) do |config|

In our case, configuration version "2" is being used. "2" represents the configuration for versions 1.1 up to 2.0.x.

Next configuration option will tell Vagrant which box to use. We initialized this machine with "init ubuntu/trusty64", so we will see that box already selected:

config.vm.box = "ubuntu/trusty64"

These two configuration options are the only ones which are uncommented by default. We will add one more:

config.vm.provision :shell, path: "bootstrap.sh"

This configuration option will tell Vagrant to use the "shell provisioner" to setup this machine, with the bootstrap.sh. What is bootstrap.sh you might ask - we will create it now! Make sure you are in the ~/vagrant/firstmachine directory by typing:

# pwd
~/vagrant/firstmachine

Create the bootstrap.sh with the following content:

# vi bootstrap.sh

#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
rm -rf /var/www
ln -fs /vagrant /var/www
fi

Save the file, exit your text editor, and run:

# vagrant up

Vagrant will now start your machine and execute the bootstrap.sh. This will install apache2 package, remove the /var/www directory, and then symlink /vagrant to the /var/www. What is /vagrant? Let's SSH into our machine and check out!

# vagrant ssh

vagrant@vagrant-ubuntu-trusty-64:~$ df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 40G 1.2G 37G 3% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 241M 12K 241M 1% /dev
tmpfs 49M 336K 49M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 245M 0 245M 0% /run/shm
none 100M 0 100M 0% /run/user
vagrant 219G 141G 79G 65% /vagrant

/vagrant is the default synced directory. This means that ~/vagrant/firstmachine on our host machine is mounted at /vagrant in our guest! You can check this by simply typing:

vagrant@vagrant-ubuntu-trusty-64:~$ ls -al /vagrant/
total 20
drwxr-xr-x 1 vagrant vagrant 4096 Dec 21 16:30 .
drwxr-xr-x 23 root root 4096 Dec 21 16:31 ..
-rw-r--r-- 1 vagrant vagrant 137 Dec 21 16:30 bootstrap.sh
drwxr-xr-x 1 vagrant vagrant 4096 Dec 21 10:23 .vagrant
-rw-r--r-- 1 vagrant vagrant 3048 Dec 21 16:30 Vagrantfile

See? All our files are there! Let's create a simple html page and test our web server.

vagrant@vagrant-ubuntu-trusty-64:~$ mkdir /vagrant/html
vagrant@vagrant-ubuntu-trusty-64:~$ vi /vagrant/html/index.html

 __        __             ______             __        __           
/  |      /  |           /      \           /  |      /  |          
$$ |      $$/  _______  /$$$$$$  | __    __ $$/   ____$$ |  ______  
$$ |      /  |/       \ $$ |  $$ |/  \  /  |/  | /    $$ | /      \ 
$$ |      $$ |$$$$$$$  |$$ |  $$ |$$  \/$$/ $$ |/$$$$$$$ |/$$$$$$  |
$$ |      $$ |$$ |  $$ |$$ |  $$ | $$  $$<  $$ |$$ |  $$ |$$    $$ |
$$ |_____ $$ |$$ |  $$ |$$ \__$$ | /$$$$  \ $$ |$$ \__$$ |$$$$$$$$/ 
$$       |$$ |$$ |  $$ |$$    $$/ /$$/ $$  |$$ |$$    $$ |$$       |
$$$$$$$$/ $$/ $$/   $$/  $$$$$$/  $$/   $$/ $$/  $$$$$$$/  $$$$$$$/

Save the file, exit the text editor, and then run:

vagrant@vagrant-ubuntu-trusty-64:/vagrant/html$ wget -qO- 127.0.0.1

You should get our nice html page! But how do you access this web server from outside this guest machine? The answer is - by adding another configuration option to out Vagrantfile, this time for port forwarding (remember that you need to forward the port in your router too):

config.vm.network :forwarded_port, host: 8080, guest: 80

Your Vagrantfile should now look like this (with all comments removed):

Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, host: 4567, guest: 80
end

You don't have to run "Vagrant destroy" each time you make a change to your Vagrantfile. You can run this command:

# vagrant reload --provision

This command will restart your virtual machine and instruct Vagrant to run the provisioners.

Now, if you point your web browser to your host machine ip, on port 8080, you should get our LinOxide html page, hosted in our guest machine.

linoxidehtml

This is just the tip of the iceberg of what Vagrant can offer you. It really is a huge subject, and it is impossible to cover everything without writing a whole book. We hope that what you saw here gets you interested to continue the research on your own - there are a ton of articles/books on the web!

Filed Under : OPENSOURCE

Free Linux Ebook to Download

Comments (1)

Trackback URL | Comments RSS Feed

  1. vicdeveloper says:

    Great. Thanks for share.

    :D

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.