Install Java with DNF on AlmaLinux 9

In this guide, we want to teach you to Install Java (Default JRE and JDK) with DNF Package Manager on AlmaLinux 9. 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.

Complete Guide To Install Java with DNF on AlmaLinux 9

To complete this guide, 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.

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 point, you can use DNF to install the default JRE from OpenJDK 17:

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
Example Output
There is 1 program that provides 'javac'.

  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/javac)

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

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
Output
  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)

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 (default JRE and JDK) with DNF Package Manager on AlmaLinux 9. Also, you have learned to set up the Java_Home environment path and manage multiple Java versions.

Hope you enjoy it. You may 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

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!