How to Use Vagrant for Easy Provisioning

How to Use Vagrant for Easy Provisioning

Vagrant helps you to deploy and manage virtual machines and install everything you need to run your web applications in a relatively simple way. I was doubtful at first, but it does work as promised and makes a provisioning easier.

In web development we always use a local environment to install everything we need to do our work and test our projects (e.g., Apache, PHP, MySQL, etc). I have always done this by hand, but doing this way has several CONs,

  • The process is very error prone. You may end up with different versions of your software in the different environments (i.e., dev, test, production). The operating system may even be different. If you’re not careful features may work in one environment and not in another.
  • Each time a new person joins the team they have to do the whole thing over again. This can chew up a lot of time that could better be invested in development.
    If anything goes wrong with your computer, you’ll have to preparing a new machine will make you lose time again.
  • Depending on your operating system, some tools may not be available directly, and compiling things manually could be problematic.
  • If you are working in different projects at the same time, you might run into conflicts, since some projects may depend on different software versions.

Vagrant comes to the rescue nicely. Vagrant allows you to deploy virtual machines and install everything you need to run your web applications in a relatively simple way. It makes provisioning easy and more robust.

How?

The process is relatively simple. When you have your Vagrant file (you could download it from somewhere or create your own), define your dependencies, and use the vagrant up command. Vagrant automatically setups a virtual machine and provisions it with the exact versions of the software you need.

A basic Vagrantfile looks like this,

For provisioning, it is possible to use shell scripting, Puppet, or Chef. Provisioning is executed the fist time you start the virtual machine. Once your virtual machine is created, you can stop it without losing your configuration using vagrant halt command. You can restart it at any time using the vagrant up command.

You can use your own OS image or you can start quickly using one of Vagrant OS images.

Connecting Remotely

Another important command is vagrant ssh, which allows you to run an ssh (i.e., secure shell) connection to your Vagrant virtual machine. In case you need to connect with your Vagrant machine, the default user is either “vagrant” or “root” and the password always is “vagrant.” Since these are internal machines, you don’t really need to change these credentials.

Shared Folders

Vagrant offers the possibility to create shared folders. This way you can be making changes to your code locally and see those changes reflected in your Vagrant virtual machine.

Shared folder performance is relatively good with small projects. For large projects you might have to use an NFS server to get good shared folder performance. The NFS feature is not available on MS Windows but there are other alternatives that might prove interesting, like Chirp.

To create shared folders, just add this configuration to your Vagrantfile,

Flexibility

Virtual Vagrant machines by default are generated with few resources (e.g., RAM, CPU) but you can change this setting in your Vagrant file for better performance.
Here is a very basic example of how to change the RAM,

Where Do I Start?

The obvious starting point is the official documentation.
There are several tools that can help you create the basic Vagrant boxes, including PuPHPet and Rove.

Chef and Puppet are excellent for provisioning, however using shell scripts is very easy and natural to start, but I really suggest you to explore these tools.
Give Vagrant a try, I think you’ll like it.

Focus Mode

Contact Request

Close

We will call you right away. All information is kept private