Creating Development Environments with Vagrant

3 (1 reviews total)
By Michael Peacock
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies

About this book

Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development/production parity, and makes the "it works on my machine" excuse a thing of the past.

"Creating Development Environments with Vagrant" is a practical, hands-on guide that walks you through the functionality of Vagrant, Puppet, and Chef to create powerful and flexible virtual development environments. Create your own virtualization environments containing configurations for different projects so that you can simulate complicated environments that can be easily shared with colleagues to get your projects up and running quickly and effortlessly.

"Creating Development Environments with Vagrant" starts with an introduction to virtualization and the concepts behind it, why it’s useful, and an overview of the architecture of Vagrant. We will learn to install Vagrant and get to know its prerequisites. Covering provisioning scripts with Puppet and Chef, learning to use them independently as well as with Vagrant to create a powerful combination.

If you want to locally test your projects, juggle multiple projects running on different versions of software, easily share technology requirement changes with colleagues, and, most importantly, want to perform all these tasks efficiently, then this book is for you.

"Creating Development Environments with Vagrant" will take you from a virtualization novice to running all of your projects across your team in robust, isolated virtual development environments.

Publication date:
August 2013


Chapter 1. Getting Started with Vagrant

Developing web-based applications can be complex. We have to be able to work with teams of people, who all need to be able to run and work on these projects, and we work with an ever-changing stack of technologies. I know personally I've spent countless hours setting up developers onto new projects in the past, and countless hours wrangling with WAMP and MAMP to switch to a newer or older version of PHP when juggling multiple projects. With everyone in a team working on their own machine, with their own development tools on their own operating systems, it's almost impossible to keep a consistent configuration across all the machines; especially if you have remote workers or freelancers where you can't force IT policies on them.

As projects get more complicated, it's also easier for auxiliary configurations to be forgotten about. Asynchronous workers, message queues, cron jobs; typically, we need to tell the rest of the team about these, and hope someone remembers them when it is time to deploy.

A virtualized development environment can help with this. Instead of having to battle configurations when working on other projects, each project can simply have its own virtualized environment. It can have its own dedicated web server, database server, and the versions of the programming language and other dependencies it needs. Because it is virtualized, it doesn't impact on other projects, just shut it down and boot up the environment for the other project.

With a virtualized environment, the development environments can also mimic the production environment. No more needing to worry if something will work when it gets deployed, if it is being developed on a machine with the exact same software configuration. Even if you deploy on a Linux machine but develop on Windows, your virtualized environment can be Linux, running the same distribution as your production environment.

While a virtualized environment makes things easier, by their nature, they are not the easiest of things to configure and manage themselves. They still need to be configured to work with the project in question, which often involves some level of system administrator skills, and we need to connect to these environments and work with them. They also, by design, are not very portable. You need to export a large image of the virtualized environment and share that with colleagues, and keeping that up-to-date as projects evolve can be cumbersome. Thankfully, there is a tool, which can manage these virtualized environments for us and provide a simple interface to configure them; an interface which involves storing configuration in simple plain text files, which are easy to share with colleagues, keeping everyone up-to-date as the project changes. This tool is Vagrant.

Vagrant ( is a powerful development tool, which lets you manage and support the virtualization of your development environment. Instead of running all your projects locally on your own computer, having to juggle the different requirements and dependencies of each project, Vagrant lets you run each project in its own dedicated virtual environment.

Vagrant provides a command-line interface and a common configuration language, which allows you to easily define and control virtual machines which run on your own systems, but which tightly integrate, allowing you to define how your own machine and the virtual machine interact. This can involve syncing folders such that the project code on your computer, which you edit using your IDE is synced so that it runs on the Vagrant development environment.

Vagrant uses Providers to integrate with the third-party virtualization software, which provides the virtualized machines for our development environment. The default provider is for Oracle's VirtualBox however, there are providers to work with Amazon Web Services and VMware Fusion. The entire configuration is stored in simple plain text files. The Vagrant configuration (Vagrantfile ), Puppet , and Chef manifests are simply written in text files in a Ruby Domain Specific Language. This means we can easily share the configurations and projects with colleagues, using Version Control Systems such as Git or Subversion.

When using Vagrant, the next time you need to go back to a previous project, you don't need to worry about any potential conflicts with changes made to your development environment (for example, if you have upgraded PHP, MySQL, or Apache on your local environment, or within the Vagrant environment for another project). If you bring a new team member into the team, they can be up and running in minutes Vagrant will take care of all the software and services needed to run the project on their machine. If you have one project, which uses one web server such as Apache, and another which uses Nginx, Vagrant lets you run these projects independently.

If your project's production environment involves multiple servers (perhaps one for the Web and one for the database), Vagrant lets you emulate that with separate virtual servers on your machine.

With Vagrant:

  • Your development environment can mimic the production environment.

  • Integrated provisioning tools such as Puppet and Chef allow you to store configuration in a standard format, which can also be used to update production environments.

  • Each project is separate in its own virtualized environment, so issues as a result of configuration and version differences for dependencies on different projects are things of the past.

  • New team members can be onboarded to new projects as easy as git clone && vagrant up.

  • "It works on my machine" is an excuse of the past.

  • The headache of linking code that you write on your own machine to your virtualized development environment, is taken care of either through custom-synced folders or the default-synced folder (everything in your project's folder gets mapped to Vagrant).

  • The environment can act as if it was your local machine and can map the web server port (80) of your development machine to your development environment if you wish.

  • You can let colleagues view your own development environment, as well as easily share the development environment.

  • Your local WAMP or MAMP installations will be gathering dust!

In this chapter, we will:

  • Discuss the requirements and prerequisites for Vagrant

  • Install Oracle's VirtualBox

  • Install Vagrant

  • Verify if Vagrant was successfully installed

Once we have Vagrant and its prerequisites on our machine, we can then look at using it for our first project.


Requirements for Vagrant

Vagrant can be installed on Linux, Windows, and Mac OS X, and although it uses Ruby, the package includes an embedded Ruby interpreter. The only other requirement is a virtualization tool such as Oracle's VirtualBox. The Oracle's VirtualBox provider is available for free, and is included built-in with Vagrant, so we will use and install VirtualBox in order to use Vagrant during the course of this book. Other providers are available, including one for VMware Fusion or Workstation, which is available as a commercial add-on (


Getting installed

Now that we know the software, which we need in order to get Vagrant running on our machine, let's start installing VirtualBox (so that we can use Vagrant's built-in VirtualBox provider) and Vagrant itself.

Installing VirtualBox

VirtualBox ( is an open source tool sponsored by Oracle, which lets you create, manage, and use virtual machines on your own computer.

VirtualBox is a graphical program, which lets you visually create virtual machines, allocate resources, load external media such as operating system CDs, and view the screen of the virtual machine. Vagrant wraps on top of this and provides an intuitive command-line interface along with integration of additional tools (including provisioners such as Puppet and Chef), so that we don't need to worry about how VirtualBox works or what to do with it; Vagrant takes care of it for us.

The first stage is to download the installer from the VirtualBox downloads page ( We need to select the download, which relates to our computer (OS X, Windows, Linux, or Solaris).

Once downloaded, let's open it up and run the installer. On OS X, this involves clicking on the VirtualBox.pkg icon that is shown on the screen. On Windows, simply opening the installer opens the installation wizard.

Before the installer runs, it first checks to see if the computer is capable of having VirtualBox installed we need to click on Continue to begin the installation process. While this process will vary from OS X to Windows to Linux, the process is very similar across all platforms. There are fully detailed installation instructions for all platforms on the VirtualBox website (

The first step in the process provides us with an introduction to the installation process and reminds us as to what we are actually installing.

Next, the installer informs us as to how much space it will use on our computer, and provides us with the option to customize the installation if we want to Change Install Location... and install the software in another location (perhaps another disk drive if our disk is getting full).

Let's leave the default install location as it is, and click on the Install button to have the installer install VirtualBox on our computer.

The installer then automatically installs VirtualBox for us.

Once the installation has finished, we are shown a confirmation screen with the option of clicking on Close to close the installer.

Now we have successfully installed VirtualBox!

Installing Vagrant

Now that we have the prerequisites installed on our computer, we can actually install Vagrant itself. This process is similar to that of installing VirtualBox. First, let's download the relevant installer from the Vagrant page (

Let's open up the installer and start the process. Again, on OS X, the first step is to double-click on the Vagrant.pkg icon.

We now need to follow the installation steps which are provided; this is very similar to the earlier steps for VirtualBox, and for most of the software packages in general.

Let's verify if Vagrant has been successfully installed, by opening a command prompt (terminal on Linux/OS X or cmd on Windows) and running Vagrant.

The preceding output shows that we have successfully installed Vagrant, and that we are able to run it.



In this chapter, we have looked at the requirements and prerequisites for Vagrant, which include a virtualization tool such as Oracle's VirtualBox (which works with Vagrant's built-in VirtualBox provider). We then downloaded and installed both Oracle's VirtualBox and Vagrant, and ran Vagrant to check if it was installed correctly.

Now that we have it installed, we can now move on to using Vagrant to set up and manage some of our projects.

About the Author

  • Michael Peacock

    Michael Peacock is an experienced software developer and team lead from Newcastle, UK, with a degree in software engineering from the University of Durham.

    After spending a number of years running his own web agency, and subsequently, working directly for a number of software start-ups, he now runs his own software development agency, working on a range of projects for an array of different clients.

    He is the author of Creating Development Environments with Vagrant, PHP 5 Social Networking, PHP 5 E-Commerce Development, Drupal 7 Social Networking, and Selling online with Drupal e-Commerce and Building Websites with TYPO3, all by Packt Publishing. The other publications Michael has been involved in include Advanced API Security, Mobile Web Development, Jenkins Continuous Integration Cookbook, and Drupal for Education and E-Learning; for these he acted as a technical reviewer.

    Michael has also presented at a number of user groups and technical conferences, including PHP UK Conference, Dutch PHP Conference, ConFoo, PHPNE, PHPNW, and Could Connect Santa Clara.

    You can follow Michael on Twitter (@michaelpeacock), or find out more about him through his website (

    Browse publications by this author

Latest Reviews

(1 reviews total)
short, but straightforward. not quite as in depth as i was hoping for.
Book Title
Unlock this book and the full library for FREE
Start free trial