How to Install Java 21 on AlmaLinux 10

How to Install Java 21 on AlmaLinux 10

Java 21 is the Java platform’s Long-Term Support (LTS) release. As a high-level, object-oriented, class-based programming language used for developing mobile and desktop applications, it can run on all major operating systems. Java is designed with the philosophy of:
“Write Once, Run Anywhere” (WORA) allows you to write Java code on one platform and run it on any other platform that supports the Java Virtual Machine (JVM) without modifying the source code. This tutorial will show you how to install Java 21 on AlmaLinux 10.

Prerequisites

  • An AlmaLinux 10 server.
  • Access to the root user account (or access to an admin account with root privileges)

Step 1. Update System Packages

First, log in to your Almalinux 10 server via SSH as the root user:

ssh root@IP_Address -p Port_number


Replace ‘IP_Address’ and ‘Port_number’ with your server’s IP address and SSH port number. If needed, replace ‘root’ with the username of your sudo account.

Once logged in, you must ensure that all AlmaLinux OS packages installed on the server are up to date. You can do this by running the following commands:

dnf update && dnf upgrade

More about Java

Before we show you the three ways you can install Java 21 on Almalinux, let’s explain the differences between the installations and the terms used, such as JRE, JDK, and JVM.


JVM: Java Virtual Machine – is the engine that runs Java bytecode. As previously mentioned, once you write Java code on one platform, you can run it on any platform that supports Java.

JRE stands for Java Runtime Environment. It’s a package that includes the JVM and the core libraries needed to run Java applications. Using JRE, you can only run Java applications, but you can’t develop them as it does not include the Java compiler, build, or debug tools.

JDK: Java Development Kit is a complete package for Java Developers that includes JRE + development tools.

  • Java Oracle JDK: the official Java SE developed by Sun MicroSystems was acquired by Oracle in 2009 and became Oracle Java or Oracle JDK.
  • Java OpenJDK: an open-source version of the Oracle JDK maintained by the community.

Step 2. Installing Java 21

Java is omitted by default in AlmaLinux 10, so you can install the OpenJDK Java 21 package using the following commands:

dnf install java-21-openjdk java-21-openjdk-devel

Once completed, you can check the Java version with the command:

# java --version
openjdk 21.0.4 2024-07-16 LTS
OpenJDK Runtime Environment (Red_Hat-21.0.4.0.7-1) (build 21.0.4+7-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.4.0.7-1) (build 21.0.4+7-LTS, mixed mode, sharing)

If you want to install Java OpenJDK from the Adoptium (Eclipse Temurin) project, you can also check their guide at: https://adoptium.net/installation/linux/

If you want to install Java Oracle JDK, you can use the following commands:

wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.rpm
rpm -i jdk-21_linux-x64_bin.rpm

Once installed, you can check the Java version with the command:

java --version
java 21.0.6 2025-01-21 LTS
Java(TM) SE Runtime Environment (build 21.0.6+8-LTS-188)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.6+8-LTS-188, mixed mode, sharing)

Switch Between Different Java Versions

If you have installed multiple versions of Java from different sources, such as OpenJDK and Oracle, you can easily set the default version using the update-alternatives command.

To set the Java default version from Oracle Java 21 to Java OpenJDK 21, run the following command:

update-alternatives --config java

You should see a list of all installed Java versions in the following output:

# update-alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*  1           /usr/lib/jvm/jdk-21.0.6-oracle-x64/bin/java
 + 2           java-21-openjdk.x86_64 (/usr/lib/jvm/java-21-openjdk-21.0.4.0.7-1.el10.x86_64/bin/java)

Enter to keep the current selection[+], or type selection number:

Type 2 and hit Enter to set Java 21 OpenJDK as the default Java version, and then if you run java – version again, you should see the Java version has changed:

# java --version
openjdk 21.0.4 2024-07-16 LTS
OpenJDK Runtime Environment (Red_Hat-21.0.4.0.7-1) (build 21.0.4+7-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.4.0.7-1) (build 21.0.4+7-LTS, mixed mode, sharing)

Set JAVA_HOME Environment Variable

You must set the JAVA_HOME environment variable to define the Java path in some Java applications.

First, find the Java installation path with the following command:

update-alternatives --config java

You should see the path of all Java versions in the following output:

There are 2 programs which provide 'java'.
  
Selection    Command
-----------------------------------------------
*  1           /usr/lib/jvm/jdk-21.0.6-oracle-x64/bin/java
 + 2           java-21-openjdk.x86_64 (/usr/lib/jvm/java-21-openjdk-21.0.4.0.7-1.el10.x86_64/bin/java)

Enter to keep the current selection[+], or type selection number:

Next, copy your desired Java version path from the above output and paste it in the /etc/environment file:

nano /etc/environment

Add the following line for Java 21:

JAVA_HOME="/usr/lib/jvm/jdk-21.0.6-oracle-x64/bin/java"

Next, activate the Java environment variable with the following command:

source /etc/environment

You can now verify the JAVA_HOME environment variable with the following command:

echo $JAVA_HOME

You should get the following output:

/usr/lib/jvm/jdk-21.0.6-oracle-x64/bin/java

Conclusion

In the above guide, you learned how to install Java 21 on an AlmaLinux 10 server.

Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services. In that case, you can ask our expert Linux admins to set it up for you. They are available 24/7 and will take care of your request immediately.

If you liked this post, please share it with your friends or leave a comment below.

Leave a Comment