This post shows students and new users how to install OpenJDK and Oracle Java JDK on Ubuntu Linux. Anyone who creates Java-based applications will use the Java programming language. The Java language is popular and is in almost all operating systems.

Java can be implemented in a number of ways. When it comes to Linux, there are two main implementations of Java: OpenJDK and Oracle Java JDK. There are two identical, except for Oracle Java, which has some commercial features and comes with some restrictions on how to use the software.

Oracle Java license limits Java for personal and development use only. You cannot use it for commercial purposes. OpenJDK is free open source Java software that allows anyone from anywhere to use it freely as they see fit.

OpenJDK consists of Java Runtime Environment (JRE) and Java Development Kit (JDK).

The JRE consists of the Java Virtual Machine (JVM), classes, and binaries that allow you to run Java programs. JDK includes JRE and development / debugging tools and libraries required to create Java applications.

When you are ready to install both Java software, use one of the following methods: If you prefer to go with the open source version, install OpenJDK. If instead you want to install Oracle Java, use the second method below.

How to install OpenJDK on Ubuntu

OpenJDK is the open source version of Java. It is highly compatible with Ubuntu and if you don’t know which version of Java to install, you should select the open source version. At the time of writing, OpenJDK 17 It is the latest stable version that can be installed on Ubuntu.

OpenJDK (

To install OpenJDK 17 on Ubuntu, run the following commands. If a newer version is available from the link above, please install it instead.

sudo apt update
sudo apt install openjdk-17-jdk

Once OpenJDK is installed, you can verify and validate that Java is installed by running the following command:

java -version

That should show lines similar to the ones shown below:

openjdk version "17" 2021-09-14
OpenJDK Runtime Environment (build 17+35-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 17+35-Ubuntu-120.04, mixed mode, sharing)

OpenJDK is installed and validated.

If you also want to install Java JRE, part of the OpenJava suite, just run the following commands:

sudo apt update
sudo apt install openjdk-17-jre

If you only want to run the minimum Java Runtime, run the following commands:

sudo apt update
sudo apt install openjdk-17-jdk-headless

Some applications are not yet fully compatible with the latest version of OpenJDK. 17. For those, they can install the older Java LTS which was the version 8.

sudo apt update
sudo apt install openjdk-8-jdk

If you have multiple versions of Java installed, just use the commands below to set what the default should be for your system.

sudo update-alternatives --config java

You will be presented with a list of all installed Java versions. Enter the version number you want to use as the default and press Pay in

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

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

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

Some programs require that JAVA_HOME is configured in the system. You can set the default house using the lines above in the configuration file.

For Java 17, it shows / usr / lib / jvm / java-17-openjdk-amd64 and Java 8, it is / usr / lib / jvm / java-8-openjdk-amd64.

To configure your homes, run the following commands to open the system environment file.

sudo nano /etc/environment

Then add a line for Java 17 as shown below:


Run the following commands to save your changes.

source /etc/environment

Verify that the JAVA_HOME The environment variable was set correctly:


That should generate a line similar to the one shown below:


You should do that! You have successfully installed OpenJDK on Ubuntu Linux.

How to install Oracle JDK on Ubuntu

If you prefer to install Oracle JAVA instead of the previous open source version, please follow the steps below. At the time of writing, the latest version of Oracle Java is 17.

Download the latest version of Java SE Development Kit 17 LTS from the link below.

Download Oracle Java

Take notes from the version number you are downloading. If there is a version number newer than the following, select it instead.

You can also easily install the Java DEB package by running the following commands. At the time of writing, the latest current version of the Java JDK is jdk-17

You may need to replace the highlighted line in the commands below with the last one when available.

cd /tmp
wget --no-check-certificate -c --header  "Cookie: oraclelicense=accept-securebackup-cookie" ""

Now that you have downloaded the correct archive package for your system, run the following commands to install Oracle Java.

Again, the latest current version is jdk-17

sudo apt install ./jdk-17_linux-x64_bin.deb

After that, run the following commands to add Oracle Java as an alternative on Ubuntu. The following commands configure Ubuntu to use Java alternatives.

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1711

Now when you rerun the commands to list the available Java packages, you should see Oracle Java as an alternative.

sudo update-alternatives --config java

Oracle Java is number 4 on the list. You can enter it to be the default.

here are 4 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  4            /usr/lib/jvm/jdk-17/bin/java                     1711      manual mode

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

If you don’t have other versions of Java installed, the commands won’t return anything.

Then run the following commands to do Java 17 to be the default Java compiler for your Ubuntu desktop.

sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk-17/bin/jar 1711
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-17/bin/javac 1711
sudo update-alternatives --set jar /usr/lib/jvm/jdk-17/bin/jar
sudo update-alternatives --set javac /usr/lib/jvm/jdk-17/bin/javac

That should have Java installed and configured. To set the JAVA environment variables, create a new file in the /etc/profile.d directory for the Java JDK.

sudo nano /etc/profile.d/

Then copy and paste the lines at the end of the file and save it.

export J2SDKDIR=/usr/lib/jvm/jdk-17
export J2REDIR=/usr/lib/jvm/jdk-17
export PATH=$PATH:/usr/lib/jvm/jdk-17/bin:/usr/lib/jvm/jdk-17/db/bin
export JAVA_HOME=/usr/lib/jvm/jdk-17
export DERBY_HOME=/usr/lib/jvm/jdk-17/db

Then run the following commands

source /etc/profile.d/

The above commands should configure Java to work and work with Ubuntu. To test if Java is installed correctly, run the following commands.

Now you can verify that Java is installed and configured by generating your home directory.


It should generate a line similar to the following:



In this tutorial we have seen how to install Java on Ubuntu Linux. If you find any errors above or have something to add, use the comment form below.

Write A Comment