Install Java with DNF on AlmaLinux 9: Comprehensive Guide
In this guide, we want to teach you how to Install Java with DNF on AlmaLinux 9 (Default JRE and JDK). You need Java for many kinds of apps and software. To set up Java, manage multiple Java versions, and set up a Java home environment path, you can follow the steps below. The default version of Java on AlmaLinux 9 is Java 11. This tutorial will help you successfully Install Java with DNF on AlmaLinux 9.
To Install Java with DNF on AlmaLinux 9, you must have access to your server as a non-root user with sudo privileges. For this purpose, you can visit the Initial Server Setup with AlmaLinux 9. Now follow the steps below to Install Java with DNF on AlmaLinux 9.
Step 1 – Run System Update on AlmaLinux 9
First, you must update your local package index with the command below:
sudo dnf update -y
Then, check that you have Java installed on your server by checking its version:
java -version
If Java is not currently installed, you’ll receive the following output:
**Output**
-bash: java: command not found
Step 2 – Install Default JRE and JDK on AlmaLinux 9
At this step of Install Java with DNF on AlmaLinux 9, you can use DNF to install the default JRE from OpenJDK 11:
sudo dnf install java-openjdk -y
When your installation is completed, you can verify it by checking its version:
java -version
In your output, you should see:
**Output**
openjdk version "11.0.19" 2023-04-18 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.19.0.7-2) (build 11.0.19+7-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.19.0.7-2) (build 11.0.19+7-LTS, mixed mode, sharing)
To compile and run some Java-based software, you may need JDK. To install the JDK, run the following command on AlmaLinux 9, which will also install the JRE:
sudo dnf install java-11-openjdk-devel -y
Verify that the JDK is installed by checking the version of Javac which is the Java compiler:
javac -version
In your output, you should see:
**Output**
javac 11.0.19
Step 3 – Manage Multiple Java Installation on AlmaLinux 9
At this point, you can configure which Java version is the default for use if you have multiple Java installations on your server. To do this, you can use the following command in your command line:
sudo update-alternatives --config java
In my case, I get the following output:
**Output**
There is 1 program that provides 'java'.
Selection Command
-----------------------------------------------
*+ 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-4.el9.alma.x86_64/bin/java)
Enter to keep the current selection[+], or type selection number:
Here I have 1 choice for Java. If you have multiple choices you can type your selection number or press enter to keep the current choice.
Also, you can do this for your Java compiler, to do this, you can run the command below:
sudo update-alternatives --config javac

Step 4 – Set up Java Home Environment Variable on AlmaLinux 9
As you know, programs that are written in Java will use the Java_Home environment variable to find which Java installation location. To do this, you need to know where your Java is installed. You can check it by using the command below:
sudo update-alternatives --config java
As you can see from the output, OpenJDK 11 is installed under the following path:
/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-4.el9.alma.x86_64/bin/java
Next, open the /etc/environment file with your favorite text editor, we use the vi editor:
sudo vi /etc/environment
Note: This file may be blank. At the end of the file, add the following line, making sure to replace the path with your own, and not include the bin/ portion of the path:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-4.el9.alma.x86_64"
When you are done, save and close the file.
Reload the file to apply the changes with the following command:
source /etc/environment
Finally, verify your Java Home environment variable path on AlmaLinux 9 with the command below:
echo $JAVA_HOME
**Output**
/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-4.el9.alma.x86_64
Note: Other users will need to run the source /etc/environment command or log out and log back in to apply this setting.
That’s it. You are done.
How to install Java on RHEL 9?
As you know Almalinux 9 is based on RHEL. So you can simply use this installation guide steps for any RHEL 9 distros such as Rocky Linux 9.
Conclusion
At this point, you have learned to Install Java with DNF on AlmaLinux 9 (default JRE and JDK). Also, you have learned to set up the Java_Home environment path and manage multiple Java versions.
Hope you enjoy it. You may also be interested in these articles:
Install OpenJDK 17 on AlmaLinux 9
Create a Local Repository on AlmaLinux 9
Differences between YUM and DNF package managers
Alternative Solutions for Installing Java on AlmaLinux 9
While the DNF package manager provides a straightforward way to install Java on AlmaLinux 9, alternative methods exist that can offer flexibility or cater to specific use cases. Here are two such alternatives:
1. Using SDKMAN! (Software Development Kit Manager)
SDKMAN! is a command-line tool for managing parallel versions of multiple Software Development Kits (SDKs) on most Unix-like systems. It simplifies installing, switching between, and managing different Java Development Kits (JDKs).
Explanation:
SDKMAN! provides a convenient way to install and manage different JDK versions without interfering with system-level configurations. This is particularly useful when you need to work with multiple Java versions for different projects. It installs the JDKs in its own directory structure within the user’s home directory, keeping them isolated from the system’s default Java installation.
Installation and Usage:
-
Install SDKMAN!:
curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh"
-
List Available Java Versions:
sdk list java
This will display a list of available Java versions from different vendors like Oracle, OpenJDK, GraalVM, etc.
-
Install a Specific Java Version:
Let’s say you want to install version 21 of the BellSoft Liberica JDK. Find the identifier in the
sdk list java
output (e.g.,21.0.2.fx-librca
). Then use the following command:sdk install java 21.0.2.fx-librca
-
Set a Java Version as Default:
sdk default java 21.0.2.fx-librca
-
Verify Installation:
java -version
This should display the version of the Java SDK you installed using SDKMAN!.
Code Example:
Here’s an example of switching between Java versions using SDKMAN!:
# List installed Java versions
sdk list java installed
# Switch to Java 17 (assuming it's installed with identifier 17.0.9-amzn)
sdk use java 17.0.9-amzn
# Verify the switch
java -version
2. Manual Installation from Archive (tar.gz)
Another approach is to download a Java JDK archive (tar.gz) directly from the vendor’s website (e.g., Oracle, Azul, Eclipse Temurin) and manually install it.
Explanation:
This method provides the greatest control over the installation process. It’s useful when you need a specific JDK version that is not available through the package manager or SDKMAN!, or when you want to customize the installation location. However, it requires more manual configuration.
Installation and Usage:
-
Download the JDK Archive:
Download the appropriate tar.gz archive for your architecture from the vendor’s website. For example, you might download
jdk-21_linux-x64_bin.tar.gz
from Oracle. -
Extract the Archive:
tar -xvf jdk-21_linux-x64_bin.tar.gz
-
Move the Extracted Directory:
Move the extracted directory to a suitable location, such as
/opt/java/
:sudo mv jdk-21 /opt/java/
-
Set Environment Variables:
Modify the
/etc/environment
file to set theJAVA_HOME
variable:sudo vi /etc/environment
Add the following line (adjust the path as needed):
JAVA_HOME="/opt/java/jdk-21"
Add the Java
bin
directory to thePATH
variable in the same file:PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/java/jdk-21/bin"
Save the file and exit the editor.
-
Reload Environment Variables:
source /etc/environment
-
Configure Alternatives (Optional):
If you want to make this Java version the system default, you can use the
update-alternatives
command:sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk-21/bin/java" 1 sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java/jdk-21/bin/javac" 1 sudo update-alternatives --config java sudo update-alternatives --config javac
-
Verify Installation:
java -version javac -version
Code Example:
Here’s an example of setting the JAVA_HOME variable in /etc/environment
using sed
command, which can be useful in automated scripts:
# Ensure JAVA_HOME is not already set, remove it if it is
sudo sed -i '/JAVA_HOME=/d' /etc/environment
# Set the JAVA_HOME variable using sed
echo 'JAVA_HOME="/opt/java/jdk-21"' | sudo tee -a /etc/environment
# Ensure PATH includes the Java bin directory
if ! grep -q "/opt/java/jdk-21/bin" /etc/environment; then
sudo sed -i 's/PATH="/PATH:/opt/java/jdk-21/bin"/' /etc/environment
else
echo "Java bin directory already in PATH"
fi
# Reload environment variables
source /etc/environment
# Verify
echo $JAVA_HOME
These alternative methods provide flexibility in managing Java installations on AlmaLinux 9, catering to different needs and preferences. The DNF method is the simplest for basic installations, while SDKMAN! and manual installations offer more control and customization options.