Getting started with Vagrant for Java Developers: Vagrant, Virtual Box, Cent OS (Linux) and Java JDK 8

Here’s a tutorial to get started with Vagrant, the easiest way to work with virtual machines on any OS.

banner

Why Vagrant?

  • Quick and easy way to get a virtual machine (VM) up and running with SSH.
  • Gives the capability to share environment as a file.
    For example – If your application development involves a lot of tools/softwares like MySQL, MongoDB, Kafka, RabbitMQ, ELK etc., it’s easy to share environment setup as a simple vagrantfile which can help anyone have environment up and running in no time.
  • No need to download and install VM images yourself.

In this example, we will setup a virtual machine running CentOS (Linux) with Java JDK 8 installed.

Pre-requisites

  1. Virtual Box
  2. Vagrant

centos-vm/Vagrantfile


# Configurations
ram = 2048			#ram in mb
storage = 20		#hdd size in gb for vm

Vagrant.configure("2") do |config|
	config.vm.box = "bento/centos-6.7"
	config.vm.hostname = "centos-vm-1"

	config.vm.network :forwarded_port, guest: 22, host: 12822, id: 'ssh'
	config.vm.network :private_network, ip: "192.168.33.51

	config.vm.provider "virtualbox" do |v|
	  v.name = "centos-vm-1"
	  v.customize ["modifyvm", :id, "--memory", "#{ram}"]
	  v.customize ["modifyvm", :id, "--usb", "off"]
	  v.customize ["modifyvm", :id, "--usbehci", "off"]
	end
end

centos-vm/java.sh


#!/bin/bash
yum update -y

cd /tmp
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.rpm"

sudo yum local install -y jdk-8u60-linux-x64.rpm

rm jdk-8u60-linux-x64.rpm

Steps / Execution

  1. Create a directory with name ‘centos-vm’.
  2. Change to directory ‘centos-vm’.
  3. Create a file with name ‘Vagrantfile’ with contents given above.
  4. Create a file with name ‘java.sh’ with contents given above.
  5. Execute command ‘vagrant up’ – to download the image from internet and provision the virtual machine as per ‘java.sh’ for the first time. Sub-sequent times, this command will just start the virtual machine.
  6. Execute command ‘vagrant ssh’ – to login to the virtual machine with ssh.
  7. Execute command ‘java -version’ – to test if the virtual machine has java installed properly.

Explanation

  • Vagrantfile has all the configurations required for the virtual machine, like –
    • ram (2048MB)
    • storage (20GB)
    • the image we want to install (“bento/centos-6.7”), there are images available for almost all the linux based operating systems.
    • static ip for the vm (192.168.33.51)
    • ssh port forwarding (12822) – just in case if you want to run more than one vm and/or you don’t want to use the default ssh port 22 on the host machine. You may also want to do port-forwarding if you choose to install other services like tomcat, mysql etc.,
  • You can extend this tutorial to include installations for other tools like mysql, tomcat etc.,

Please feel free to comment for questions, issues and suggestions.

Please share if this tutorial is helpful.

Advertisements
This entry was posted in java and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s