Applies to: OpenJDK 17, 21 and 25 on Ubuntu 24.04 and Debian 13 (notes for AlmaLinux and Rocky Linux 9)

Ubuntu and Debian include OpenJDK, the free open-source Java. You need the JRE to run Java programs and the JDK to build them. We tested these steps on Ubuntu 24.04 and Debian 13.

Which version?

  • 21: the default on Ubuntu 24.04 and Debian 13 (default-jdk), a long-term-support release.
  • 25: the newest long-term-support release; needed for example by Minecraft 26.x.
  • 17: for older applications that ask for it.

Check what your application needs before you choose.

Install

sudo apt update
sudo apt install default-jdk               # OpenJDK 21 (JDK)
sudo apt install openjdk-25-jdk            # or a specific version
sudo apt install openjdk-25-jre-headless   # just the runtime, for servers

-headless packages leave out the graphical parts, which a server doesn’t need.

Check it

java -version
javac -version       # only with a JDK

Several versions: choose the default

sudo update-alternatives --config java
sudo update-alternatives --config javac

Pick the number of the version you want. More detail: how to work with multiple Java versions.

Set JAVA_HOME

Some tools need it. Find the folder of the active Java:

dirname $(dirname $(readlink -f $(which java)))

It prints something like /usr/lib/jvm/java-21-openjdk-amd64. Add it to /etc/environment as JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64" and log in again.

Test with a small program

printf 'public class Hello { public static void main(String[] a) { System.out.println("Hello from Java " + System.getProperty("java.version")); } }\n' > Hello.java
java Hello.java

AlmaLinux and Rocky Linux

sudo dnf install java-21-openjdk-devel     # JDK (or java-25-openjdk-devel)
sudo dnf install java-25-openjdk-headless  # runtime only
sudo alternatives --config java

Running a Minecraft server? See how to make a Minecraft server on Linux.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket