Kicaj Blog

Vagrant basics part 1

In previous blog posts I described how to setup Apache, PHP and MySQL development environment. Setting it up was relatively easy but it involved a lot of steps and required some time.

This approach is fine if you need to do it once but the problem arises when you need more than one environment, each configured differently. For example, when you work with few projects in the same time. It’s becoming even more complicated if you work with other developers and you need to make sure all of you have environments setup the same way.

This is where Vagrant shines. It allows you to quickly spin up virtual machine (VM) and configure it, based on “recipe” file called Vagrantfile, which you can later share as part of your project with other developers so everyone has the same development environment as you.

In this blog post I’ll show you how to install and get familiar with Vagrant as well as how it can help you in your development process.

Video tutorial

Prerequisites

To use Vagrant you actually need to install two peaces of software. The Vagrant itself and virtualization software. In this tutorial I will be using VirtualBox as virtualization platform.

To download VirtualBox go to VirtualBox download page and click the download link for your operating system. You should also download the VirtualBox Extension Pack from the same page.

Once you have both install VirtualBox by double clicking dmg file and when the installer opens double click pkg file.

Once you finish the installation start VirtualBox and go to VirtualBox -> Preferences -> Extensions. Then click the Add button on the right to add Extension Pack file you downloaded. Follow the installation steps.

When the installation is finished you can download and install Vagrant.

Vagrant installation

Vagrant basics

Once Vagrant is installed we can start playing with it. Start the Terminal, create directory for your example project and cd to it. We will start with the default Vagrantfile which can be generated using command:

$ vagrant init

The default Vagrant configuration file has a lot of informative comments. I encourage you to read it. All of the options are commented out except one that is close to the top of the file

config.vm.box = "base"

which tells Vagrant what VM image (box) to use. For now this is the only one that we are interested in. By default it’s set to box called base. But for this tutorial we want to use Ubuntu Wily image.

To find what we need to replace base with go to Boxes search page or click Boxes link on the top navigation bar of https://vagrantup.com. When the search page opens type “ubuntu wily” in the search box. The box we want should be the first link on the results page.

Click the link to see more details about the box. To use the box simply put its full name as a value for config.vm.box:

config.vm.box = "ubuntu/wily64"

When you are done save and close the Vagrantfile. Now we are ready to start our first virtual machine with

$ vagrant up

command.

When you run it the first time Vagrant will download the selected box. Download time will vary depending your Internet connection speed and the box size. But it’s done only once. Vagrant caches downloaded boxes and reuses them as needed.

After a few minutes Vagrant should be done creating and configuring virtual machine. You can verify the VM is running by opening VirtualBox, where you should see one VM running.

Let’s take a quick look at what Vagrant did for us by looking at the vagrant up log:

It downloaded the Ubuntu Wily box.

It setup the VM name.

It forwarded ports between host and guest OS.

In our case only the SSH server port.

It configured network interfaces.

It configured SSH service and the user in the guest OS.

It mounted shared directories.

By default Vagrant mounts the directory with Vagrantfile from host OS to guest in /vagrant.

And lastly it provisioned the guest OS.

The provisioning is customization of the guest OS based on Vagrantfile and user configuration. We will talk about provisioning in the next tutorial.

It’s a lot of stuff we didn’t have to do by hand. Impressive considering to achieve that we didn’t have to do much.

At this point we can SSH to the VM, which is made simple by Vagrant, with command:

$ vagrant ssh

If everything is OK you should see Ubuntu command prompt.

Shared directory

As I mentioned Vagrant by default mounts host OS directory where Vagrantfile is to guest OS. Let’s check it out. Change directory to /vagrant:

$ cd /vagrant

and list files with ls:

$ ls
Vagrantfile

The Vagrantfile you see is exactly the same file you have on the host OS. So if we open the file with text editor and edit it and then open the Vagrantfile on the host OS it will have the changes. The same works the other way around. If you edit the file on the host OS the changes will be instantly visible on the guest OS.

It means you can work on your project files on the host OS using for example some kind of IDE and your changes are instantly visible on the guest OS.

Other useful Vagrant commands

To exit guest OS type:

$ exit

It will close the ssh connection but it will not stop the VM which is still running in the background. You can verify that by checking the VirtualBox.

To stop the VM issue:

$ vagrant halt

To start it again use:

$ vagrant up

as before.

The second time you start up the VM Vagrant will not download the box. So the startup process is way faster.

It is very important to know that all the changes to the VM are preserved between restarts but if you wish to totally destroy the VM, to start from scratch for example, issue:

$ vagrant halt
$ vagrant destroy

Destroying the VM will remove the box that has been provisioned for this project only. It will not remove the cached box, so starting the VM with vagrant up will be faster then the first time when Vagrant was downloading the box. You can verify this by issuing: