How to Install Java OpenJDK and Oracle JDK on Ubuntu 22.04

Java is one of the most powerful programming languages. It is very popular among web developers due to its simplicity and robustness. Generally, it is used to develop computer, video, and mobile games.

OpenJDK is an open-source Java edition and is available for free use. It supports multiple operating systems like Windows, Linux, Solaris, macOS, etc.

Oracle Java is a programming language developed by Oracle corporation. It gives customers enterprise features that minimize the costs of deploying and maintaining their Java-based applications.

This post will show you how to install OpenJDK and Oracle JDK on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 22.04.
  • A root password is configured on the server.

Install OpenJDK on Ubuntu

At the time of writing this article, Java 18 is the latest LTS release available for installation. Ubuntu 22.04 default repository provides the OpenJDK version 18, 17, 11, and 8 packages. You can use the APT package manager to install your desired Java version on your system.

To install OpenJDK 8, run the following command:

apt install openjdk-8-jdk

To install OpenJDK 11, run the following command:

apt install openjdk-11-jdk

To install OpenJDK 18, run the following command:

apt install openjdk-18-jdk

After the installation, verify the active version of OpenJDK using the following command:

java -version

You should see the following output:

openjdk version "18-ea" 2022-03-22
OpenJDK Runtime Environment (build 18-ea+36-Ubuntu-1)
OpenJDK 64-Bit Server VM (build 18-ea+36-Ubuntu-1, mixed mode, sharing)

Install Oracle JDK

At the time of writing this article, Oracle Java 18 and Java 17 are available to download without any commercial license. First, install the required dependencies using the following command:

apt install -y libc6-x32 libc6-i386

Next, visit the Oracle download page and download the latest version of Oracle JDK with the following command:

wget https://download.oracle.com/java/18/latest/jdk-18_linux-x64_bin.deb

Once the download is completed, install the downloaded version using the following command:

apt install ./jdk-18_linux-x64_bin.deb

Once installed, you can set the Oracle JDK 18 as the default version using the following command:

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-18/bin/java 100

Switch Between Different Java Versions

If multiple Java versions are installed on your server, you can easily switch between Java versions in your system as per your requirement.

To change the default Java version, run the following command:

update-alternatives --config java

You will be asked to set the default Java version as shown below:

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

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-18-openjdk-amd64/bin/java      1811      auto mode


  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-18-openjdk-amd64/bin/java      1811      manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  4            /usr/lib/jvm/jdk-18/bin/java                     100       manual mode

Press  to keep the current choice[*], or type selection number: 4

Type 4 and press the Enter key to set the Oracle JDK as the default Java version:

Next, verify the default Java version using the following command:

java -version

You should see the following output:

java version "18.0.2" 2022-07-19
Java(TM) SE Runtime Environment (build 18.0.2+9-61)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.2+9-61, mixed mode, sharing)

Conclusion

In this post, we explained how to install OpenJDK and Oracle JDK on Ubuntu 22.04. I hope you can now easily install your preferred Java versions on your server. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

1 Comment(s)

Add comment

Comments

By: Mike at: 2022-08-04 12:48:50

Hello sir,

I am trying to install OpenJDK on Ubuntu 20.04. When I execute 'apt install openjdk-18-jdk' it produces the error:

"Unable to locate package openjdk-18-jdk"

On the Ubutu packages website (https://packages.ubuntu.com/) I cannot find a package named 'openjdk-18-jdk' for Focal. Do you have any suggestions how I can move forward with installing this? 

Thank you for your article and any support you can provide. Mike