Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

How to Install Java on Ubuntu: A Complete Guide

Install Java on Ubuntu with one apt command, choose between JRE and JDK, install a specific version, switch between versions and set JAVA_HOME.

3 min readBy Swati

Reviewed by the AmazingRDP technical team

Setting up Java on Ubuntu

Running a Java application or a game server? A Linux VPS gives you root access. See Linux VPS from $6.99

Many server applications, build tools and games need Java before they start. On Ubuntu it is one package away. The questions are which package and which version, and this guide answers both.

Is Java installed already?

java -version

A version number means yes. "Command not found" means no.

JRE or JDK?

PackageContainsFor
JRE (runtime)What is needed to run Java programsRunning an application
JDK (development kit)The runtime plus compiler and toolsDeveloping and building
"headless" variantsThe same without graphics librariesServers without a desktop

Install the default version

sudo apt update
sudo apt install default-jdk

For running only, on a server: sudo apt install default-jre-headless. "Default" means the version Ubuntu has chosen for that release:

UbuntuDefault Java
22.04 LTS11
24.04 LTS21
26.04 LTS25

Install a specific version

Applications often name the version they need. Java's long-term versions are 8, 11, 17, 21 and 25; those are the ones to choose among.

apt search openjdk | grep jdk
sudo apt install openjdk-17-jdk
sudo apt install openjdk-21-jre-headless

The first line shows which versions your Ubuntu release offers. Several can be installed side by side.

Switch between installed versions

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

Each shows a numbered list; type the number of the version that the plain java command should mean. An application can also be started with a particular version by its full path, for example /usr/lib/jvm/java-17-openjdk-amd64/bin/java.

Set JAVA_HOME

Some tools (Maven, Gradle, Tomcat) look for a variable that points to the Java folder. Find the folder:

ls /usr/lib/jvm/

Then set it for all users:

echo JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 | sudo tee -a /etc/environment

Log out and in again, and check with echo $JAVA_HOME. Use the folder name that ls showed; it carries the version and the processor type.

Test it

cat > Hello.java <<END
public class Hello { public static void main(String[] a) { System.out.println("Java works"); } }
END
java Hello.java

Current Java versions run a single source file directly. It prints Java works.

Other builds of Java

OpenJDK from Ubuntu's repository is the right choice in nearly all cases and is updated with the system. Two alternatives exist for special needs:

  • Eclipse Temurin and similar builds from their own repositories, when you need a version Ubuntu does not carry.
  • Oracle JDK, as a .deb file from Oracle's site, when a vendor requires it. Read its licence terms for business use.

Running a Java application as a service

An application that should start with the server belongs in a systemd unit and under its own user, not in a terminal left open. Give it a memory limit that fits the server, with the options -Xms and -Xmx, and leave room for the system: on a server with 4 GB, -Xmx3g is the upper end.

Remove Java

sudo apt remove openjdk-17-jdk
sudo apt autoremove

Problems

MessageCause
UnsupportedClassVersionErrorThe program was built for a newer Java than the one running it. Install the version it names
java: command not found after installingOnly a JDK from a tar file was unpacked; install the apt package, or add its bin folder to the PATH
The wrong version answersChoose with update-alternatives --config java
Killed, or OutOfMemoryErrorThe memory limit or the server's memory is too small: see monitoring with htop

For a server to run it on, see our Linux VPS plans. The same kind of version switching for JavaScript: NVM for Node.js.

Questions

How do I install Java on Ubuntu?

Run sudo apt update and sudo apt install default-jdk.

What is the difference between JRE and JDK?

The JRE runs Java programs. The JDK adds the compiler and tools needed to build them.

How do I switch Java versions on Ubuntu?

Run sudo update-alternatives --config java and choose the version by number.

How do I set JAVA_HOME on Ubuntu?

Add a line such as JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 to /etc/environment and log in again.