As I’ve written about before, Vagrant is handy tool for creating localized VMs. It’s a lot like firing up EC2 images, but, for the most part, things are localized (you can, by the way, use Vagrant to fire up EC2 images). If you’ve ever used VMWare before, its the same thing, except Vagrant is free. You can create VMs of various operating systems, fire them up, and tear them down all with ease.
Vagrant plays nicely with hip DevOps frameworks like Chef and Puppet and if your installations require a number of components, then these tools are defiantly the way to go. Sometimes, however, a simple Bash script is good enough as in the case for auto-installing some base component, like Java, Node.js or Ruby.
Using Vagrant’s configuration file, aptly dubbed
Vagrantfile, you can instruct a VM instance to run a series of steps – these steps can be simple shell scripts, Chef cookbooks, or the Puppet equivalent.
This creates a
Vagrantfile in the directory where you ran the command and creates a named VM (i.e. “ubuntu.lts.64”) that is based off of Ubuntu 12.04 LTS.
Base Ubuntu installations do not come with Java; if you’d like to install a particular JDK, say Oracle’s JDK 7, you can leverage ubuntu-equip, which is a series of Bash scripts that install various components like Java, Node.js, MongoDB, Redis, Ruby, etc.
Thus, for step 2, open up the newly created
Vagrantfile and you should see two lines like so:
vm.box_url declaration, insert the following line:
This command instructs the instance to run an inline Bash command once it is up and running, which in this case auto-installs Oracle’s Java 7 JDK (see the ubuntu-equip project for more information).
VagrantFile and then, for step 3, run the following command in the same directory:
If this is the first time firing up this particular VM, you should see some text indicating that a particular box is being downloaded. Once the download is complete, the instance will boot up and subsequently invoke the inline provision command that kicks off the installation of Java.
If all goes well, you should see a lot of text scroll by ending with:
And that’s it. To use the VM, simply SSH to it. Go ahead and type
java -version just to convince yourself. Go ahead, I’ll wait for you…there, are you happy now? Wasn’t that easy? Provisioning Ubuntu VMs with Vagrant couldn’t be any easier with ubuntu-equip, dig it?