039 Vagrant

20 Apr 2013

Vagrant creates reproducible, portable development environments that can be packaged and passed on to other developers to create an exact copy. In this episode, we will learn how to installed virtual machines with packages as well as view them in the host machine. We will run Vagrant with a simple html page and a NodeJS "hello world" project.

Download video: mp4

Sample code: Github

Version: 1.2.1

Similar episodes: 014 Local web servers

Background on Vagrant

  1. Main website
  2. Github
  3. Documentation

Things to learn with Vagrant

1. install

  1. download
  2. check version in the command line:

    $ vagrant --version
    Vagrant version 1.2.0
    $ vagrant --help
    $ vagrant <command> -h
    
  3. start a vagrant config file in an empty project folder. this will create Vagrantfile

    $ mkdir project_name
    $ cd project_name
    $ vagrant init
    
  4. edit Vagrantfile to add the boxname

    Vagrant.configure("2") do |config|
      config.vm.box = "boxname"
    end
    
  5. add a base image or box to quickly clone a virtual machine. this will create a folder .vagrant with an ubuntu box

    $ vagrant box add boxname http://files.vagrantup.com/precise32.box
    $ vagrant box list
    $ vagrant box remove <name> <provider>
    
  6. boot up the guest machine

    $ vagrant up
    
  7. ssh into the machine

    $ vagrant ssh
    
  8. exit ssh

    $ exit
    
  9. 3 ways of teardown:

    1. save current running state of machine and stop it

      vagrant suspend
      
    2. cleanly shut down machine

      vagrant halt
      
    3. nothing left - disk space and RAM is reclaimed by host machine

      vagrant destroy
      
  10. remove box

    vagrant box remove <boxname> <provider>
    

2. synced files

  1. in the guest virtual machine

    $ vagrant ssh
    $ ls /vagrant
    Vagrantfile
    
  2. create a file on local machine and view it in the guest virtual machine

    $ touch index.html
    $ vagrant ssh
    $ ls /vagrant
    index.html Vagrantfile
    
  3. create a file in the guest virtual machine and view it in the local machine

    $ vagrant ssh
    $ touch /vagrant/readme.md
    $ exit
    $ ls
    index.html readme.md Vagrantfile
    

3. installing softwares

  1. in the same folder as the file Vagrantfile, create a new file bootstrap.sh with the following code:

    #!/usr/bin/env bash
    apt-get update
    apt-get install -y apache2
    rm -rf /var/www
    ln -fs /vagrant /var/www
    
  2. configure Vagrant to run the script for installing and setup port forwarding in host machine:

    Vagrant.configure("2") do |config|
      config.vm.box = "boxname"
      config.vm.provision :shell, :path => "bootstrap.sh"
      config.vm.network :forwarded_port, host: 4567, guest: 80
    end
    
  3. run the installations

    • if the virtual machine is not running
    $ vagrant up
    
    • if the virtual machine is already running
    $ vagrant reload
    

4. package box for reusing

  1. create an inline script inside file Vagrantfile

    $script = <<SCRIPT
    apt-get update
    apt-get install -y apache2
    rm -rf /var/www
    ln -fs /vagrant /var/www
    SCRIPT
    
    Vagrant.configure("2") do |config|
      ...
      config.vm.provision :shell, :inline => $script
      ...
    end
    
  2. package the currently running environment into a re-usable box called package.box

    $ vagrant up
    $ vagrant package --output package.box --vagrantfile Vagrantfile
    

5. reusing box

  1. create another brand new project folder that has the same package.box

    $ vagrant init # amend the box name accordingly
    $ vagrant box add <new-boxname> <package.box>
    $ vagrant up
    

6. nodejs with ubuntu box

  1. create a new Vagrantfile in a new folder

    $script = <<SCRIPT
    apt-get update
    echo "Y" | apt-get install nodejs
    node --version
    ln -fs /vagrant /home/vagrant/<new-folder-name>
    SCRIPT
    
    Vagrant.configure("2") do |config|
      config.vm.box = "base"
      config.vm.box_url = "http://files.vagrantup.com/precise32.box"
      config.vm.provision :shell, :inline => $script
      config.vm.network :forwarded_port, host: 3000, guest: 3000
    end
    
  2. create a new index.js hello world for nodejs

    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World with Vagrant!\n');
    }).listen(3000);
    console.log('Server running at http://127.0.0.1:3000/');
    
  3. start the guest machine:

    $ vagrant up
    
  4. start the node server in the guest machine and visit 127.0.0.1:3000 in the browser

    $ vagrant ssh
    $ cd <folder-name>
    $ node index.js
    

More Resources on Vagrant

  1. Vagrant generator
  2. Vagrant boxes
  3. Vagrant - what, why and how
  4. Introduction to Vagrant

Related Build Podcast episodes

  1. Virtual box
  2. SSH
  3. Apache

Build Link of this episode

  1. codepen.io