Install Oracle Java JDK in Ubuntu and Debian

Learn to install Oracle Java JDK in Ubuntu and Debian, and set it up completely with environment variables and PATH variables set. A typical installation of Ubuntu comes prepacked with the OpenJDK environment. This environment is substantially enough for regular users who wish to develop simple Java programs. However, some Java libraries are not included or compatible with the OpenJDK environment. Take Atlassian JIRA as an example of an application that requires the more traditional and proprietary Sun Java JDK platform. Due to its proprietary status, Ubuntu does not include this in its repository. Hence, we would need to install this manually.

UPDATE 22 FEB 2017: This post was recently updated to make the installation process seamless and easier to maintain. Keep me posted if the steps below doesn’t work for you!

Check Operating System Type

First things first, check whether you’re running on 32-bit or 64-bit Ubuntu:

uname -m
  • i686 – 32-bit kernel
  • x86_64 – 64-bit kernel

Download Oracle Java JDK

Now, go to the Oracle JDK download site and decide which version of JDK you would prefer.

In this guide, we’ll be downloading Java JDK 8 Update 92. Accept the agreement, and copy the tar.gz link for your operating system type.

We will use wget to download the compressed file. In your terminal, type the following:

wget --header "Cookie: oraclelicense=accept-securebackup-cookie"

The cookie header is included in the wget command because Oracle does not allow a direct download without accepting their agreement; thus, we parse a cookie to ensure that Oracle recognises that you have accepted their agreement.

Install Oracle Java JDK in Ubuntu and Debian

Create a new directory under opt:

sudo mkdir /opt/jdk

Then, extract the compressed file to the above directory:

sudo tar -xzvf jdk-8u121-linux-x64.tar.gz -C /opt/jdk

Verify that the files are extracted correctly:

cd /opt/jdk
ls -lah /opt/jdk

Now, we’ll create a symbolic link called current and link it with the newly extracted JDK directory:

sudo ln -s /opt/jdk/jdk1.8.0_121/ current

Next, we’ll need to register the Java binaries with update-alternatives:

update-alternatives --install "/usr/bin/java" "java" "/opt/jdk/current/bin/java" 1
update-alternatives --install "/usr/bin/javac" "javac" "/opt/jdk/current/bin/javac" 1
update-alternatives --install "/usr/bin/javaws" "javaws" "/opt/jdk/current/bin/javaws" 1

Finally, mark the new Java binaries as default:

update-alternatives --set java /opt/jdk/current/bin/java
update-alternatives --set javac /opt/jdk/current/bin/javac
update-alternatives --set javaws /opt/jdk/current/bin/javaws

To verify that the installation has been done correctly, check the version of java:

java -version

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Set Environment Variables and PATH

Finally, we’re going to set the $JAVA_HOME environment variable so that you’ll be able to run most Java applications.

To set for a single user:

Let’s load the .bash_profile from the home directory:

nano ~/.profile

Go to the end of the file, and add the following:

export JAVA_HOME=/opt/jdk/current
export PATH=$PATH:$JAVA_HOME/bin

Hit Ctrl and x to save the file, and y to overwrite it.

Finally, to ensure the changes are instant, run the following command:

source ~/.profile

To set for all users:

This step is similar to the above, with the only exception being that you would need to edit the global profile file. This allows every user the right to understand the environment variable that was set.

The global profile file can be found in the /etc/ directory.

Technology Consultant at ServiceRocket. Great knowledge in Atlassian JIRA. Blogger. Programmer. Anime Vector Artist.