How to install Java 21 on Ubuntu

While Ubuntu does not come with Java’s JDK preinstalled, it does come prepackaged with the ability to easily install Java with the apt command.

How to install Java on Ubuntu

To quickly install Java on Ubuntu and have the JDK’s utilities included on the operating system’s PATH, follow these five steps:

  1. First verify that Java is not already inststalled
  2. Issue the sudo apt update command
  3. Install the default JRE with apt
  4. Run java --version on the command line to validate the install
  5. Set JAVA_HOME globally for all Ubuntu users

Check for existing Java installs

You don’t need to setup Java on Ubuntu if someone has already installed it for you. A quick java --version check will tell you if an install is necessary:

java - version
Command 'java' not found, but can be installed with:
sudo apt install default-jre

If the command indicates that Java is not found, proceed with the next steps.

Update Ubuntu’s package listing

As with all Ubuntu software installs, it’s a good idea to update your Ubuntu package list with the most recent information:

sudo apt-get update
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
Apache and Docker tutorials

Master the fundamentals of Apache, Docker and Kubernetes technology.

Install Java on ubuntu with apt

The full Java suite includes the Java Runtime Environment (JRE), Java Virtual Machine (JVM) and utilities to develop Java source code.

The Java Development Kit (JDK) provides everything a user needs to run Java applications. Install the JDK with the following command:

sudo apt install default-jre
Setting up default-jdk-headless (2:1.21) ... 
Setting up openjdk-21-jdk:amd64 (21.0.5+11-1ubuntu~24.04.1) ... 
using /usr/lib/jvm/java-11-openjdk-amd64/bin/jconsole 
Setting up default-jdk (2:1.21) ...

Verify Java is on the PATH

To validate the successful install of Java on Ubuntu, and to ensure the JDK’s bin directory is accessible through the OS’s PATH variable, issue the following command:

java -version
openjdk version "21.0.5" 2024-10-15
OpenJDK Runtime Environment (21.0.5+11-1ubuntu~24.04.11)
OpenJDK 64-Bit Server VM (21.0.5+11-1ubuntu~24.04.1, mixed mode, sharing)

If the java -version command outputs information about the version of Java installed, along with information about the JVM and runtime environment, then you successfully installed Java on Ubuntu with apt.

Configure JAVA_HOME on Ubuntu

Configuring JAVA_HOME isn’t absolutely required, but many enterprise Java applications such as Tomcat or Kafka query the JAVA_HOME directory when they startup and use that information at runtime, so it’s prudent to configure it.

The JAVA_HOME environment variable simply points to the folder in which Java was installed.

Where is Java installed on Ubuntu?

To find out where apt installed Java on Ubuntu, issue the following command and copy the location provided:

$ update-alternatives --config java
There is only one alternative in link group java (providing /usr/bin/java): 
/usr/lib/jvm/java-11-openjdk-amd64/bin/java

However, Ubuntu allows you to reference the preferred Java install through the symbolic link default-java, which is the location that should be used to set environment variables like JAVA_HOME

Add the JAVA_HOME environment variable

With the location of the Java install on the clipboard, open up the server’s environment file with Nano:

sudo nano /etc/environment

Paste the JAVA_HOME assignment at the bottom of the file:

JAVA_HOME="JAVA_HOME="/usr/lib/jvm/default-java"

Then force the Ubuntu terminal to reload the environment configuration file:

source /etc/environment

You should then be able to echo the JAVA_HOME environment variable in an Ubuntu terminal window:

echo $JAVA_HOME
/usr/lib/jvm/default-java

Ubuntu Java install commands

As a quick review, here are all of the commands that were issued in this tutorial to get Java installed on Ubuntu with apt:

  • sudo apt-get update
  • sudo apt install default-jre
  • java -version
  • update-alternatives --config java
  • sudo nano /etc/environment
  • source /etc/environment
  • echo $JAVA_HOME

That’s how easy it is to install Java on Ubuntu with apt.