Read in 6 minutes

last updated 

How to Install 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.

This tutorial describes how to install OpenJDK as well as Oracle Java on Ubuntu 18.04.

The same instructions apply for Ubuntu 16.04 and any Ubuntu-based distribution, including Kubuntu, Linux Mint and Elementary OS.


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 installation 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.

Installing 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 Java OpenJDK on an Ubuntu machine:

  1. First, update the apt package index with:

    sudo apt update
  2. Once the package index is updated install the default Java OpenJDK package with:

    sudo apt install default-jdk
  3. Verify the installation, by running the following command which will print the Java version:

    java -version

    The output will look something like this:

    openjdk version "10.0.2" 2018-07-17
    OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
    OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)

Installing OpenJDK 8

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

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

sudo apt update
sudo apt install openjdk-8-jdk

Installing Oracle Java with APT

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 Java 8 JDK for us.

  1. First, 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 the software-properties-common package.

    When adding the repository, you’ll see a message like this:

    Press Enter to continue and the installer will update the package index.

  2. Update the packages list and install the Oracle Java 8 package with:

    sudo apt update
    sudo apt install oracle-java8-installer

    To install the package you’ll need to press Enter and then accept the Oracle Binary Code license terms by selecting Yes as shown below:

    Once you accept the license the installer will download the Java tar.gz package from the Oracle download page and will install Oracle Java 8 JDK on your Ubuntu system.

Installing Oracle Java from the Oracle Website

At the time of writing this article, the WebUpd8 repository includes only the latest version of Java 8. If you want to install Java 10 or any other specific Oracle Java version then you can simply download the Java tarball from the Oracle’s downloads page.

In this guide, we will download the latest Java version 10. Before downloading the Java tar.gz file you should check if there is a new version available.

Follow the steps below to install the latest Oracle Java from the Oracle Website:

  1. Start by downloading the Java archive file using the following curl command:

    curl -L -b "oraclelicense=a" -O

    The command above will automatically accept the Oracle license.

  2. Next, create a directory for the Java installation:

    sudo mkdir /usr/local/oracle-java-10
  3. Extract the Java .tar.gz file to the previously created directory:

    sudo tar -zxf jdk-10.0.2_linux-x64_bin.tar.gz -C /usr/local/oracle-java-10
  4. Once the files are extracted run the following commands to create new alternatives:

    sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/oracle-java-10/jdk-10.0.2/bin/java" 1500
    sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/oracle-java-10/jdk-10.0.2/bin/javac" 1500
    sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/oracle-java-10/jdk-10.0.2/bin/javaws" 1500

    The alternatives command is used to create and remove symbolic links which are determining default commands.

We have extracted the Java files in the /usr/local/oracle-java-10 directory, you can use a different directory if you want.

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 below:

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
  4            /usr/local/oracle-java-10/jdk-10.0.2/bin/java    1500      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.

Set the JAVA_HOME Environment Variable

Some applications written in Java are using the JAVA_HOME environment variable to determine the Java installation location.

To set the JAVA_HOME environment variable, use the update-alternatives command to find where Java is installed:

sudo update-alternatives --config java

In our case the installation paths are as follows:

  • OpenJDK 10(11) is located at /usr/lib/jvm/java-11-openjdk-amd64
  • OpenJDK 8 is located at /usr/lib/jvm/java-8-openjdk-amd64/jre
  • Oracle Java 8 is located at /usr/lib/jvm/java-8-oracle/jre
  • Oracle Java 10 is located at /usr/local/oracle-java-10/jdk-10.0.2

Copy the installation path of your preferred installation. Next, open the /etc/environment file:

sudo nano /etc/environment

Add the following line, at the end of the file:


Make sure you replace the path with the path to your preferred Java version.

You can either log out and log in or run the following command to apply the changes to your current session:

source /etc/environment

To verify that the JAVA_HOME environment variable is correctly set, run:


/etc/environment is a system-wide configuration file, which is used by all users. If you want to set the JAVA_HOME variable on a per-user basis then add the line to the .bashrc or any other configuration file which is loaded when the user logs in.

Uninstall Java

If for any reason you want to uninstall the Java package, you can uninstall it like 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


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

You can now install applications that run on Java, such as Tomcat, JBoss/WildFly, Jetty, Glassfish, WebLogic, Cassandra, Jenkins, Gradle …etc

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