How To Install Java with Apt-Get in Ubuntu 16.04

Java and the JVM (Java Virtual Machine) are widely used and required for many types of software. This article will guide you through the process of installing and managing different versions of Java using apt-get.

Default Installation JRE / JDK

The most straightforward option for Java installation is using the packaged version with Ubuntu. Explicitly, this will install OpenJDK 8, the latest recommended version.

First, we will update the package index.

sudo apt-get update

Next, we will install Java. Specifically, this command will install the Java runtime environment (JRE).

sudo apt-get install default-jre

There is another Java default installation called JDK (Java Development Kit). The JDK is usually only needed if you are going to compile Java programs or if the software that is going to use Java specifically requires it.

The JDK contains the JRE, so there is no problem if the JDK is installed instead of the JRE, except for the size of the file.

You can install the JDK with the following command:

sudo apt-get install default-jdk

Installing the Oracle JDK

If you want to install the Oracle JDK, which is the official version distributed by Oracle, you will have to take a few more steps.

First, add Oracle PPA, then update the package repository.

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update

Then, depending on the version you want to install, we will execute one of the following commands:

Oracle JDK 8

This is the latest stable version of Java at the moment, and the recommended version to install. You can do it using the following command:

sudo apt-get install oracle-java8-installer

Oracle JDK 9

You can find more information about Java 9 on the official JDK 9 page.

To install JDK 9, use the following command:

sudo apt-get install oracle-java9-installer

Managing Java

There may be several Java installations on a server. You can configure what will be the default version for use by using the command line update-alternatives, which manages which symbolic links are used for different commands.

sudo update-alternatives --config java

The output will be something like the following. In this case, this is what the output will show with all the installed Java versions mentioned above:

There are 5 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
* 0            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      auto mode
  1            /usr/lib/jvm/java-6-oracle/jre/bin/java          1         manual mode
  2            /usr/lib/jvm/java-7-oracle/jre/bin/java          2         manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  4            /usr/lib/jvm/java-8-oracle/jre/bin/java          3         manual mode
  5            /usr/lib/jvm/java-9-oracle/bin/java              4         manual mode

Press <enter> to keep the current choice[*], or type selection number:

Now you can choose the number you want to use as the default. This can also be done for other Java commands, such as the compiler (javac), the documentation generator (javadoc), the signature JAR tool (jarsigner), and more. You can use the following command, filling in the command you want to customize:

sudo update-alternatives --config command

Define Environment Variable JAVA_HOME

Many programs, such as Java servers, use the environment variable JAVA_HOME to determine the location of the Java installation. To set this environment variable, you must first find out where Java is installed. You can do this by executing the same command as in the previous section.

sudo update-alternatives --config java

Copy the preferred installation path and then open /etc/environment using nano or your favorite editor

sudo nano /etc/environment

At the end of this file, add the following line, making sure to replace the route with your copied route.


Save, exit the file and reload it.

source /etc/environment

Now you can test if the environment variable has been set by executing the following command:


This will return the route you just set.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *