Home Cloud & Networking Vagrant Virtual Development Environment Cookbook

Vagrant Virtual Development Environment Cookbook

books-svg-icon Book
eBook $28.99 $19.99
Print $48.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $28.99 $19.99
Print $48.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Setting Up Your Environment
About this book
Publication date:
February 2015
Publisher
Packt
Pages
250
ISBN
9781784393748

 

Chapter 1. Setting Up Your Environment

In this chapter, we will cover:

  • Installing Vagrant and VirtualBox

  • Initializing your first environment

  • Installing Vagrant providers

  • Finding additional Vagrant boxes

  • Using existing virtual machines with Vagrant

 

Introduction


Over the past decade, data centers and server architectures have been revolutionized with the practice of virtualization—the ability to host computational resources that once depended on hardware in specialized software containers. The ability to use flexible virtual environments on shared computational resources allowed system administrators to become more flexible on how software is configured and deployed. More recently, the advantages of virtualization got extended to the desktop. Software packages such as the VMware Desktop (Fusion for OS X, Workstation for Windows and Linux) along with Oracle VirtualBox make it possible to run different operating systems and environments in the context of the desktop operating systems. Web developers, for example, can run a Linux-based web server on their desktop without modifying the parent operating system or running entirely separate physical computers.

Vagrant was originally launched as an open source project by Mitchell Hashimoto with the core idea to make virtual machines simpler to manage. Virtual machines have been used for software development for some time. Some software development teams developed workflows around building virtual machines and shared them with others—often through the creation of a completely configured virtual machine (referred to as a "golden image") and shared by users. If you have worked with virtual environments for any length of time, you are likely to be familiar with the process of downloading a multigigabyte virtual machine or passing around a portable drive with virtual machine files. Vagrant makes it possible to share consistent and reproducible environments with code rather than binary files. In practical terms, this means that a virtual machine is often used by checking out the source definitions from version control and running a vagrant up command rather than finding ways to create, copy, and manage up-to-date versions of large binary files. More recently, Vagrant proved to be so useful and pervasive that Hashimoto founded HashiCorp to support the ongoing development and support of Vagrant. In addition to core Vagrant development, HashiCorp created add-on software that allows Vagrant to use other hypervisor software (plugins for VMware Fusion and Workstation) as well as other software projects. More recently, Vagrant has been extended with the provider framework in order to make development with containers (such as Docker available at http://docker.io) simpler. Developing with containers gives developers the option to create lightweight isolated Linux environments that can be easier and faster to work with these virtual machines.

In any case, the first step when using Vagrant is to set up a working environment in order to define and run Vagrant machines. With Vagrant, a virtual machine and the software that runs inside the machine can be defined in a special file called a Vagrantfile. A Vagrantfile defines a virtual machine, how this virtual machine interacts with the outside world, and how software is installed on the virtual machine.

Before we start with Vagrant, let's review some terminology that we will use in this chapter and throughout the book.

A virtual machine is a computing node that runs within a software process that mimics the behavior of a physical computer. The software process (often called a hypervisor) provides infrastructure to virtual machines such as computing power (CPU), memory (RAM), and interfaces to external resources (such as networking interfaces and physical (disk) storage).

A host machine is a computer that runs a hypervisor to host virtual machines. A host machine will, most likely, run one of two types of hypervisor:

  • A Type 1 hypervisor that runs natively on host machine hardware. A Type 1 hypervisor does not require a separate operating system; the hypervisor itself controls access to physical resources and shares them between hosted virtual environments. Most modern shared virtual environments are Type 1 hypervisors (common examples include VMware ESX/ESXi, Oracle VM Server, and some versions of Microsoft Hyper-V). These environments are typically installed as shared resources that define server infrastructure or other shared resources.

  • A Type 2 hypervisor is a software that runs on top of a traditional operating system. In this case, the hypervisor uses the underlying operating system to control (or define) resources and gain access to resources. Most use cases for Vagrant use Type 2 hypervisors as host environments for virtual machines and this will be the environment that will be used throughout this book. The two common Type 2 hypervisors are Oracle VirtualBox and the VMware Workstation / Fusion family of software. We'll take a look at these products later on in this chapter.

In both cases, the hypervisor is responsible for managing physical resources and sharing them with one or many virtual machines.

A guest machine is a virtual machine that runs within the hypervisor. The machines that we will define with Vagrant are guest machines that operate within the environment controlled by our hypervisor. Guest machines are often entirely different operating systems and environments from the host environment—something we can definitely use to our advantage when developing software to be executed on a different environment from our host. (For example, a developer can write software within a Linux environment that runs on a Windows host or vice versa.)

As we proceed with the recipes, you'll see that Vagrant is a useful tool to manage the complexities of hypervisors and virtual machines. Vagrant also allows a consistent API to operate virtual machines on different hypervisors—something that can make sharing virtual environments much simpler between teams and people working on different platforms.

 

Installing Vagrant and VirtualBox


Before we explore how to use Vagrant, we'll first need to install the software required to manage a virtual machine environment (a hypervisor) as well as the Vagrant software itself. In this recipe, we will install VirtualBox to use it with Vagrant. VirtualBox is an open source hypervisor that was initially the only hypervisor supported by Vagrant. As such, VirtualBox is broadly supported by the Vagrant community.

Getting ready

Before we install the VirtualBox and Vagrant software, we'll need to obtain its latest versions.

VirtualBox can be downloaded from the project website at http://virtualbox.org. You'll notice that while VirtualBox has a corporate sponsor (Oracle), the VirtualBox software is open source and freely available for use. VirtualBox is also supported on a wide variety of host platforms with a few limitations:

  • VirtualBox is supported only on Intel or AMD hardware. The Intel/AMD platform constitutes the vast majority of personal computing platforms in use today, but there are always exceptions. Make sure to check the VirtualBox manual for supported operating systems.

  • While the VirtualBox specifications note fairly minimal system requirements, keep in mind that your single workstation will be supporting two (or more) running operating systems at the same time. A rough guideline for system's RAM is to have minimal RAM to support your host operating system, plus the operating system requirements of the individual guests. This will vary depending on the guest operating system. For example, if you are running your Vagrant environments on a Windows machine with 8 GB of RAM, you'll want to limit your Vagrant machine to use 6 GB of RAM, leaving enough working memory for the host operating system. If the operating systems are using too much memory, you'll notice some significant performance issues as the host operating system begins paging to disk.

The packages downloaded from the VirtualBox site will be native to your particular operating system. Take particular care when downloading Linux packages; you'll want to ensure that the downloaded package is compatible with the operating system and system architecture. (Linux users might also find VirtualBox in repositories provided by your operating system provider. These packages are often outdated, but they may work with Vagrant. Be sure to check the minimum versions required in the Vagrant documentation.)

Vagrant packages are operating system-specific and can be downloaded from the Vagrant website at http://vagrantup.com. Download the version appropriate for your system.

Note

Warning

Vagrant was initially available for download through the use of RubyGems and is still available through gem install. This version, however, is significantly outdated and unable to support most of the features that will be covered in this book. Due to the complexity of managing Ruby dependencies, the Vagrant maintainers decided to ship Vagrant as a standalone package with an embedded Ruby interpreter to avoid possible conflicts. It's recommended that you use the package distributions from http://vagrantup.com, wherever possible.

How to do it...

Installing Vagrant and VirtualBox is similar to other software installation for your particular operating system. The project sites include detailed instructions to install Vagrant or VirtualBox on the software platform of your choice. We'll go through the installation of Vagrant and VirtualBox on OS X. There are versions available for Windows and a wide variety of Linux distributions. In any case, the installers, all roughly, follow the same procedure for the OS X installation demonstrated here.

Installing VirtualBox

  1. Download a copy of the installer from the VirtualBox website. In this example, we'll choose the version for OS X hosts.

  2. Start the VirtualBox installer by opening the downloaded (OS X disk image) file. The disk image will include an installer along with documentation for VirtualBox and, if necessary, an uninstall tool. Double-click on the installer package to begin the VirtualBox installation.

    Note

    The VirtualBox installation will require administrator permissions to both install the package and to modify system network settings. The installation of the VirtualBox hypervisor requires the installer to create a set of new network interfaces, which will allow network communications between the host and guest operating systems.

  3. Once the installation is complete, the installer will give you the option to open VirtualBox. A new installation of VirtualBox will display a welcome message in a window titled Oracle VM VirtualBox Manager. Once a few virtual machines are created, this dialog displays information about the machines created using VirtualBox (or the Vagrant VirtualBox provider).

After the installation is completed and we are presented with the VirtualBox Manager dialog box, we can proceed with the installation of Vagrant itself.

Installing Vagrant

  1. Download a copy of the Vagrant installer from the Vagrant website (http://vagrantup.com). Select the appropriate version for your operating system. In this case, we will download the OS X universal installer that will download an installer that will work for both 32 and 64-bit machines. For the features discussed in this chapter (and for the majority of recipes in the book), you'll want to ensure that the Vagrant version is 1.5 or greater.

  2. The OS X download contains an installation package and an uninstall tool. Double-click on the installer to begin the installation. The Vagrant package installer is a native OS X package that will run the OS X software installer. Installing Vagrant will not be much different than installing other OS X software.

  3. The Vagrant installer will extract, copy files, and add the vagrant command to the executable path. On OS X, this will install Vagrant to the default OS X Applications/ directory. Vagrant is a command-line driven application, however, there are no programs accessed from the OS X Finder.

  4. Verify that Vagrant is working by opening a terminal window and executing the vagrant version command.

With both software packages installed successfully, we're ready to start using Vagrant!

Note

If you are a Ruby user or programmer, you might also note that a version of Vagrant is available via the Ruby gem package manager (gem install vagrant). When Vagrant 2.0 was released, the official distributions were released as packages with an embedded Ruby runtime. As such, the versions installed with the gem installer are outdated and will not work with most of the examples in this book.

How it works...

What we've done here is installed a working Vagrant environment that consists of:

  • A hypervisor application that can contain virtual machines

  • Vagrant, a tool that makes managing these machines simpler and available in code

It's important here to note that Vagrant is simply a framework to manage virtual machines, not an application to create and host virtual machines. When using a Vagrant environment, you'll often encounter errors that are not only related to Vagrant itself, but also related to the hypervisor application. For this reason, the choice of hypervisor becomes important when working with Vagrant. Many users can find tools that make VMware Desktop applications (Fusion and Workstation) simpler to troubleshoot when working with many virtual machines, whereas some will find it simpler to use external hypervisors (such as Amazon EC2 or DigitalOcean). Some experimentation might find the right workflow for you—keep in mind that Vagrant is a layer on top of many choices.

See also

 

Initializing your first environment


Once we have a working Vagrant environment with a hypervisor, we can initialize our first environment. There are two ways with which we can often work with Vagrant:

  • In a new environment with a newly initialized Vagrantfile

  • In an environment maintained in source control that has a Vagrantfile included in a project

Keeping Vagrantfiles and projects in a source control system (such as Git, SVN, and so on) is a powerful technique to manage and track changes in Vagrant environments. The use of source control systems allows developers and users to check in Vagrant projects, which makes modification of the project less risky and makes the sharing of Vagrant projects much simpler. The use of source code repositories reinforces the concept of infrastructure as code, giving administrators the ability to recreate environments in a consistent and repeatable way.

No matter how you use Vagrant, knowing how to initialize a new environment will aid you to effectively use Vagrant. In this example, we will initialize a new environment and look at the basic configuration of a Vagrantfile.

Getting ready

We've seen in the previous section that Vagrant itself is a command-line-driven application. There are some GUI tools available that can help start and stop environments, but in order to truly understand how Vagrant works, we'll use the command-line interface to initialize and interact with our environment.

For this example (and others in the book), you'll need to open a terminal window (a Unix terminal program in Mac OS X, or Linux, or the windows command application). Verify that Vagrant is installed by typing the command:

vagrant version

A full example of what the command-line session would look like is given in the following screenshot:

If you encounter errors or if the system cannot find Vagrant, you might either need to repeat the installation steps to install Vagrant in the previous recipe, or adjust your system path to include Vagrant. In most cases, the installer should complete this step for you.

Before proceeding with this first command, you might also want to make sure that your desktop machine is connected to the Internet with a fairly reliable and fast connection. In this example, you will be downloading a Vagrant box file that can be a few hundred megabytes in size. (Using a 12 MB/s download connection, I often note that Vagrant box downloads can take between 6 to 10 minutes on average.)

Once you've verified your command-line environment, we can proceed to initialize our first environment.

How to do it...

With a terminal window open and the command getting executed in a directory of your choice, run the command:

vagrant init puppetlabs/ubuntu-14.04-32-nocm

This command should return a brief text summary of your action, informing you that a new Vagrantfile has been created in the current directory. With this file in place, execute the command:

vagrant up

This command might output several results; we'll note a few important ones:

  • A status message indicating that the default machine is being started with the VirtualBox provider.

  • If you are running this command for the first time, a message will also be displayed noting that the box (in this case, puppetlabs/ubuntu-14.04-32-no-cm) cannot be found. Vagrant will automatically attempt to download a box file. This might take a while depending on the bandwidth available between you and the box provider. After starting a box for the first time, Vagrant will cache the box file itself so that subsequent uses of the box (even for different Vagrantfiles) will not trigger the download.

  • After the box file is downloaded, you should see messages that note machine startup, port forwarding, shared folders, and networking.

After Vagrant returns to the command line, executing the vagrant ssh command will open a command-line interface in the newly initialized virtual machine. In this example, the operating system is Ubuntu 14.04, which is specified in the return prompt:

With the virtual machine running, feel free to modify the machine—create files, install programs, or make any modifications you wish. Once you are finished with this environment, log out of the virtual machine either with a control-d command, or by typing exit. At this point, we can either keep the machine active as a background process or we might wish to:

  • Stop the machine, keeping the environment available for later use. This is accomplished with the vagrant halt command.

  • Destroy the machine, discarding the entire working environment. This is accomplished with the vagrant destroy command.

In this example, we'll discard the virtual machine by typing vagrant destroy.

Vagrant will now prompt you to make sure that you want to destroy the environment; type y to proceed with destroying the environment and deleting the VM. The entire machine can be recreated in this directory again with the vagrant up command.

How it works...

What we've done in this example is use Vagrant to create and destroy a virtual machine—an instance of Ubuntu running within the VirtualBox hypervisor. The information that Vagrant requires to create the environment is stored in a special type of file called a Vagrantfile. While Vagrantfiles can grow to become quite complex, this Vagrantfile contains only a few basic items of configuration.

Let's open the Vagrantfile we've created to see what our basic configuration instructs Vagrant to do. The first thing you'll notice when opening this file is that the initial Vagrantfile contains quite a bit of instruction on how to use the file—from box definitions to provisioning instructions. The only parts of the initial file that are not commented are:

  • A definition of the Vagrant environment itself

  • A definition of the box that serves as the base template of the environment itself

The opening of the Vagrantfile looks like this:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

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 vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "puppetlabs/ubuntu-14.04-32-nocm"
…

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You might notice a few features of the Vagrantfile itself:

  • Take note that the Vagrantfile uses the syntax of the Ruby programming language (http://ruby-lang.org). In fact, the Vagrantfile itself is Ruby code—something we'll use later on when we create more complex Vagrantfiles.

  • The Vagrantfile uses an API version. In this case, version 2: the most current version. Version 1 Vagrantfiles can still be found in use in a few projects as Vagrant itself can be backwards compatible. For most new projects, however, the latest revision of the API will be the one that is used.

  • The sole line of uncommented code is the definition of the config.vm.box parameter. This parameter was initialized with our init command that used this box name as a parameter. If we wished to change the base box for our project, we could do that in the definition of the config.vm.box parameter.

This Vagrantfile can be expanded to include more complex requirements, which will be explored in later recipes.

 

Installing Vagrant providers


Vagrant and VirtualBox are a great environment to get started with. However, there might be instances where the use of other desktop hypervisors would be preferred, such as the VMware Desktop products (Fusion and Desktop). Recent versions of Vagrant (1.1 or higher) support VMware as a commercial addition. The VMware Fusion provider was the first commercial product released by HashiCorp and was quickly followed by VMware Desktop support. You can find more information about Vagrant and VMWare support at http://www.vagrantup.com/vmware.

Many users (including myself) immediately found the VMware provider to be tremendously useful for its improved speed and stability of the VMware platform. In this recipe, we'll look at installing the plugins for VMware Fusion, keeping in mind that the VMware Desktop products and the Vagrant provider for the VMware Desktop are commercial products. You'll need to have on hand a VMware Desktop license for your platform and need to purchase the Vagrant provider for VMware from HashiCorp. In this example, we'll look at the installation of the provider, but keep in mind that all the examples in this book should also work with the freely available VirtualBox or Vagrant environment.

Getting ready

Before we can start with this example, we'll have to assume that you have purchased and installed the VMware Desktop product for your platform: Fusion for OS X, Workstation for Windows or Linux. These products can be purchased from a number of retailers or directly from VMware (http://www.vmware.com).

With VMware installed, we'll have to obtain a copy of the Vagrant provider directly from HashiCorp. At the time of writing this book, the plugin is not available through third parties. You can purchase the VMware plugin at http://vagrantup.com/vmware.

Once you have paid for the plugin, HashiCorp will send an e-mail with the download instructions and some basic instructions on how to install the provider. We'll walk through this installation in this recipe.

How to do it...

Vagrant providers rely on Vagrant's plugin capability—the ability to extend Vagrant through the Ruby environment. To install the plugin, open a command-line environment and execute Vagrant with the plugin command.

In this example, we'll install the VMware Fusion plugin, although the plugin installation will be similar for any number of providers. (See https://github.com/mitchellh/vagrant/wiki/Available-Vagrant-Plugins for a relatively up-to-date listing of maintained plugins.)

  1. Install the VMware Fusion plugin with the vagrant plugin install vagrant-vmware-fusion command.

    This will download the plugin and add the code to your local Vagrant installation. With many plugins, this will be the final step—installation itself is pretty straightforward. In this case, however, we'll need to install the license for the plugin.

  2. Install the plugin license using the plugin license command from the directory where the license file was placed:

    vagrant plugin license vagrant-vmware-fusion-license.lic
    

    This will install the plugin license and ready the plugin for use.

  3. Verify the plugin installation with:

    vagrant plugin list
    

    A list of currently installed plugins is returned, including some that are packaged with the distribution, these are marked system.

  4. Start a VMware environment by initializing a new environment. This will be identical to the steps in the prior recipe.

  5. With a terminal window open and the command executing in a directory of your choice, execute the vagrant init puppetlabs/ubuntu-14.04-32-nocm command

    This will create a new Vagrantfile that is identical to the previous example. This time, we'll start the environment with the provider option:

    vagrant up –provider=vmware_fusion
    

    A boot sequence will be presented with the difference to the prior example being that a new environment (box file) will be downloaded and booted. This new machine will use the VMware Fusion hypervisor to manage the Vagrant virtual machine.

How it works...

This example installed a new bit of functionality within Vagrant; the expanded functionality of plugins allows Vagrant to manage different virtual environments with an identical API. In general, Vagrant plugins can be used to extend Vagrant in a number of different ways—providers are

You might have noticed that the only difference in starting the Vagrant environment from the previous recipe was the use of the provider option when starting the machine. If you want to ensure that a virtual machine always uses a specific provider when starting, set the VAGRANT_DEFAULT_PROVIDER=vmware_fusion environment variable.

Setting an environment variable depends on your system and terminal shell in a Unix- based system (OS X, Linux); you might set this variable in your login shell profile (either .bash_profile or .bashrc), and for Microsoft Windows, this variable is set in the Environment Variables... dialog. Consult the documentation for your platform on how to create system variables.

With a VMware Desktop plugin installed, you can use VMware to manage virtual environments, whereas with other plugins, we can also use Vagrant to manage virtual machines locally with other hypervisors (for example, Parallels on OS X) or even in remote hypervisors (for example, VMware ESXi environments, Amazon Web Services). We'll see examples on how to use Vagrant in these environments in later recipes in the book.

See also

  • VMware: http://vmware.com. VMware provides a wide variety of hypervisor platforms from the desktop platforms used in this book to hypervisor infrastructures for data center management.

  • A list of currently available Vagrant plugins: https://github.com/mitchellh/vagrant/wiki/Available-Vagrant-Plugins. The Vagrant project keeps a list of plugins that are available to extend the functionality of Vagrant. The VMware providers are only one example of a wide variety of plugins available.

 

Finding additional Vagrant boxes


Up to this point, we have provisioned Vagrant environments using a single box—a version of Ubuntu 14.04 LTS (Trusty Tahr) provided by PuppetLabs, a company that sponsors the open source Puppet configuration management software as well as commercial Puppet products. (We'll see how to use Puppet with Vagrant in later recipes.) There are two reasons why we used this box in the examples:

  • PuppetLabs packaged Ubuntu 14.04 boxes for a few different hypervisors (VirtualBox and VMware).

  • PuppetLabs, as a company, offered a relatively stable set of boxes to develop Puppet. These should be broadly available after the publication of this book.

Most users will likely want to use Vagrant boxes that reflect the eventual production deployment environment of the code being developed inside Vagrant boxes and not just the single distribution we've seen so far.

To use different operating systems and operating environments, we need to obtain (or create) different Vagrant boxes. A Vagrant box is a packaged virtual machine that consists of a virtual machine image (a set of VMDK files for VMware, OVF files for VirtualBox) and a metadata file that specifies (at minimum) the provider that the box file uses along with other information that box users might need. Several Vagrant workflows use a base box along with provisioning to create new development environments, where the base box is the operating system that is eventually used in a production environment. For example, if a production environment has standardized on CentOS 6.5 as an operating system to host a web application, developers can use a Cent OS 6.5 Vagrant box as a development environment, ensuring that the web server versions and configurations are identical between environments.

There are many cases where you will want to build an environment, but in this example, we'll take a look at finding Vagrant boxes on the Vagrant Cloud (http://vagrantcloud.com).

Getting ready

Vagrant Cloud is an offering from HashiCorp to use and share Vagrant environments. Vagrant Cloud allows box providers and other users the ability to publish and share Vagrant boxes with other users. In many cases, these shared boxes will have certain software preinstalled for your use, and in other cases, the boxes will be basic operating system installations for you to provision and configure.

The navigation option DISCOVER BOXES on the top menu will take you to a repository (https://vagrantcloud.com/discover) for you to search for boxes and view information about box versions and what might be installed.

Note

A note on types of boxes

In this example, we will be downloading and using 64-bit Vagrant boxes, which might cause problems with some environments. In particular, 64-bit guests require systems to have Intel processors that support Intel Virtualization Technology (Intel VT) and have Intel VT support enabled in the BIOS settings of the host operating system. If you are unsure of the support available for your platform, there is a useful article on the VMware Knowledge Base with some tools to test the ability of your desktop system to support 64-bit guests. The article can be found here:

http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1003944

How to do it...

HashiCorp provides a repository of box files that can be downloaded for use in the Vagrant Cloud repository. At the time of writing this book, HashiCorp is also migrating the Vagrant Cloud repository to the new Atlas platform. While Atlas might have additional features, HashiCorp has committed to keeping the Vagrant Cloud features free to the community, including the box repository.

Finding boxes

Within the Vagrant Cloud box repository, we can search for boxes based on providers, operating systems, or software packages installed. At the time of writing this book, the Ubuntu 14.04 LTS release (Trusty Tahr) is starting to come into more widespread use a few months after release. As an example, let's find a basic installation of Ubuntu 14.04 to use in our environment.

  1. On the box repository page (https://vagrantcloud.com/discover), enter the search term ubuntu 14.04. You'll notice that by default, the discovery page displays a list of featured boxes—these are boxes that are popular or noted by the community as being of high quality or useful for a wide variety of environments. In our case, the Ubuntu release has not been published as a featured environment as of yet.

    Once the search term has been entered, the repository allows you to filter search results; this is useful if you are looking for a specific desktop version (for example, VirtualBox or VMware).

    Another item you might wish to note about the search results is that the repository also follows the naming convention for box naming of box creator or box name. Looking through the search results for ubuntu 14.04 and using the virtualbox filter, you'll find a box called ubuntu/trusty64. This box was created by the Ubuntu project itself and is fairly popular. At the time of writing this book, it has been downloaded over 200,000 times.

  2. The box name is a link to a page, which displays some detail about the box. The box providers might publish some further information about the box and how it can be used. There is also a stanza that can be copied and used to initialize a new environment.

Now that we have found a box to use, there are a few ways that we can use this box in our environment.

Initializing an environment with a new box:

Initializing an environment with our new box is identical to how we initialized our first environment in the previous recipe. In this case, we can even copy the line presented in the detailed description of the box in the Vagrant Cloud repository. In this example, copy the vagrant init ubuntu/trusty64 line into a new terminal window. This will generate a Vagrantfile with the config.vm.box = "ubuntu/trusty64" box definition.

As before, a simple vagrant up command will prompt Vagrant to download the box (if it has not been previously downloaded) and boot a new virtual machine instance. We now have an environment to begin provisioning and configuring.

Adding a new box without initializing an environment:

The other option to use a new box is to simply add the box to our local Vagrant cache for later use in Vagrantfile definitions.

Vagrant maintains a local cache of downloaded boxes for later use. New environments will simply copy the base image to boot new environments from the cache, rather than triggering downloads every time Vagrant is initialized. This is particularly handy when developing system configurations; destroying and rebuilding boxes will copy and provision clean images without requiring users to be concerned about maintaining snapshots, copies, or other artifacts of the virtual machine environment itself.

To cache a box for later use, execute this command in a terminal window:

vagrant box add ubuntu/trusty64

In this case, we're using the vagrant box command to manage our box cache. Using the add command will trigger the download of the box from the Vagrant Cloud repository to the local cache. With the box cached locally, we can use it later to initialize new environments without triggering a download. (As you might suspect, you can also clean up your cache by using the vagrant box remove command, or see a list of the boxes present in the cache along with the provider information about the box by using the vagrant box list command.)

There's more...

While there are several boxes to discover and use on the Vagrant Cloud, you might also encounter situations where there are different boxes or repositories used for Vagrant projects. In these cases, you can specify an HTTP URL to the vagrant box commands to cache boxes for later use. For example, a frequent case might be development teams sharing custom boxes on an internal server. In this case, adding the box would use the URL of the box file itself, as such:

vagrant box add http://servername/boxes/environment.box

Assuming the example here, servername represents a known web server address with a box called environment.box served from the boxes path on the web server. In this way, teams can share and use box files without requiring the use of the Vagrant Cloud service, or sharing Vagrant boxes using source control.

 

Using existing virtual machines with Vagrant


Using Vagrant to create new environments for use up to this point has been pretty simple so far. In the previous recipes, we have downloaded existing Vagrant boxes, created new Vagrantfiles, and booted entirely new environments. This is a pretty suitable use for new software (or configuration) projects, or to possibly create environments in order to migrate existing projects. This isn't such a good workflow if your team or you have existing virtual environments in use (such as the virtual machine on a network share or a flash drive that is passed around between team members).

Fortunately, 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.

Note

Warning!

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:

    Username

    uaccount

    Password

    passw0rd

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

    Note

    WARNING!

    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 package --base=CentOS --output=centos64.box
    

    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 centos64.box in the working directory.

  4. 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 centos64.box --name=centos64
    

    This 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.

  1. 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.

  2. 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 vagrantup.com.
    
      # Every Vagrant virtual environment requires a box to build off of.
      config.vm.box = "centos64"
      config.ssh.username="uaccount"
      config.ssh.password="passw0rd"

    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=virtualbox
    

    In 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 https://www.virtualbox.org/manual/ch04.html.)

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 centos64.box 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.

Latest Reviews (1 reviews total)
Vagrant Virtual Development Environment Cookbook
Unlock this book and the full library FREE for 7 days
Start now