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.
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,
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
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.
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.
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,
config.vm.network "private_network", ip: "10.11.12.17" #use any ip address
config.vm.synced_folder "./", "/folderinsidevm", type: "nfs", nfs: true
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,
config.vm.provider "virtualbox" do |v|
v.name = "myvagrantmachine"
v.memory = 1024
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.