Provisioning Ubuntu with Java in 3 steps

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.

Accordingly, the first step to provision an Ubuntu box with Java is to initialize a 64-bit Ubuntu 12.04 LTS (Precise Pangolin) instance. You can do this via the vagrant init command like so:

Initializing a Vagrant box
<span class='line-number'>1</span>
<code class='bash'><span class='line'><span class="nv">$></span> vagrant init ubuntu.lts.64 http://files.vagrantup.com/precise64.box
</span></code>

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:

A basic VagrantFile contains the box and box_url attributes
<span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<code class='ruby'><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">box</span> <span class="o">=</span> <span class="s1">'ubuntu.lts.64'</span>
</span><span class='line'><span class="c1"># a few comments...</span>
</span><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">box_url</span> <span class="o">=</span> <span class="s1">'http://files.vagrantup.com/precise64.box'</span>
</span></code>

After the vm.box_url declaration, insert the following line:

Installing Java
<span class='line-number'>1</span>
<code class='ruby'><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">provision</span> <span class="ss">:shell</span><span class="p">,</span> <span class="n">inline</span><span class="p">:</span> <span class="s1">'wget --no-check-certificate https://github.com/aglover/ubuntu-equip/raw/master/equip_java7_64.sh && bash equip_java7_64.sh'</span>
</span></code>

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).

Save your VagrantFile and then, for step 3, run the following command in the same directory:

Firing up a new VM
<span class='line-number'>1</span>
<code class='bash'><span class='line'><span class="nv">$></span> vagrant up
</span></code>

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:

Java is installed!
<span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<code class='bash'><span class='line'>java version <span class="s2">"1.7.0_25"</span>
</span><span class='line'>Java<span class="o">(</span>TM<span class="o">)</span> SE Runtime Environment <span class="o">(</span>build 1.7.0_25-b15<span class="o">)</span>
</span><span class='line'>Java HotSpot<span class="o">(</span>TM<span class="o">)</span> 64-Bit Server VM <span class="o">(</span>build 23.25-b01, mixed mode<span class="o">)</span>
</span></code>

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?

Related:
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.