Installing JDK on Linux

Both 32-bit and 64-bit versions of the Linux JDK versions are available. They can be downloaded in the following formats:

  • As a compressed .tar.gz file for manual installation
  • As an RPM Package Manager .rpm file for Linux versions supporting this packaging format

Oracle has certified a few Linux distributions for installing JDK. At the time of writing this, various versions of Oracle Linux, Red Hat, and Ubuntu are certified. If your Linux distribution is not one of these, it does not automatically mean that JDK or JVM won't work on your machine - just that it is not officially supported by Oracle.

In this section, we will only cover the installation of JDK on an Ubuntu installation. Ubuntu does not support the RPM format natively, so downloading the .tar.gz file is recommended on Ubuntu. Although not required on Linux, we will install JDK at the system-wide level. If you have a Linux distribution that supports RPM or , does not support some of the commands used below, refer to the Installation Instructions link on the Java SE Downloads page.

Open a new Terminal window, change to the directory where you placed the .tar.gz file, and type the following commands. A few notes on this:

  • The system's root password is required. If not available, substitute su with sudo -s, but sudo -s will only work if your user has root rights
  • The VERSION text must be substituted with the version number of the downloaded JDK
  • The assumed platform here is the 64-bit (x64) version; if you have downloaded the 32-bit version, replace x64 with i586

Here are the commands you need to type:

su
tar xvfz jdk-VERSION-linux-x64.tar.gz
ls
mv jdk1.VERSION /usr/local/

Take care that, in the last command, you move the directory extracted from the downloaded file, not the downloaded .tar.gz file itself. Also, note that the VERSION format of the directory is different than the downloaded file. Copy the full path to /usr/local/jdk1.VERSION to the clipboard or write it down. You will need it in the following step.

Now, set up the JAVA_HOME environmental variable. We want the Terminal to load it automatically for each user:

nano /etc/profile

Scroll to the end and add the following lines to the file. Paste the path you copied to the clipboard after the JAVA_HOME= text:

JAVA_HOME=/usr/local/jdk1.VERSION
export JAVA_HOME

Press Cltrl + X, say Y to save changes, and press Enter to confirm the filename.

Lastly, you need to register the JDK and JRE commands with Ubuntu. By entering the following commands in your Terminal window, the proper symbolic links will be created for the two most important commands: java (to run JVM applications) and javac (the Java compiler):

. /etc/profile
update-alternatives --install "/usr/bin/java" "java" $JAVA_HOME/bin/java 1
update-alternatives --install "/usr/bin/javac" "java" $JAVA_HOME/bin/javac 1

The first command reloads the modified /etc/profile file so that the JAVA_HOME variable is available. Note the first dot in the command.

If you want to use other commands from JDK's bin subdirectory in future, you'll have to use the update-alternatives command illustrated in the preceding code in a Terminal with root access, and you'll have to do so for each command that you plan to use.

Type exit twice and close the Terminal window. Now open a new Terminal window (do not request root access) and enter the following command to verify the installation:

javac -version

If everything goes smoothly, you'll see the version number that would match the downloaded JDK.