The following will show you how-to install Java on Ubuntu. I’ll be using Ubuntu 12.04.3 x64 for this tutorial.If you don’t already have a VPS or server, I recommend Digital Ocean.

First, let’s make sure Java is not already installed on the server:

java -version

Before we start installing JRE and JDK, let’s do an package repository update:

sudo apt-get update

Install JRE

To install Java Runtime Environment (JRE), run the following command:

sudo apt-get install default-jre

Install JDK

To install Java Development Kit (JDK), run the following command:

sudo apt-get install default-jdk

Install OpenJDK 7 or Oracle JDK

What we did above will install the default JRE and JDK, but does not install OpenJDK or the Oracle JDK.

OpenJDK

We’ll first start by installing the OpenJDK JRE by running the following command:

sudo apt-get install openjdk-7-jre

Next, you’ll install the OpenJDK JDK:

sudo apt-get install openjdk-7-jdk

Oracle JDK

Now let’s look at installing the Oracle JDK. First, we’ll install the python-software-properties package, like so:

sudo apt-get install python-software-properties

Okay, now we’ll install Oracle Java 7 through a PPA repository:

sudo add-apt-repository ppa:webupd8team/java

Now let’s update the package lists:

sudo apt-get update

We are now ready to install Oracle JDK 7. Execute the following command:

sudo apt-get install oracle-java7-installer

As I said, this will install Oracle JDK 7. If you want to install version 6, all you have to do is slightly modify the above command. Change oracle-java7-installer to oracle-java6-installer. As you can see, the key is the change is java7 changes to java6. If you want version 8, change java7 to java8.

Is Java Now Installed?

When we first started, we checked to make sure Java was not already installed. Now that we are done, let’s make sure Java is installed. Run the following command again:

java -version

You should see something along these lines:

java version “1.7.051″ Java(TM) SE Runtime Environment (build 1.7.051-b13) Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)