Introduction to Vagrant
I’ve used excellent software like WAMP and MAMP in my local development environment for years. These tools are simple to setup, easy to use, and are excellent for personal programming; but issues arise when you use a distributed development workflow, deploy to servers, or have to cope with operating system level library requirements. And let’s not forget those acronyms leave out some important players like postgres, nginx, or rails!
What is Vagrant?
So what is Vagrant then? I’ll let Mitchell Hashimoto and the good people at Hashicorp explain: “Vagrant provides easy to configure, reproducible, and portable work environments with an easy-to-use workflow and focus on automation.”
I consider Vagrant as the pilot for my virtual machines. But for a typical developer, Vagrant ends up being just a few configuration files that you store in your codebase or repository that allows anyone, using almost any operating system, to use the exact same environment for your programming, however, the Vagrant configuration files don’t affect anything if your peers still want to use a different LAMP solution.
But It Works for Me!
No longer do you need to fully document or communicate all the details of your application configuration. Like what version of PHP should you use? Which extensions are required? What image libraries do I need to brew install? Get the idea? Vagrant makes the "it works on my machine" comment a thing of the past.
The 10 Minute Trial
Vagrant is powerful, and like many technologies, it can be fairly straightforward to use or very complex with endless options if you wish. So, we’ll just perform a simple server setup and I’ll show you where to go next. Also, keep in mind, the initial setup is the true overhead here. Once you’ve installed the prerequisites, spinning up environments is fast and trivial.
Install the Prerequisites
You need to install two pieces of software on your system: a virtualization provider (we’ll use VirtualBox, but there are others) and Vagrant. For now, install the following:
Let’s create a directory on your Desktop to keep our Vagrant test in. And yes, Vagrant is run from the terminal/command line so open up your console and follow these steps:
$ mkdir %userprofile%/desktop/test
$ cd %userprofile%/desktop/test
$ mkdir ~/Desktop/test
$ cd ~/Desktop/test
Remember, Vagrant is just the pilot for your virtual machine, so you’ll need to tell it what operating system to use for your environment. Vagrant calls this the “box”. We’ll use a 32-bit Ubuntu box for our test, but you can peruse the other boxes at the Vagrant Box Catalog.
To create a standard configuration file for your Vagrant project, you initialize it with `init` followed by the base box you wish to use.
$ vagrant init hashicorp/precise32
The vagrant init command creates a configuration file named Vagrantfile, that Vagrant will look for each time you start your virtual environment. The Vagrantfile is the foundation of your vagrant box. This is where you set ports, adjust memory, or call scripts to install custom software when the system boots. For now, let’s just start the server, which in this case will also provision the server (or download it from the Internet). The following command may take a few minutes depending on your Internet speed:
$ vagrant up
Once this command has finished, you’ll have a fully functional 32-bit Ubuntu server running in an virtual environment. This server shares a folder with your workstation. Let’s connect to your server, check the shared directory, then throw it away. You connect to your virtual server over a local ssh tunnel like so:
$ vagrant ssh
You are now logged into your virtual server. Your prompt should have changed to something like this:
Change to the vagrant shared directory and list out the contents:
$ cd /vagrant/
See your Vagrantfile? It’s the same file in your Desktop/test directory. If this was a project, you’d place your codebase in there and be able to edit locally on your workstation while seeing the updates from your server. Ok, let’s stop the server by first exiting your ssh tunnel and halting:
$ vagrant halt
That’s the short tour! To wrap up, if you want to get rid of the server (which takes up space on your workstation), type the following:
$ vagrant destroy
Where to go from Here?
All we did was start a server and turn it off - big deal. What you really wanted was a replacement for LAMP right? There are endless pre made systems ready for you to use like the Laravel Homestead box or the Scotch Box. Once you start using Vagrant for your development environment, I doubt you’ll go back to your MAMP setup anytime soon.