- Creating Development Environments with Vagrant(Second Edition)
- Michael Peacock
- 773字
- 2025-02-19 14:05:20
Creating our first Vagrant project
Now that we have Vagrant installed on our machine, let's take a look at creating Vagrant projects. Any folder can act as a Vagrant project; it only requires a special configuration file, called the Vagrantfile, within it. Vagrant uses this file to set up the virtual machines (guests) and manage their integration with our computer (or host machine).
Vagrant has a command to create a Vagrantfile within the current directory you are in, within your computer's terminal: the vagrant init
command. To create a new project, let's create a new folder anywhere in our system – ideally, somewhere easily reachable, then we need to go into this folder, and run the init
command:

Vagrant will then create a Vagrantfile within that folder, and show us a confirmation message:

If we take a look at the contents of this Vagrantfile file that was created, we will see a range of configuration options. Most of the options are commented out (as they are prefixed with a #
character) to give us an idea of how we can configure the project.
There are only four lines of actual usable configuration in the file, as shown here:
VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "base" end
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.
Different versions of Vagrant use different structures and instructions within their Vagrantfile files. To make Vagrant backward compatible, the various settings are wrapped in a block of code that contains the version of the configuration to be used. Configuration Version 2 is compatible with Vagrant Version 1.1 up to Version 2.0.x (when released).
Within here, we have a single option —the type of Vagrant box to use. A Vagrant box is an image of an operating system that is configured to work with Vagrant.
We can generate a Vagrantfile without the illustrative comments by providing the minimal option (-m
or --minimal
, for example, vagrant init --minimal
).
In order to boot our Vagrant machine, we run the vagrant up
command:

As Vagrant doesn't know what the box base is, our project is unable to boot. While Vagrant projects have their configuration stored within their projects folder, boxes are installed globally on your computer and can be shared across projects. If Vagrant doesn't have a box installed with that name, it will either try to download it from the URL provided in the Vagrantfile (if there is one provided) or it will look for the relevant box in HashiCorp Atlas (formerly Vagrant Cloud). HashiCorp, the company behind Vagrant, provides official boxes for the latest Long Term Support version of Ubuntu (this box is hosted on and distributed through Vagrant Cloud; we will discuss this in more detail in Chapter 9, HashiCorp Atlas). To use this box, we simply pass the name of the box to the init
command (or update the Vagrantfile). As we already have a Vagrantfile in place for our project, we need to use the force
option to override the existing file (-f
or --force
):
vagrant init --force hashicorp/precise64
Now if we try to run our Vagrant project, Vagrant will look for the box, and this time, it will download the precise64
box from Vagrant Cloud, for use in our project:

Typically, boxes are minimal installations of operating systems that contain only what is required for the operating system to function, tools to integrate with Vagrant, and a minimal amount of other tools. This provides greater flexibility when it comes to using Vagrant to manage projects, as we can decide exactly what software we need to run on our virtual machine for our project to function, without having to worry about the conflicting versions of the said software. Some existing boxes may have more software installed, and of course, we may want to package a box that contains some of the key software our projects need (particularly, useful as a backup for users with unreliable Internet connections).
Note
While we are using commands to initialize our Vagrant projects in this chapter, these are simply quick ways to create a Vagrantfile
file with some prepopulated values. A Vagrantfile
file is the configuration file that defines how Vagrant should use the project (such as operating system to be used, virtual machines to boot up, synced folders, forwarded ports, and so on). We can, of course, create this file ourselves within the folder we wish to use as our project.