Pinterest Stumbleupon Whatsapp
Ads by Google

If you’ve ever worked on a Web development project, you know just getting started can be tough. Even if you’re just making a simple WordPress widget How To Create Your Own Basic Wordpress Widgets How To Create Your Own Basic Wordpress Widgets Many bloggers will search for the perfect Wordpress widget that will do exactly what they want, but with a little programming experience you may find it's easier to write your custom widget. This week I'd... Read More , you’re going to need a WordPress instance to work with. That often means working on one somewhere in the cloud, or maybe setting up a local Web server Run A Local PHP Web Server on Mac OS X With Zend Server Run A Local PHP Web Server on Mac OS X With Zend Server Hosting your own PHP server has two main advantages. First of all you can host it locally so you don't need to bother with transferring your files and only need to refresh the page in... Read More . And if you’re collaborating with anyone, they’ll have to create exactly the same setup, too.

That’s annoying, but it gets worse: If you happen to be working on more than one project at the same time, and both projects use slightly different stacks (different Web servers, versions of PHP, etc.) you may find yourself with a lot to keep track of. Thankfully, there’s a better way: Meet Vagrant, a free and powerful way to create project-specific virtual machines.

The End Result, Or: Why Bother?

computer

The concept behind Vagrant sounds a little tricky at first. You basically spin up a VM that runs your Web server and any related scripts, but your project folder is outside the VM. So you can use whatever text editor and browser you usually work with, and don’t have to put up with a slow VM GUI. The VM just does the heavy lifting: It runs a local Web server and serves whatever files you need.

The appeal is ease of use: Once you have a Vagrant box configured for your project, when it’s time to get coding, you simply go to the project folder and type vagrant up. This boots up the VM, and off you go. When you’re done, shut the VM down with vagrant halt and that’s it – nothing polluting your hard drive and system configuration, it’s all self-contained.

What’s even cooler is that you don’t have to understand how the magic happens: Let’s say you’re part of a three-person development team. Only one guy really knows how to configure the environment (a common situation). In the past, he’d have to set up every machine manually. Now, he can just do it once, and everyone else can type vagrant up and get rolling – sharing code on GitHub (or BitBucket, as Matthew recommends Love GitHub? 4 Reasons Why You Should Host Your Code On BitBucket Love GitHub? 4 Reasons Why You Should Host Your Code On BitBucket You have to think about where you intend to store your code. It's likely you've have heard of GitHub. That's not surprising. GitHub is used by individuals and enterprises to host code, collaborate on documentation... Read More ) and coding on the same setup.

Ads by Google

Note that Vagrant is meant to work alongside a source control system: You set up your runtime environment in Vagrant, and you use Git for the code. So if someone just has your Vagrant box, they won’t be able to do much with it, because all of your Website’s files would be missing: These are found in a folder outside the virtual machine.

Installing Vagrant and Initializing a Box

Vagrant itself can be easily installed on Windows, OS X, and Linux. Go to the official download page and grab an installer for your OS of choice. You may need to install VirtualBox and Ruby separately, but that depends on your particular case.

Once you have Vagrant installed, it’s time to kick the tires with the default box. Type the following incantation:

vagrant box add base http://files.vagrantup.com/precise32.box
vagrant init
vagrant up

The first line adds a “base box” to your system, and is going to take a while as it actually downloads the box from Vagrant’s servers. You’re getting the 32-bit version of Ubuntu 12.04, Precise Pangolin. The second line initializes the box, and the third line boots it up.

Assuming it all goes well, your console should look something like this:

vagrant

You can now type vagrant ssh to ssh into your new box, just to see that it works.

These commands will work from within the folder that contains your Vagrant box (whatever folder you ran vagrant init in). This means you can have multiple Vagrant VMs configured (and perhaps even running) alongside each other – one VM per project, for example.

In itself, this is not the most exciting outcome in the world. I mean, it is neat that you now have an Ubuntu VM running, but you can’t really do much with it out of the box.

To make things more useful, you would either need to set your environment up on this box (installing the Web server and development stack you need), or you’d need to find a pre-existing box that already has much of what you need. Since setting up a development environment is beyond the scope of this article (and changes from project to project), let’s now take a look at the wide variety of existing boxes you can get.

Getting Other Boxes

Even if you do know how to set up the development environment you need, the whole point of Vagrant is that it saves duplication of effort. For example, if you’re a WordPress developer, you need Apache, PHP, MySQL, PhpMyAdmin, and WordPress – and you’re not the only one. In this particular case you could go to a site called Vagrantpress and with a few quick lines of code, set up everything you need:

wget -O vagrantpress-master.zip https://github.com/chad-thompson/vagrantpress/archive/master.zip 
unzip vagrantpress-master.zip 
cd vagrantpress-master 
vagrant up

And that’s it – you would now be able to go http://localhost:8080 and see WordPress running. This bit of magic (being able to go to your local host’s port 8080) takes place courtesy of VirtualBox’s bridged Ethernet adapters.

This is just one premade Vagrant box out of hundreds of different boxes floating online. For a large collection, you could visit Vagrantbox.es, which is basically a large table listing the many boxes. Note that Vagrantbox.es doesn’t actually host the boxes – instead, the box files are spread all over the place, large in Sourceforge, Dropbox, or Google Drive folders. This also means they could go down any time, so once you find a box that works for your project, you should make a local copy of it – or better still, export the box you end up working with (as you will doubtlessly make some tweaks and customizations).

Not an End, But a Beginning

This post was not meant as a general guide for Vagrant — that’s what the Vagrant docs are there for. Instead, I hope you now have a sense of what a cool tool Vagrant is, and how useful and approachable it can be. Now go make a box!

  1. Kevin M
    January 4, 2014 at 6:50 am

    To bad the directions are so vague on how to set this up. Appears if you don't want to purchase the book you may as well use something with better documentation. I have been using XAMPP but I have to run three different versions to support the various projects I have. Would be great to have a solution that I don't have to keep switching. Of course it has to be a solution that is documented with more information. It would be nice if the developer knew CSS so that the page was well formatted. None of which the developers of Vagrant appear to be capable of.

    • Erez Z
      January 5, 2014 at 5:21 pm

      That's so weird to hear - the official docs are at http://docs.vagrantup.com/v2/ and are beautifully formatted, at least for me. Also, installation was really a next-next-next affair, again, for me (on Windows).

      Are you looking at that docs URL?

  2. Doc
    January 3, 2014 at 10:07 pm

    "Vagrant itself can be easily installed on Windows, OS X, and Windows." Right...I think you meant one instance of "Windows" to be "Linux."

    • Erez Z
      January 5, 2014 at 5:20 pm

      Oops, sorry about that! I meant to say it can be installed easily on Windows, Windows, and Windows. :)

      j/k, post fixed! Thanks for the catch!

      (But what about Windows?)

Leave a Reply

Your email address will not be published. Required fields are marked *