Creating a Custom Box from Scratch

We begin by understanding what a “.box” file is. A Vagrant “.box” is simply a
tarred gzip package containing, at minimum, the VM’s metafiles, disk image(s),
and a metadata.json; which identifies the provider the box has been created for.
Other optional supporting files, such as a custom Vagrantfile, can be packaged
with the box if desired.

Sanity Check

A quick sanity test. The instructions have been generalized and should work with
most Hosts and Guests of a similar configuration. Only the below have been
tested. So no guarantees explicit or implied.

Our Host

This article assumes an OS X development environment. All software on the Host
should be assumed the OS X version unless otherwise stated.

Apple OS X Mountain Lion (10.7.5)

VirtualBox 4.2.12

VMware Fusion 5.0.4

Vagrant 1.2.2

Vagrant’s VMware Fusion provider 0.6.3 with a valid license.

Potential Guests

All tested Guests are 64-bit distributions of Linux.

CentOS 6.4

Debian 7.0

Scientific Linux 6.4

Ubuntu 12.10 Desktop

Ubuntu 12.10 Server

Create the Virtual Machine

Start by launching the virtualization software and creating a VM which our
Vagrant box will be based off of. The following configuration options are
recommended when defining the VM:

VirtualBox

Create a new VM in VirtualBox:

Name: vagrant-<distro><arch> i.e. vagrant-centos64

Type: Linux

Version: <distro> (64-bit)

Memory Size: 1024MB*

New Virtual Disk:

Type: VMDK (Dynamically allocated)

Max Size: 40GB

Modify the VM’s settings:

Disable audio

Disable USB

Ensure Network Adapter 1 is set to NAT

Add a port forwarding rule as follows:

Name: SSH

Protocol: TCP

Host IP:

Host Port: 2222

Guest IP:

Guest Port: 22

Mount the Linux distro ISO

VMware Fusion

Create a new Virtual Machine with the Assistant:

Select “Continue without disc”

Select “Choose a disc or disc image…”

Locate the ISO for the distribution you want to install, then select “Open”

Installing the Guest OS

Boot the VM, install the Guest OS and customize the following settings when
prompted:

Host Name: vagrant-<distro><arch>

Root Password: vagrant

Full Name: vagrant

User: vagrant

Password: vagrant

Setting up the Guest OS

These are the minimal set of steps to get your Guest OS off the ground. I
personally recommend not polluting your box with extras if they’re not
necessary. Additional software packages can be added by a provisioning tool.
This allows your base box to be reusable across projects or teams.

Update the sudoers file

The vagrant user will be doing all the work for us. The vagrant user requires
password-less sudo privileges and needs to run without a tty (a terminal).

As most Linux distros only assign the root user to the sudoers group we must
add our vagrant user to the sudoers file. We’ll have to do this as root

$ su -
$ visudo

Ubuntu distros will add the user created during the installation to the
sudoers list. While the user has sudo permission we still need to change
the settings.

Cleaning Up

Before we package up our box we want to clean up the box to minimize it’s size.

RHEL6

$ sudo yum clean all

Once we are satisfied with the state of the VM, shut it down to prepare it for
packing.

$ sudo shutdown -h now

Packing our box

Packaging up the VM will create the .box file described earlier.
[tabbed_section][tab title=”VirtualBox” id=”t85” active=”true”]

The directory where the vagrant package command is executed is where Vagrant
will create the .box file. If you have a specific location where you want
this box to be created you should change to that directory prior to running
the vagrant package command. The example below we package a CentOS guest.