The Short Answer
sudo apt update
sudo apt install default-jdk-headless
java -versionThe default-jdk packages install the OpenJDK version that Ubuntu chose as the default for your release: OpenJDK 21 on Ubuntu 24.04 and OpenJDK 25 on Ubuntu 26.04. The same packages and commands exist on Debian, with Debian's own default version.
JRE or JDK, Headless or Not
| Package | Contains |
|---|---|
default- | Runtime, no desktop libraries |
default- | Runtime with desktop libraries |
default- | Runtime, compiler, and tools |
default- | All of the above |
A server has no screen, so choose a headless package. Running a finished .jar file needs only the runtime (JRE). Building code, or tools such as Maven and Gradle, need the JDK.
Install a Specific OpenJDK Version
Many applications name the Java version they support. See what your release offers and what the default points to:
apt list 'openjdk-*-jdk-headless'
apt-cache policy default-jdk
sudo apt install openjdk-21-jdk-headless21 is an example. The long-term support lines are the ones to run in production; in November 2026 the newest are 21 and 25, and both are packaged on Ubuntu 24.04 and 26.04. Your application's documentation says which one it needs. Several versions can be installed side by side.
Switch Between Installed Versions
sudo update-alternatives --config java
sudo update-alternatives --config javacEach command prints a numbered list of installed versions; type the number of the one you want. Automatic mode follows the highest priority, which is normally the newest version.
Set JAVA_HOME
Some tools need JAVA_. Find the real path of the active Java first:
readlink -f "$(which java)"The output ends in /bin/java; JAVA_ is the folder above bin, for example /usr/lib/jvm/java-21-openjdk-amd64 (on ARM servers the name ends in arm64). To set it for all users, add a line to /etc/environment:
JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"Log out and in again, then check with echo $JAVA_. A systemd service does not read that file; set the variable in the unit with an Environment= line. More on variables in how to set environment variables in Linux.
Check the Installation
java -version and javac -version print to standard error, so in a script use java -version 2>&1. A quick test runs a single source file directly:
cat > Hello.java <<'EOF'
public class Hello { public static void main(String[] a) { System.out.println("Java works"); } }
EOF
java Hello.javaRun a Java Application as a Service
Create a dedicated user, place the application under /opt, and start it from a systemd unit:
ExecStart=/usr/bin/java -Xmx1g -jar /opt/app/app.jarSet -Xmx well below the server's memory, so the system and other services keep room. The full unit layout is in systemd unit files.
Other OpenJDK Builds and Distributions
Some OpenJDK builds, such as Eclipse Temurin, come from their own apt repository. Adding one means trusting its publisher with root on the server; prefer the Ubuntu packages unless you need a version they lack, and add the repository with a signed-by key as shown in add-apt-repository command not found. On AlmaLinux and Rocky Linux, install a numbered package such as java-21-openjdk-headless with dnf; dnf list 'java-*-openjdk-headless' shows the versions your release offers.
Keep Java Updated
OpenJDK security fixes arrive through normal updates; see sudo apt update. Restart your Java services afterwards, because running processes keep the old version in memory.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations