Vagrant Virtual Development Environment Cookbook

More Information
  • Define single and multiple virtual machine Vagrant environments
  • Provision Vagrant environments in a consistent and repeatable manner with various configuration management tools
  • Control powerful cloud resources from a desktop development environment
  • Use Vagrant to publish and share development environments
  • Start and expand your Vagrant environment with community resources
  • Share resources on a development machine with a virtual Vagrant environment

Vagrant allows you to use virtualization and cloud technologies to power faster, efficient, and sharable development environments. It duplicates the development environment to allow users to easily share and combine data on different machines and also takes care of security concerns.

Each recipe of Vagrant Virtual Development Environment Cookbook provides practical information on using Vagrant to solve specific problems and additional resources to help you learn more about the techniques demonstrated.

With recipes ranging from getting new users acquainted with Vagrant, to setting up multimachine environments, you will be able to develop common project types and solutions with the help of this practical guide.



Read an Extract from the book

Using existing virtual machines with Vagrant

We can repackage existing environments to use with Vagrant, replacing the shared disk with a new box file. While box files are still essentially virtual machines, boxes can be published (and updated) and even have additional configuration applied after booting. These box files can make managing virtual machines and different versions of these virtual machines vastly simpler, especially, if you don't want to build environments from base boxes every time.

Getting ready

In this example, we'll assume that we have an existing virtual machine environment built with Oracle VirtualBox.


This example will use a VirtualBox-only feature to set up box packaging, as Vagrant has a built-in shortcut to package existing VirtualBox environments into box files. Creating Vagrant boxes for other platforms will be covered in later chapters.

In this example, I'll choose an existing environment based on the CentOS operating system that has been created as a VirtualBox machine. In this case, the virtual machine has a few properties we'll want to note:

  • In this case, the virtual machine was created from an ISO installation of CentOS 6.5.
  • There is a user account present on the machine that we want to reuse. The credentials are:





  • The machine is used as a development web server; we typically access the machine through terminal sessions (SSH).


    We'll want to make sure that any machine that we will access using the vagrant ssh method has the SSH server daemon active and set to start on machine start. We'll also want to make one adjustment to the sshd configuration before packaging.

    With the machine created and active on a development workstation, the listing appears in the VirtualBox Manager console:

    If this machine boots locally and allows us to SSH into the machine normally, we can proceed to convert the virtual machine into a Vagrant box.

How to do it...

If we use an existing virtual machine with VirtualBox, we can use a few simple commands to export a new Vagrant box.

Packaging the VirtualBox machine

Before we can use the virtual machine with Vagrant, we need to package the machine into an appropriate box format.

  1. Note the name that VirtualBox assigns to the machine. In this case, our virtual machine is named CentOS as displayed in the left-hand menu of the VM VirtualBox Manager console:
  2. Create a temporary workspace to package the box. As with all Vagrant commands, you will do this on the command line. If you are working on a Unix machine (Linux or OS X), you can create a working directory with the mkdir ~/box-workspace command. This will create a folder in your home directory called box-workspace. Change directories to this workspace with cd ~/box-workspace.
  3. Execute the packaging command. (Warning! This is for VirtualBox only.) This command is:Vagrant Virtual Development Environment Cookbook
  4. We'll discuss a bit more about this in the following section. For now, Vagrant will return some text:==> CentOS: Exporting VM...This command might take some time to execute; Vagrant is copying the existing VirtualBox machine into a box file along with some metadata that allows Vagrant to recognize the box file itself. When this command is finished, you'll end up with a single file called in the working directory.
  5. Import the box file into your environment. In this case, we will directly add the box for use to our local Vagrant environment so that we can proceed to test the new box with a Vagrantfile. It is also possible at this stage to simply publish the box to a web server for use by others, but it is highly recommended to attempt to boot the box and access it with an example Vagrantfile. Your users will appreciate it. Add the box with the command:vagrant box add --name=centos64This command will copy the box to your local Vagrant cache, so you are now ready to directly use the box!

Configuring a Vagrant environment

Now that the box is added to our local cache, we'll need to configure a new Vagrant environment to use the box.

Initialize a new Vagrant environment with our new box. Do this by executing the vagrant init centos64 command. This will create a basic Vagrantfile that
uses our new centos64 box.

  1. Configure the Vagrantfile to use the correct user to SSH into the machine. We'll use the supplied username and password given in the preceding table. Edit the Vagrantfile created in the previous step to include two new lines that specify parameters for the config.ssh parameters:Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # All Vagrant configuration is done here. The most common configuration # options are documented and commented below. For a complete reference, # please see the online documentation at # Every Vagrant virtual environment requires a box to build off of. = "centos64" config.ssh.username="uaccount" config.ssh.password="passw0rd"
  2. By default, Vagrant relies on a common public key that is used by most box publishers that allows access to an account called vagrant. In this case, our environment will not have this key installed, we can instead configure the Vagrantfile to use a username and password. After the first login, Vagrant will place a key in the appropriate account; so, if desired, the password can be removed from the Vagrantfile after the first boot.
  3. Boot the environment. You might need to specify the provider along with the vagrant up command:vagrant up --provider=virtualboxIn this case, you will note quite a bit of output; the typical Vagrant boot messages along with some information about logging in with the password, replacing the password with the key, and so on. You might also (depending on how the box was packaged) see some information about Guest Additions. While Vagrant can use a virtual machine that has the guest additions disabled, some features (shared folders, port forwarding, and so on) rely on the VirtualBox guest additions to be installed. It's likely that your virtual machine has these already installed, especially if it has been used previously in a VirtualBox environment. Newly packaged boxes, however, will need to have the guest additions installed prior to packaging. (See the VirtualBox manual on the installation of guest additions at

Once the environment is booted, you can interact with the virtual machine, as you did previously, either through SSH or other services available on the machine.

How it works...

Using Vagrant with virtual machines is entirely dependent on the Vagrant box format. In this example, we used a built-in feature of Vagrant to export an existing VirtualBox environment into Vagrant. It's also possible to package box files for other environments, a topic we'll revisit later in the book. In this case, the package command generated a box file automatically.

The Vagrant box file is a file in a Unix Tape ARchive (TAR) format. If we untar the box file with the tar xvf command, we can look at the contents of the box to see how it works. The following are the contents of the untarred file:

-rw------- 0 cothomps staff 1960775680 Jul 24 20:42 ./box-disk1.vmdk -rw------- 0 cothomps staff 12368 Jul 24 20:38 ./box.ovf -rw-r--r-- 0 cothomps staff 505 Jul 24 20:42 ./Vagrantfile

So, the box file contains two files required to operate a VirtualBox virtual machine (the vmdk file that defines the virtual hard drive, and the ovf file that defines the properties of the virtual machine used by VirtualBox). The third file is a custom Vagrantfile that contains (primarily) the MAC address of the packaged virtual machine. There might also be custom files added to packaged boxes (such as metadata), describing the box or custom files required to operate the environment.

  • Configure and deploy software to Vagrant machines with scripts and configuration management tools
  • Manage and share Vagrant development environments with cloud
  • Packed with practical real-life examples to improve existing working systems
Page Count 250
Course Length 7 hours 30 minutes
ISBN 9781784393748
Date Of Publication 26 Feb 2015


Chad Thompson

Chad Thompson is a software developer, architect, and DevOps specialist in Central Iowa. He has 15 years of experience in creating and deploying applications for the Web. Chad began using Vagrant 3 years ago when he was trying to solve a tough problem in legacy application development. Since then, he has made use of Vagrant and configuration management tools to support the development and deployment of several web applications in data centers and cloud platforms. He holds certifications in Puppet and Oracle technologies and has enjoyed the pleasure of speaking before several technical conferences and camps. Chad holds two degrees in physics and can be found playing low brass instruments in ensembles around the state of Iowa.

Chad has written articles for O'Reilly web publications and the IOUG SELECT Journal (where he briefly worked as an executive editor). Recently, he reviewed the book Creating Development Environments with Vagrant for Packt Publishing, and recorded a set of video presentations titled Learning Git by Infinite Skills.