Install Java 21 LTS (OpenJDK 21) on AlmaLinux | CentOS | RHEL 10

In September 2023, Oracle announced the General Availability of Java 21, which comes bundled with tons of intriguing new features. OpenJDK 21 LTS is an implementation of the Java SE 21 platform licensed under GNU GPL version 2. The binary packages for installation are available for download on the Oracle official website. The feature list with all the 15 JEPS (Java Enhancement Proposals) can be accessed on OpenJDK 21 features. JEPS marked as preview means that you can’t use them in your application, but you must enable a preview mode first to use them. Please take note of this.

Java is a class-based object-oriented programming language developed by James Gosling in 1995. It is a high-level programming language running in more than 3 billion devices world wide. It is cross-platform and easily deployable in Windows, Linux and macOS. The JDK ( Java Development Kit ) contains the necessary requirements to effectively write, run and compile a Java program. It includes a Java compiler, javac, the primary java compiler, a runtime environment called Java Virtual Machine (JVM) and other development tools and libraries. To run Java programs, JDK kit must be installed in your system. Everyone can download and install Java because it is free to download.

This guide will demonstrate how to install Java JDK 21 on AlmaLinux, CentOS Stream and RHEL 10. These are open-source enterprise systems designed to be bug-for-bug compatible with Red Hat Enterprise Linux®. Let us get started.

These two methods are commonly used to install Java JDK 21 the latest long-term support on RHEL-based distributions:

  1. Via YUM repositories.
  2. Via Oracle JDK binaries.

In this guide, I will demonstrate using the two methods.

Method 1: Install OpenJDK 21 from YUM repositories

Begin the installation process by updating the system packages.

$ cat /etc/os-release
NAME="AlmaLinux"
VERSION="10.0 (Purple Lion)"
ID="almalinux"
ID_LIKE="rhel centos fedora"
VERSION_ID="10.0"
PLATFORM_ID="platform:el10"
PRETTY_NAME="AlmaLinux 10.0 (Purple Lion)"
ANSI_COLOR="0;34"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:almalinux:almalinux:10::baseos"
HOME_URL="https://almalinux.org/"
DOCUMENTATION_URL="https://wiki.almalinux.org/"
VENDOR_NAME="AlmaLinux"
VENDOR_URL="https://almalinux.org/"
BUG_REPORT_URL="https://bugs.almalinux.org/"

ALMALINUX_MANTISBT_PROJECT="AlmaLinux-10"
ALMALINUX_MANTISBT_PROJECT_VERSION="10.0"
REDHAT_SUPPORT_PRODUCT="AlmaLinux"
REDHAT_SUPPORT_PRODUCT_VERSION="10.0"
SUPPORT_END=2035-06-01

Let’s check if OpenJDK 21 is available:

sudo dnf search 'openjdk 21'

Then install Java 21 using the following command:

sudo dnf install java-21-openjdk java-21-openjdk-devel -y

The command sample output:


Installed:
  java-21-openjdk-1:21.0.7.0.6-1.el10.alma.1.x86_64             java-21-openjdk-devel-1:21.0.7.0.6-1.el10.alma.1.x86_64             java-21-openjdk-headless-1:21.0.7.0.6-1.el10.alma.1.x86_64
  javapackages-filesystem-6.4.0-1.el10.noarch                   lksctp-tools-1.0.21-1.el10.x86_64                                   mkfontscale-1.2.2-8.el10.x86_64
  ttmkfdir-3.0.9-72.el10.x86_64                                 tzdata-java-2025b-1.el10.noarch                                     xorg-x11-fonts-Type1-7.5-40.el10.noarch

Complete!

Confirm the Java version installed on the system:

$ java -version
openjdk version "21.0.7" 2025-04-15 LTS
OpenJDK Runtime Environment (Red_Hat-21.0.7.0.6-1) (build 21.0.7+6-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.7.0.6-1) (build 21.0.7+6-LTS, mixed mode, sharing)

Method 2: Using Oracle JDK 21 from binary

To download the Java 21 binary, navigate to Oracle Java Downloads and grab the latest rpm release for JDK 21 for Linux systems.

sudo dnf install curl vim
curl -L -b "oraclelicense=a" -O https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.rpm

The next step is to install Java 21 on the system using the rpm command. Execute the command below.

sudo rpm -Uvh jdk-21_linux-x64_bin.rpm 

Sample Output:

The package is already installed on my system. Your output should be different.

After the installation pro cess is complete, check the version installed.

$ java -version
cloudspinx@CentOS-10:~$ java -version
java version "21.0.7" 2025-04-15 LTS
Java(TM) SE Runtime Environment (build 21.0.7+8-LTS-245)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.7+8-LTS-245, mixed mode, sharing)

The default Java installation path is /usr/lib/jvm/. To see the contents of the file, use the list command.

$ ls /usr/lib/jvm/jdk-21.0.7-oracle-x64/
bin  conf  include  jmods  legal  lib  LICENSE  man  README  release

Setup the default Java version on your system.

If you have more than one version installed in your system, you can choose the default version to use by the following commands:

List all Java versions installed in your system using the command below:

sudo alternatives --config java
java -version

Sample Output:

To select the default program, type the selection number and press enter.

Setting Java environment variables

Setting environment variables helps pass configuration information to applications.  Setting your system to use the Java 21 environment is a simple process. To achieve this, issue the command below.

cat << 'EOF' >> ~/.bashrc
export JAVA_HOME=$(readlink -f $(which java) | sed 's:/bin/java::')
export PATH=$JAVA_HOME/bin:$PATH
EOF

Make the environment active by using the following command:

source ~/.bashrc

Examine Java variables using the set of commands below:

$ echo $JAVA_HOME
/usr/lib/jvm/jdk-openjdk

$ echo $PATH
/usr/lib/jvm/java-21-openjdk/bin:/home/cloudspinx/.local/bin:/home/cloudspinx/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

Sample Output:

Run a simple Java program for demonstration

I will write a simple HelloWorld program for demonstration purposes. Use your text editor to create a Java configuration file, as shown below.

vim helloworld.java

Add the following parameters to the file:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello\nWorld!Java is fun");
    }
}

Compile and run your application with this command:

$ java helloworld.java
Hello
World!Java is fun

Demo – use of JEP preview feature

The code above works and only demonstrates how we write a simple Hello World program. I will now take advantage of the new features and do a simple illustration.

Using the text editor, add the following Java code.

vim application.java

Add the code below.

void main() {
System.out.println("Hello World!");
}

Running and compiling this code will result in an error, as shown below.

$ javac application.java
application.java:1: error: unnamed classes are a preview feature and are disabled by default.
void main() {
^
  (use --enable-preview to enable unnamed classes)
1 error

The error reads: “Unnamed classes are a preview feature and are disabled by default“. We are therefore to enable a preview feature for the code to work.

The good thing is that the code offers some help on what we need to do i.e “use –enable-preview to enable unnamed classes

Let’s run and compile the code again using the proposed solution. Execute the code specifying the use of Java 21 as below.

$ javac --release 21 --enable-preview application.java
##Output of the command
Note: application.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.

Now issue the command below.

$ java --enable-preview application
Hello World!

Congratulations! This code compiles and runs a Java application in Java 21 while showcasing the usage of preview features.

Wrap up

That concludes our guide on installing Java JDK 21 LTS (OpenJDK 21) on RHEL, AlmaLinux, and CentOS Stream 10. Java remains a popular programming language due to its cross-platform features. It is common among developers because it is a general-purpose programming language, allowing them to write an application once and run it anywhere. I hope your installation was successful.

See more guides:

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Unlock the Right Solutions with Confidence

At CloudSpinx, we don’t just offer services - we deliver clarity, direction, and results. Whether you're navigating cloud adoption, scaling infrastructure, or solving DevOps challenges, our seasoned experts help you make smart, strategic decisions with total confidence. Let us turn complexity into opportunity and bring your vision to life.

Leave a Comment

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

Related Post

Java has become one of the prominent programming languages in software application development. To be able to write, compile and […]

Upgrading to a newer version of FreeBSD can actually feel like a frightening and, in many ways, intimidating ordeal for […]

AlmaLinux is an open-source, community-driven, and cost-free distribution of the Linux operating system that is compatible with RHEL (Red Hat […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.