Read in 2 minutes

How to install Java on Ubuntu 18.04

In this tutorial, we will walk through installing and managing Java on Ubuntu 18.04. Java is one of the most popular programming languages used to build different kinds of applications and systems.

There are two main implementations of Java, OpenJDK and Oracle Java with almost no differences between them except that Oracle Java has a few additional commercial features.

Install OpenJDK

OpenJDK 10, the open source implementation of the Java Platform is the default Java development and runtime in Ubuntu 18.04. Once OpenJDK 11 reaches GA, it will become the default version in Ubuntu 18.04.

Installing OpenJDK is simple and straightforward.

Install OpenJDK 10 JDK

sudo apt install default-jdk

Install OpenJDK 8 JDK

sudo apt install openjdk-8-jdk

Install Oracle Java

In this section we will go through the steps of installing Oracle Java from the WebUpd8 repository. The WebUpd8 package will automatically download and install Oracle JDK for us.

To be able to add new repositories with the add-apt-repository command, first we need to install the install the software-properties-common package.

sudo apt install software-properties-common dirmngr

Now we can add the WebUpd8 repository and install Oracle Java:

sudo add-apt-repository ppa:webupd8team/java
sudo apt update

Install Oracle Java 8

apt-get install oracle-java8-installer

The installer will ask you to accept the Oracle license terms. To install the package you need to accept these conditions.

Advertisement

Set the default version

We can check the version of our default Java, with:

java -version
openjdk version "10.0.1" 2018-04-17
OpenJDK Runtime Environment (build 10.0.1+10-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 10.0.1+10-Ubuntu-3ubuntu1, mixed mode)

If we have multiple Java versions installed on the server we can change the default version using the update-alternatives system:

sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  3            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode

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

Enter the number when prompted and press Enter.

Uninstall Java

If for any reason you want to uninstall the Java package, you can uninstall it as any other package installed with apt. For example if you want to uninstall the openjdk-8-jdk package simply run:

sudo apt remove openjdk-8-jdk

If you don’t know which Java implementation to use, the general recommendation is to stick with the default OpenJDK version available on Ubuntu 18.04.

Thats all! Feel free to leave a comment below.