dribbblefacebookgithubgooglepluslinkedinrsstwitter

Creating Vagrant Boxes using Vagrant

Posted on by Devin Clark

Vagrant is a huge pain in the ass (sometimes). Creating Vagrant boxes is more so (or so I thought).

A little bit of backstory. I started out using a tool called Packer to build machine images. Packer has a post processor for Vagrant to export a Vagrant box. The most frustrating part for me was having to start from nothing, instead of starting from a base image.

So then I came across a package command in the Vagrant documentation. This command allows you to export a running box. I got the wild idea to create a Vagrantfile to build my Vagrant box. Using an existing base and shell provisioning gave me exactly what I needed.

First, we set the the box to be "ubuntu/trusty". You can set this to whatever box you want. This alone eliminates most of the work Packer had to do.

config.vm.box = "ubuntu/trusty"

Next, we provision the box with whatever we want to install. In my case, it is IO (Node), and a few global node modules. There are about a dozen different provisioners available. We are going to use inline shell because it is the simplest.

config.vm.provision "shell", privileged: true, run: "once", inline: <<-SHELL
  # Install Node dependences
  sudo apt-get -qy install build-essential git wget curl
  sudo apt-get install -y python-software-properties python g++ make

  # Install Node
  curl -sL https://deb.nodesource.com/setup_iojs_2.x | sudo bash -
  sudo apt-get install -y iojs

  # Remove node install dependencies
  sudo apt-get purge -y wget apt-transport-https software-properties-common python python-software-properties g++ make
  sudo apt-get autoremove -y
  sudo apt-get clean all

  # Install node modules
  sudo npm install -g pm2 grunt-cli

SHELL

Now, we can create the machine by running vagrant up in the same directory as the Vagrantfile. If you aren't super familiar with Vagrant, I recommend you go through their getting started tutorials. After this finishes, we can run vagrant package --output whatevs.box. This command will package the machine we just created and provisioned into a Vagrant box.

You can upload that to a server, fax it, or whatever so the rest of you team can use it. We are currently storing these boxes in a Vagrant repository on our Artifactory server.

If you are on Windows and are having an issue relating to SSH timeouts, check out this article on enabling virtualization in BIOS.

Here is my full Vagrantfile.

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty"

  config.vm.provision "shell", privileged: true, run: "once", inline: <<-SHELL
    sudo apt-get -qy install build-essential git wget curl
    sudo apt-get install -y python-software-properties python g++ make

    curl -sL https://deb.nodesource.com/setup_iojs_2.x | sudo bash -
    sudo apt-get install -y iojs

    sudo apt-get purge -y wget apt-transport-https software-properties-common python python-software-properties g++ make
    sudo apt-get autoremove -y
    sudo apt-get clean all

    sudo npm install -g pm2 grunt-cli

  SHELL
end