Ubuntu

How To Install OpenJDK On Ubuntu 24.04 LTS (Noble Numbat) : A Comprehensive Guide

On this article we will show you how to install OpenJDK on Ubuntu 24.04 LTS. Installing Java Development Kit (JDK) on an Ubuntu system is a relatively simple process, whether we hill use OpenJDK or Oracle JDK. By following this guide, you can ensure that your Java environment is set up correctly, allowing you to focus on developing and running your Java applications.  Remember to keep your Java installation up to date to benefit from the latest features and security improvements.

Prerequisites

Before you start, ensure you have the following:

  • A system running Ubuntu version 24.04.
  • Administrative privileges to install software.

The OpenJDK installation on Ubuntu 24.04 LTS will be consist of several step as mention below :

Step 1: Update Your Package Index
Step 2: Check for Existing Java Installations
Step 3: Install Java Development Kit (JDK)
Step 4: Verify the Installation
Step 5: Set JAVA_HOME Environment Variable

1. Update Ubuntu Package Index

Before installing any new software, it’s always a good practice to update your package index. Open a terminal and run following command line :

$ sudo apt update

2. Check for Existing Java Installations

It’s helpful to check if Java is already installed on your system and what versions are available. Use the following command:

$ java -version

Output :

ramansah@bckinfo:~$ java --version
Command 'java' not found, but can be installed with:
sudo apt install default-jre # version 2:1.17-75, or
sudo apt install openjdk-17-jre-headless # version 17.0.10~6ea-1
sudo apt install openjdk-11-jre-headless # version 11.0.21+9-0ubuntu1
sudo apt install openjdk-19-jre-headless # version 19.0.2+7-4
sudo apt install openjdk-20-jre-headless # version 20.0.2+9-1
sudo apt install openjdk-21-jre-headless # version 21.0.1+12-3
sudo apt install openjdk-22-jre-headless # version 22~22ea-1
sudo apt install openjdk-8-jre-headless # version 8u392-ga-1

java on ubuntu repository

From the output result above, we know if there is no openjdk installed on the system.

3. Install Java Development Kit (JDK)

Ubuntu repositories provide the OpenJDK (an open-source implementation of the Java Platform). To install the default JDK, we just submit following command line :

$ sudo apt install default-jdk

This command installs the latest version of OpenJDK available in the Ubuntu repositories. Output will be shown below :

ramansah@bckinfo:~$ sudo apt install default-jdk
[sudo] password for ramansah: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
ca-certificates-java default-jdk-headless default-jre default-jre-headless
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-21-jdk openjdk-21-jdk-headless openjdk-21-jre
openjdk-21-jre-headless x11proto-dev xorg-sgml-doctools xtrans-dev
Suggested packages:
libice-doc libsm-doc libx11-doc libxcb-doc libxt-doc openjdk-21-demo
openjdk-21-source visualvm fonts-ipafont-gothic fonts-ipafont-mincho
fonts-wqy-microhei | fonts-wqy-zenhei fonts-indic
The following NEW packages will be installed:
ca-certificates-java default-jdk default-jdk-headless default-jre
default-jre-headless 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-21-jdk
openjdk-21-jdk-headless openjdk-21-jre openjdk-21-jre-headless x11proto-dev
xorg-sgml-doctools xtrans-dev
0 upgraded, 24 newly installed, 0 to remove and 17 not upgraded.
Need to get 137 MB of archives.
After this operation, 317 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
. . .
update-alternatives: using /usr/lib/jvm/java-21-openjdk-amd64/bin/serialver to provide /usr/bin/serialver (serialver) in auto mode
update-alternatives: using /usr/lib/jvm/java-21-openjdk-amd64/bin/jhsdb to provide /usr/bin/jhsdb (jhsdb) in auto mode Setting up default-jre (2:1.21-75+exp1) ...
Setting up openjdk-21-jdk:amd64 (21.0.3+9-1ubuntu1) ...
update-alternatives: using /usr/lib/jvm/java-21-openjdk-amd64/bin/jconsole to provide /usr/bin/jconsole (jconsole) in auto mode
Setting up default-jdk-headless (2:1.21-75+exp1) ...
Setting up default-jdk (2:1.21-75+exp1) ...

Installing Specific Versions

If you need a specific version of Java, such as OpenJDK 11 or OpenJDK 17, you can specify the version number by submitting command line below :

$ sudo apt install openjdk-17-jre-headless
# or
$ sudo apt install openjdk-11-jre-headless

Below is an example installing OpenJDK version 17.

ramansah@bckinfo:~$ sudo apt install openjdk-17-jre-headless
[sudo] password for ramansah: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei
| fonts-wqy-zenhei fonts-indic
The following NEW packages will be installed:
openjdk-17-jre-headless
0 upgraded, 1 newly installed, 0 to remove and 17 not upgraded.
Need to get 47.9 MB of archives.
After this operation, 192 MB of additional disk space will be used.
Get:1 http://id.archive.ubuntu.com/ubuntu noble/main amd64 openjdk-17-jre-headless amd64 17.0.11+9-1 [47.9 MB]
Fetched 47.9 MB in 40s (1,182 kB/s) 
Selecting previously unselected package openjdk-17-jre-headless:amd64.
(Reading database ... 149195 files and directories currently installed.)
Preparing to unpack .../openjdk-17-jre-headless_17.0.11+9-1_amd64.deb ...
Unpacking openjdk-17-jre-headless:amd64 (17.0.11+9-1) ...
Setting up openjdk-17-jre-headless:amd64 (17.0.11+9-1) ...
Processing triggers for ca-certificates-java (20240118) ...
done.

4. Verify the Installation

After installation, verify that Java is correctly installed by checking the version again:

$ java --version

Output :

ramansah@bckinfo:~$ java --version
openjdk 21.0.3 2024-04-16
OpenJDK Runtime Environment (build 21.0.3+9-Ubuntu-1ubuntu1)
OpenJDK 64-Bit Server VM (build 21.0.3+9-Ubuntu-1ubuntu1, mixed mode, sharing)

openjdk 21 on ubuntu 2404

OpenJDK Version 21.0.3 is default Java version on Ubuntu 24.04.

5 .Set JAVA_HOME Environment Variable

Setting the JAVA_HOME environment variable is essential for some applications and development tools. To move to another Java version we just submit the command line :

$ sudo update-alternatives --config java

Output :

ramansah@bckinfo:~$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 auto mode
1 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
2 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode

Then we will select number ‘1’ to change existing Java environment. And then we will check the default java environment after resetting, as shown below :

ramansah@bckinfo:~$ java --version
openjdk 17.0.11 2024-04-16
OpenJDK Runtime Environment (build 17.0.11+9-Ubuntu-1)
OpenJDK 64-Bit Server VM (build 17.0.11+9-Ubuntu-1, mixed mode, sharing)
ramansah@bckinfo:~$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 auto mode
* 1 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
2 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode

Until here, we have installed OpenJDK from Ubuntu 24.04 default repository successfully and as additional we also installed OpenJDK versio 17.0 then switch between them.

Conclusion

Installing Java on an Ubuntu operating system is a straightforward process, but ensuring you get the right version and configuration can make a significant difference in your development environment.

(Visited 104 times, 2 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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