Install OpenJDK 21
sudo apt update
sudo apt install -y openjdk-21-jdk-headless
java -version
javac -version
The -headless package has no graphical libraries and is what a server needs. Use openjdk-21-jre-headless if you only run applications and never compile. Java 21 is in the Ubuntu 22.04 and 24.04 repositories.
Other versions
sudo apt install -y openjdk-17-jdk-headless # Java 17
sudo apt install -y openjdk-11-jdk-headless # Java 11 (22.04; check availability on newer releases)
sudo apt install -y openjdk-8-jdk-headless # Java 8 (22.04; check availability on newer releases)
apt-cache search openjdk | grep jdk-headless
The last command lists what your Ubuntu release offers. Which one to choose is covered in Java LTS versions explained.
Switch the default version
sudo update-alternatives --config java
sudo update-alternatives --config javac
It lists the installed versions with numbers. Type the number and press Enter. The change applies to new shells and to services that call plain java.
Set JAVA_HOME
Do not guess the path. Print it:
dirname $(dirname $(readlink -f $(which javac)))
On x86 servers this is /usr/lib/jvm/java-21-openjdk-amd64, on ARM it ends in arm64. Set it for all users:
echo 'JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"' | sudo tee -a /etc/environment
source /etc/environment; echo $JAVA_HOME
For a service, set it in the systemd unit with Environment=JAVA_HOME=... instead. Services do not read your shell profile.
Run one application on a different version
You do not have to change the default. Call the exact binary in the service:
ExecStart=/usr/lib/jvm/java-17-openjdk-amd64/bin/java -jar /opt/app/app.jar
Is Java 25 available?
Java 25 is the newest LTS. If your Ubuntu release does not package it, install a build from Eclipse Temurin or another vendor. We install it on request on every plan.
Servers on our Java VPS hosting plans come with the JDK version you choose.
Last updated October 29, 2026.