How to Install Java (OpenJDK 18, 17, 11 and 8) on Ubuntu 22.04

On this short article, we will learn how to install OpenJDK 18, 17,11 and 8 on Ubuntu 22.04 LTS operating system

Introduction

Java is one of the most cross-platform, object-oriented, and multipurpose programming language which is mainly used on this age. By using Java, we can create mobile, web, and cloud applications and also we can use Java to create games, chatbots, enterprise applications, and many more. So far, Java 17 is the latest LTS version which is ready for installation. JDK packages provide the full Java development libraries for the development systems. To run a Java application, we only need a Java runtime environment (JRE).  On this article we will learn how to install OpenJDK 18, OpenJDK 17, OpenJDK 11 and OpenJDK 8 on Ubuntu 22.04 LTS operating system.

Prerequisites

Before we are going to install several version of OpenJDK on Ubuntu 22.04, On this short article we will install OpenJDK 8, OpenJDK 11, OpenJDK 17 and OpenJDK 18. There are several prerequisite to be fulfilled, namely :

  • an Ubuntu 22.04 LTS erver
  • user account with sudo privilege
  • sufficient disk space and good internet connection

1. Install OpenJDK 11 On Ubuntu 22.04

Update System

Before starting installation, we need our local Ubuntu repositories packages. By updating our Ubuntu system, it will refresh the local list of available packages software to the newer stable version. To update Ubuntu 22.04 LTS repositories, we will use the command line below.

$ sudo apt update

Output :

ramans@bckinfo:~$ sudo apt update
[sudo] password for ramans: 
Hit:1 http://id.archive.ubuntu.com/ubuntu jammy InRelease 
Get:2 http://id.archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB] 
Get:3 http://id.archive.ubuntu.com/ubuntu jammy-backports InRelease [99,8 kB] 
Get:4 http://id.archive.ubuntu.com/ubuntu jammy-updates/main i386 Packages [267 kB]
. . .

Installing Java 11

To install OpenJDK 11 on Ubuntu 22.04 server or workstation machine, we will use the following command line.

$ sudo apt install openjdk-11-jdk

Ouput :

ramans@bckinfo:~$ sudo apt install openjdk-11-jdk
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
ca-certificates-java fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libice-dev
libpthread-stubs0-dev libsm-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-11-jdk-headless
openjdk-11-jre openjdk-11-jre-headless x11proto-dev xorg-sgml-doctools xtrans-dev
Suggested packages:
default-jre libice-doc libsm-doc libx11-doc libxcb-doc libxt-doc openjdk-11-demo openjdk-11-source visualvm
fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei
The following NEW packages will be installed:
ca-certificates-java fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libice-dev
libpthread-stubs0-dev libsm-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-11-jdk
openjdk-11-jdk-headless openjdk-11-jre openjdk-11-jre-headless x11proto-dev xorg-sgml-doctools xtrans-dev
0 upgraded, 20 newly installed, 0 to remove and 193 not upgraded.
Need to get 261 MB of archives.
After this operation, 412 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...
Processing triggers for sgml-base (1.30) ...
Setting up x11proto-dev (2021.5-1) ...
Setting up libxau-dev:amd64 (1:1.0.9-1build5) ...
Setting up libice-dev:amd64 (2:1.0.10-1build2) ...
Setting up libsm-dev:amd64 (2:1.2.3-1build2) ...
Setting up libxdmcp-dev:amd64 (1:1.1.3-0ubuntu5) ...
Setting up libxcb1-dev:amd64 (1.14-3ubuntu3) ...
Setting up libx11-dev:amd64 (2:1.7.5-1) ...
Setting up libxt-dev:amd64 (1:1.2.1-1) ...

Verify OpenJDK 11 Instalation

After installation was completed done, then we will verify the installation by submitting command line :

$ java -version

Output :

ramans@bckinfo:~$ java -version
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)

2. Install OpenJDK 17 On Ubuntu 22.04

Install OpenJDK 17

On this stage we will install OpenJDK 17 on Ubuntu 22.04 LTS operating system. To install OpenJDK 17 on Ubuntu 22.04 server or workstation machine we will use the following command line.

$ sudo apt install openjdk-17-jdk

Output :

ramans@bckinfo:~$ sudo apt install openjdk-17-jdk 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
openjdk-17-jdk-headless openjdk-17-jre openjdk-17-jre-headless
Suggested packages:
openjdk-17-demo openjdk-17-source visualvm fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei
The following NEW packages will be installed:
openjdk-17-jdk openjdk-17-jdk-headless openjdk-17-jre openjdk-17-jre-headless
0 upgraded, 4 newly installed, 0 to remove and 193 not upgraded.
Need to get 267 MB of archives.
After this operation, 417 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
. . .
Setting up openjdk-17-jdk:amd64 (17.0.4+8-1~22.04) ...
update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/jconsole to provide /usr/bin/jconsole (jconsole) in auto mode
Processing triggers for desktop-file-utils (0.26-1ubuntu3) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...

Verify OpenJDK 17 Instalation

After installation was completed done, then we will verify the installation by submitting command line :

$ java -version

Output :

ramans@bckinfo:~$ java -version
openjdk version "17.0.4" 2022-07-19
OpenJDK Runtime Environment (build 17.0.4+8-Ubuntu-122.04)
OpenJDK 64-Bit Server VM (build 17.0.4+8-Ubuntu-122.04, mixed mode, sharing)

3. Install OpenJDK 8 On Ubuntu 22.04

Install OpenJDK 8

On this stage we will install OpenJDK 8 on Ubuntu 22.04 LTS operating system. To install OpenJDK 8 on Ubuntu 22.04 server or workstation machine we will use the following command line.

$ sudo apt install openjdk-8-jdk

Output :

ramans@bckinfo:~$ sudo apt install openjdk-8-jdk 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
openjdk-8-jdk-headless openjdk-8-jre openjdk-8-jre-headless
Suggested packages:
openjdk-8-demo openjdk-8-source visualvm fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei fonts-wqy-zenhei
The following NEW packages will be installed:
openjdk-8-jdk openjdk-8-jdk-headless openjdk-8-jre openjdk-8-jre-headless
0 upgraded, 4 newly installed, 0 to remove and 193 not upgraded.
Need to get 43,8 MB of archives.
After this operation, 148 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
. . .Setting up openjdk-8-jdk:amd64 (8u342-b07-0ubuntu1~22.04) ...
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/bin/appletviewer to provide /usr/bin/appletviewer (appletviewer) in auto mode
Processing triggers for desktop-file-utils (0.26-1ubuntu3) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...
Processing triggers for libc-bin (2.35-0ubuntu3) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...

Verify OpenJDK 8 Instalation

After installation was completed done, then we will verify the installation by submitting command line :

$ java -version

Output :

ramans@bckinfo:~$ java -version
openjdk version "1.8.0_342"
OpenJDK Runtime Environment (build 1.8.0_342-8u342-b07-0ubuntu1~22.04-b07)
OpenJDK 64-Bit Server VM (build 25.342-b07, mixed mode)

4. Install OpenJDK 18 On Ubuntu 22.04

Install OpenJDK 18

OpenJDK 18 (openjdk-18-jdk) package, is a non-LTS release yet until this tutorial is being made. To install OpenJDK 18 on Ubuntu 22.04 server or workstation machine we will use the following command line.

$ sudo apt install openjdk-18-jdk

Output :

ramans@bckinfo:~$ sudo apt install openjdk-18-jdk 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
openjdk-18-jdk-headless openjdk-18-jre openjdk-18-jre-headless
Suggested packages:
openjdk-18-demo openjdk-18-source visualvm fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei
| fonts-wqy-zenhei
The following NEW packages will be installed:
openjdk-18-jdk openjdk-18-jdk-headless openjdk-18-jre openjdk-18-jre-headless
0 upgraded, 4 newly installed, 0 to remove and 193 not upgraded.
Need to get 273 MB of archives.
After this operation, 426 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
. . .Setting up openjdk-18-jdk:amd64 (18.0.2+9-2~22.04) ...
update-alternatives: using /usr/lib/jvm/java-18-openjdk-amd64/bin/jconsole to provide /usr/bin/jconsole (jconsole) in auto mode
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...
Processing triggers for desktop-file-utils (0.26-1ubuntu3)

Verify OpenJDK 18 Instalation

After installation was completed done, then we will verify the installation by submitting command line :

$ java -version

Output :

ramans@bckinfo:~$ java -version
openjdk version "18.0.2-ea" 2022-07-19
OpenJDK Runtime Environment (build 18.0.2-ea+9-Ubuntu-222.04)
OpenJDK 64-Bit Server VM (build 18.0.2-ea+9-Ubuntu-222.04, mixed mode, sharing)

5. Change Default Java Version on Ubuntu

Currenty, on our system we have had 4 OpenJDK version, namely : OpenJDK 11, 17, 18 and 8.  By this condition, we will have a tools for changing default Java version suited with our needs. To chane default Java version, we will do the following command line :

$ sudo update-alternatives --config java

Output :

ramans@bckinfo:~$ sudo update-alternatives --config java 
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-17-openjdk-amd64/bin/java 1711 manual mode
3 /usr/lib/jvm/java-18-openjdk-amd64/bin/java 1811 manual mode
* 4 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode

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

On the example above, we have default java version on OpenJDK 8, then we will switch it to OpenJDK 18 (number 3), by entering ‘3’ on the menu. The result is as shown below :

ramans@bckinfo:~$ sudo update-alternatives --config java 
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-17-openjdk-amd64/bin/java 1711 manual mode
* 3 /usr/lib/jvm/java-18-openjdk-amd64/bin/java 1811 manual mode
4 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode

Conclusion

On this article, we have learnt how to install Java (OpenJDK 11, OpenJDK 18, OpenJDK 17 and OpenJDK 8) on Ubuntu 22.04 LTS operating system. I hope this article will be helpful.

 

 

 

Add a Comment

Your email address will not be published. Required fields are marked *