Read in 3 minutes

last updated 

How to install Java on Ubuntu 18.04

In this tutorial, we will walk through the process of installing Java on Ubuntu 18.04. Java is one of the most popular programming languages in the world, used for building different types of cross platform applications.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.

Java variations

Java is distributed in three different editions, Standard Edition (SE), Enterprise Edition (EE), and Micro Edition (ME). This tutorial covers the instalaltion of the Java SE (Standard Edition) edition.

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

We will show you how to install various Java packages. If you don’t know which Java implementation or version to use, the general recommendation is to stick with the default OpenJDK version available on Ubuntu 18.04.

Install the Default OpenJDK

The default Java version in Ubuntu 18.04 is OpenJDK 10. Once next LTS version OpenJDK 11 is released, it will become the default Java version in Ubuntu 18.04.

Follow the steps below to install the Java on your Ubuntu system:

  1. Update the package index

    First, update the package index to the latest version:

    sudo apt update
  2. Install Java

    Once the package index is updated install the default OpenJDK package with:

    sudo apt install default-jdk
  3. Verify the Java installation

    To verify the installation, we will simply check the java version by typing:

    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)

Install OpenJDK 8

At time of writing, OpenJDK 8 is the current LTS version of Java.

If your application requeres Java 8 you can install it by typing the following commands:

sudo apt update
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.

Fist add the WebUpd8 repository to your sources list:

sudo add-apt-repository ppa:webupd8team/java

If you get an error message saying add-apt-repository command not found then you will need to install install the software-properties-common package.

sudo apt install software-properties-common

Update the package index:

sudo apt update

Once the package index is updated, install the Oracle Java 8 package with:

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 Java version

To check the default Java version you would use the following command:

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 you have multiple Java versions installed on the server you can change the default version using the update-alternatives tool as shown bellow:

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:

To change the default Java version just enter the version number (the number in the Selection column) 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

Conclusion

In this tutorial you learned how to install and manage multiple Java versions on your Ubuntu server.

Your next step could be to install an application which runs on Java, such as Tomcat, JBoss/WildFly, Jetty, Glassfish, WebLogic, Cassandra, Jenkins ..etc

If you have any question or feedback feel free to leave a comment.