Simple Vagrant

Simple Vagrant

/>Intro to Vagrant
Because I quite often like to test different applications locally, I use virtualbox a lot. I have recently found that using Vagrant can simplify the deployment of setting up a local development environment and replaces it with almost a single config file where you can pick and choose what features you want your server to have. No longer are you stuck using XAMMP, configuring virtual environments manually, or working remotely in your editor via FTP. Vagrant is quick, easy, and helps you manage multiple development environments at one time.

Imagine you’re developing an app with a team of say 15 people. This app is crazy awesome! It uses NodeJS ,ExpressJS , AngularJS , MongoDB and a bunch of othe modules.

Ideally, you’re going to want all 15 people on the team working on this app to have as identical development environemts. Not all the developers on the team are going to have the expertise of Sysops or being a System Admin. Getting the development environments identically setup can be a huge undertaking. On top of that, some people use Mac while others Use Linux or Windows. Before you know it, developers will be throwing computers through walls exhausted from constantly configuring and configuring.

Vagrant manages all this for you so everyone can focus on coding rather than maintaining their development environement.

Install Necessary Dependencies


To Help find a ready box you can search on the Vagrant website. These are usually prebuild virtual machines for Vegrant that are build from basic distros.

For our example i have downloaded and installed Vegrant for debian on my Ubuntu machine on whcih i am running virtualbox. I will be getting the Vegrant box Ubuntu 14.04 which will be downloaded automatically by Vagrant. So in order to download it you run the following command:

vagrant init ubuntu/trusty64

To initalize the vm you run the following

vagrant up --provider virtualbox

I order to connect to the vagrant machine , which is essentially ssh strapped onto vegrant, run the following command:

vagrant ssh

Looks simple right,well thats the whole point.

A run down of the most common commands

Vagrant Commands

There’s a ton of commands you can use to talk to Vagrant. For a full list see the official docs here, but I’ll go over the more common ones.

vagrant up

vagrant up This is the command you just ran to boot the box up and provision the server based on the configuration file Vagrantfile. I use this for starting as well as resuming my environments.

vagrant suspend

vagrant suspend Use this command to “pause” your virtual environment. Make sure you do this before shutting down your computer to safely be able to restore the environment later.
Starting, Pausing, and Resuming: To start or restore the environment you can just run vagrant up. The state will be saved and everything should be where you left it off from including your database changes.

vagrant halt

will cleanly shutdown the virtual machine.Adding the --force ooption will sforce shutdown

vagrant destroy

vagrant destroy This permanently removes the virtual environment from your machine.

vagrant reload

vagrant reload and vagrant reload --provision If your environment suddenly stops working, it’s useful to reboot by running this command. If you add the --provision flag, it will reprovision the box as well. This is useful with removing or adding things to the server via Cookbooks or Puppet.

vagrant ssh

vagrant ssh Anything you edit on your computer in the public folder is automatically shared to the virtual environment without having to SSH in. However, if for whatever reason you need to connect to the virtual server, just run this command.

vagrant ssh-config

vagrant ssh-config This lets you see the detailed login credentials of how you could connect to the virtual environment.

vagrant box list

Will list the vegrant boxes that have been downloaded and are available on your directoty

vagrant status

This will tell you the state of the machines Vagrant is managing.

It is quite easy, especially once you get comfortable with Vagrant, to forget whether your Vagrant machine is running, suspended, not created, etc. This command tells you the state of the underlying guest machine.


Networking Basics

Modify the Vagrantfile and add the following line. This will cause port 8080 on the host machine to get forwarded to the virtual machine. "forwarded_port", guest: 80, host:8080
To enable run vegrant reload on the running machine


Provisioning and Provisioners

Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process. This is useful since boxes typically are not built perfectly for your use case. Of course, if you want to just use vagrant ssh and install the software by hand, that works. But by using the provisioning systems built-in to Vagrant, it automates the process so that it is repeatable.

Provisioners can be used for Chef and Puppet.

In order to use provision we create a bash script that we place in out vagrant directory. I will use an example where i will install apache and link the apacje /var/www directory to our local directory in the vagrant directory.

So inside our vegrant directory we create a file called "" and inside we add the following

#!/usr/bin/env bash
echo "Installing Apache"
apt-get update >/dev/null 2>&1
apt-get install -y apache2
rm -rf /var/www
ln -fs /vagrant /var/www

One more thing is t add a reference to this file inside the Vagrantfile that exists in the same directory. we can add this under the line we previously modified to forward port 8080. "forwarded_port", guest: 80, host: 8080
config.vm.provision "shell", path: ""

Then we just use vegrant up and we start our vm that will install and configure apache

During startup you will see :

==> default: Reading package lists...
==> default: Building dependency tree...
==> default: Reading state information...
==> default: The following extra packages will be installed:
==> default: apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3...



Using Chef and Vagrant together

I have written another tutorial on chef .This guide will provision for chef solo.destro

Firstly we can edit the vagrant file and add the following

config.vm.provision "chef_solo" do |chef|
 chef.add_recipe "vagrant_la" #you can add the path to your chef-repo here

what we are doing is that we are pointing to a chef recipe and telling vagrant to run that recipe through chef .

So we need to create the cookbook vagrant_la

If we have knife installed then we can do this by running

knife cookbook create vagrant_la

otherwise just create the file called default.rb /cookbooks/vagrant_la/recipes/default.rb and add that inside your vagrant folder.

Then inside the default.rd add the following text

execute "apt-get update"
package "apache2"
execute "rm -rf /var/www/html"
link "/var/www" do
 to "/vagrant"

Thejust run vegrant up and you will notice the chef installation will run and execute the apache installation and configuration.

There are thousand of ready cookbooks for chef and these cookbooks can all be used in conjunction with vagrant to deploy virtual machines.

Private Networks

Vagrant private networks allow you to access your guest machine by some address that is not publicly accessible from the global internet. In general, this means your machine gets an address in the private address space.


The easiest way to use a private network is to allow the IP to be assigned via DHCP.
Vagrant.configure("2") do |config| "private_network", type: "dhcp"

Static IP

You can also specify a static IP address for the machine. This lets you access the Vagrant managed machine using a static, known IP. The Vagrantfile for a static IP looks like this:
Vagrant.configure("2") do |config| "private_network", ip: ""

Disable Auto-Configuration

If you want to manually configure the network interface yourself, you can disable Vagrant's auto-configure feature by specifying auto_config:
Vagrant.configure("2") do |config| "private_network", ip: "",
    auto_config: false

Public Networks

Network identifier: public_network

Vagrant public networks are less private than private networks, and the exact meaning actually varies from provider to provider, hence the ambiguous definition.



The easiest way to use a public network is to allow the IP to be assigned via DHCP. In this case, defining a public network is trivially easy:
Vagrant.configure("2") do |config| "public_network"

Using the DHCP Assigned Default Route

Some cases require the DHCP assigned default route to be untouched. In these cases one may specify the use_dhcp_assigned_default_route option. As an example:
Vagrant.configure("2") do |config| "public_network",
    use_dhcp_assigned_default_route: true

Default Network Interface

If more than one network interface is available on the host machine, Vagrant will ask you to choose which interface the virtual machine should bridge to. A default interface can be specified by adding a :bridge clause to the network definition. "public_network", bridge: "en1: Wi-Fi (AirPort)"