How to Install Java on AlmaLinux

AlmaLinux is a FOSS alternative to the old CentOS. It’s a great server distro and a lot of people switched to Alma after the whole CentOS Stream announcement. In this tutorial, we’ll show you how to install and configure Java on AlmaLinux via the CLI.

Java has different editions to choose from, so we’ll include instructions for the most popular and widely used one – the Standard Edition. Java also has different packages to choose from, Java Runtime Environment (JRE) and the Java Development Kit (JDK). The JDK also includes JRE, so this tutorial will show you how to install JDK on Alma Linux. There are also two different implementations – OpenJDK (FOSS), and Oracle Java. Most people prefer OpenJDK, so that’s what we’ll use. If you don’t know the differences between any of these, you can either google them or don’t worry about it – this tutorial will set up the environment that will most likely work for you.

Prerequisites

These instructions were written and tested for AlmaLinux, but they should work on CentOS and Fedora too, any versions.

This is what you’ll need:

  • Root access (log in as the root user before doing the first step)
  • An AlmaLinux server. You can get one from Linode. Or if you need a managed server and you want the hosting provider to install Java for you, then get one from SolaDrive.

Step 1: Update the system

As always, the first step is to update your system. Run the following command:

yum update

Step 2: Install Java 17 (latest LTS version)

As of writing, the latest LTS release of Java is version 17. To install it, run the following command:

yum install java-17-openjdk-devel

If you want to install JRE (not JDK), just remove “-devel” from the command.

You can also install multiple versions of Java. We’ll show you how to switch between versions below.

To verify that you installed Java 17, run the following command:

java -version

This should give you an output similar to this:

openjdk version "17.0.1" 2021-10-19 LTS
OpenJDK Runtime Environment 21.9 (build 17.0.1+12-LTS)
OpenJDK 64-Bit Server VM 21.9 (build 17.0.1+12-LTS, mixed mode, sharing)

And that’s it. Java is now installed and you can continue using it on your AlmaLinux. You don’t have to do the next 2 steps, they are optional.

Step 3: Change the default Java version (optional)

If you’ve installed multiple Java versions, you can change the default version you’re using by running the following command:

alternatives --config java

This will list out all the installed Javas on your system, and you’ll need to enter a number to choose what version you’d like to use. An example output is:

There are 2 programs which provide 'java'.

Selection    Command
-----------------------------------------------
*+ 1         java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64/bin/java)
2            java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/bin/java)

So, in this example, the current default version of Java that is used is Java 11, to change it to Java 17, just type “2” and hit Enter.

You can verify if you changed the default version by running:

java -version

Step 4: Set the JAVA_HOME environment variable (optional)

Some applications use the JAVA_HOME variable to find the location of your Java installation. To set the variable, you need to add the location of where you’ve installed Java to the JAVA_HOME variable.

There are multiple methods of finding where you’ve installed Java. An easier one is just running the following command:

type java

Which should give you a result with the location of the Java installation. Example output:

java is hashed (/usr/bin/java)

So, the location is “/usr/bin/java”.

And finally, set the variable with the following command:

export JAVA_HOME=/usr/bin/java

And that’s it. You’ve installed and configured Java on AlmaLinux.

Leave a comment

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