Beginners Guide to Vagrant


In this video we will install Vagrant, create our first Vagrantfile, then boot up this machine and see what we get by default.

There are two things we need to install to get Vagrant up and running, and a third to allow us to provision our newly created machine.

These are:

Vagrant allows us to describe the server(s) we want in a text (via the Vagrantfile) format.

Vagrant by itself cannot actually run the server. For that we need a Provider. A Provider is Vagrant-speak for our server virtualisation software.

In our case we will make use of the excellent and competitively priced (read: free) VirtualBox by Oracle. You may wish to use VMWare, a paid alternative to VirtualBox. If you do then be aware that there is also a cost for a plugin to integrate VMWare with Vagrant.

I have used both VirtualBox and VMWare for many years now. VMWare is fantastic, but VirtualBox is - for 99% of use cases - identical in features, and easier to get working in terms of Shared Folders (synced folders in Vagrant-speak). Plus, VirtualBox is free.

Lastly, we need a way of provisioning our newly minted server. This is the process of running our build script against the server to tell it what needs installing and how to configure the installed services.

For this process we are going to use Ansible.

Ansible is fantastic. It is free, and in my opinion, much simpler to understand and use than Puppet, Chef, or Docker.

Really the only downside to Ansible in this instance is that we must install it locally.

If you have a preference for one of the other supported Provisioning services, feel free to adapt and alter as you see fit.

Available Vagrant Boxes

Hashicorp, the organisation who created Vagrant, run a service called Atlas.

Atlas is a service which combines all the different development and infrastructure tools that Hashicorp have developed into a version control system for your infrastructure.

Atlas has a list of publicly available Vagrant boxes.

You can pick from a wide range of operating systems, and specific releases within those operating systems (e.g. the official Ubuntu trusty64 build, Hashicorp's own Ubuntu precise32 build, etc) depending on your project's needs.

Whilst in the video I use a 64bit Ubuntu instance, it can be preferable to stick to 32bit operating systems if you are sharing your project publicly. Not everyone can run 64bit machines, and this may exclude certain people from contributing to your project.

Creating our First Vagrantfile

Once you have Vagrant installed, creating our Vagrantfile is very straightforward:

cd /your/project/directory
vagrant init your/boxName
# e.g.
vagrant init ubuntu/trusty64

This won't actually download the server image. That only happens when you run vagrant up for the first time.

A nice feature is that if you have two different projects both using the same server build - e.g. both using ubuntu/trusty64 - then Vagrant would only download the box once, and share it between any projects that use it.

This is not to be confused with all your Vagrant installs running a shared server, only that they re-use the same base image. Think of it like installing Windows on 10 different machines using on 1 CD.

A bare bones Vagrant file will only name the server image we wish to use, and allow us to run:

vagrant up

The resulting server, once built, can be accessed by:

vagrant ssh

You won't need a username or password, but for reference both are vagrant by default.

Vagrant's Default Shared Folder

Even though it is not evident from our Vagrantfile, by default we do get a shared folder created for us.

Once you have vagrant ssh'd on to your new box you can access the shared folder:

cd /vagrant
ls

After running the ls command you should see your Vagrantfile`.

This is the same Vagrantfile we just used to build our new box. If you delete this file, it will delete it from your local machine also. Likewise, if you make any changes to this file, it will change the one on your local machine. This is a shared / synced folder.

We will cover setting up Vagrant synced folders in a future video. There are a few gotchas in there to be aware of.

Cleaning Up

We will cover cleaning up our Vagrant operation in a future video, but for now you might wish to do the following whilst ssh'd in to your vagrant box:

exit
# logged out, back to your terminal
vagrant halt
# shuts down your box 
vagrant destroy
# removes this installed Vagrant box instance, but keeps the ubuntu/trusty64 file on your disk

These are the basics of Vagrant. From here we will customise our Vagrantfile and configure our provisioning scripts to make the box instantly useful.

Episodes

# Title Duration
1 Beginners Guide to Vagrant 07:58
2 Vagrant Ansible Provisioning 14:35
3 Vagrant Housekeeping 03:12
4 Vagrant and Symfony2 13:50