Oracle JDK 21 is a Long-Term Support release that brings virtual threads, pattern matching, and a range of performance improvements to the Java platform. If you need Oracle’s commercially supported JDK on Ubuntu, this guide walks you through the full installation on Ubuntu 24.04 (Noble Numbat) and Ubuntu 22.04 (Jammy Jellyfish).

Oracle JDK vs OpenJDK – What is the Difference?

Before downloading anything, it helps to understand the two main JDK distributions:

  • OpenJDK – The open-source reference implementation of the Java SE Platform. It is available in Ubuntu’s default repositories and maintained by the community plus contributors from Oracle, Red Hat, and others. Most server workloads run fine on OpenJDK.
  • Oracle JDK – Oracle’s proprietary build that includes additional tooling, commercial features, and longer-term patch support under a paid license. Some enterprise software vendors certify their products only against Oracle JDK, which makes it a hard requirement in those environments.

Starting with JDK 17, Oracle switched its free license to the “Oracle No-Fee Terms and Conditions” (NFTC) for JDK 17, then introduced the “Oracle Technology Network License” for JDK 21. Check Oracle’s current licensing page to confirm whether your use case requires a subscription.

Prerequisites

  • Ubuntu 24.04 or 22.04 server/desktop with sudo privileges
  • A working internet connection
  • An Oracle account (free to create) for downloading the JDK package

Step 1 – Remove Old Java Versions (Optional)

If you have older JDK packages installed and want a clean slate, remove them first:

sudo apt remove --purge oracle-java* openjdk-* -y
sudo apt autoremove -y

Verify that no Java binary remains in your PATH:

java -version

You should see “command not found” if the removal was successful.

Step 2 – Download Oracle JDK 21 .deb Package

Oracle publishes Debian packages for Ubuntu. Head to the official download page:

https://www.oracle.com/java/technologies/downloads/#java21

Select the Linux tab, then download the x64 Debian Package (.deb) file. If you are working on a headless server, copy the download link and pull it with wget:

wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb

For ARM-based systems (like Oracle Cloud Ampere instances), grab the aarch64 variant instead:

wget https://download.oracle.com/java/21/latest/jdk-21_linux-aarch64_bin.deb

Confirm the file downloaded correctly by checking its size – it should be roughly 170-190 MB:

ls -lh jdk-21_linux-*_bin.deb

Step 3 – Install the .deb Package

Install the downloaded package with dpkg:

sudo dpkg -i jdk-21_linux-x64_bin.deb

If dpkg reports missing dependencies, fix them with:

sudo apt --fix-broken install -y

The package installs Oracle JDK 21 under /usr/lib/jvm/jdk-21-oracle-x64.

Step 4 – Configure Alternatives

Ubuntu’s alternatives system lets multiple Java installations coexist. Register Oracle JDK 21 as an alternative for both java and javac:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-21-oracle-x64/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-21-oracle-x64/bin/javac 1

If you have multiple JDK versions installed, select Oracle JDK 21 as the default:

sudo update-alternatives --config java

You will see a numbered list. Enter the number that corresponds to /usr/lib/jvm/jdk-21-oracle-x64/bin/java and press Enter. Repeat for javac:

sudo update-alternatives --config javac

Step 5 – Set JAVA_HOME Environment Variable

Many build tools (Maven, Gradle, Ant) and application servers (Tomcat, WildFly) rely on the JAVA_HOME variable. Set it system-wide by creating a profile script:

sudo tee /etc/profile.d/jdk21.sh > /dev/null <<'EOF'
export JAVA_HOME=/usr/lib/jvm/jdk-21-oracle-x64
export PATH=$JAVA_HOME/bin:$PATH
EOF

Load the file into your current session:

source /etc/profile.d/jdk21.sh

Verify the variable is set:

echo $JAVA_HOME

Expected output:

/usr/lib/jvm/jdk-21-oracle-x64

Step 6 – Verify the Installation

Run a few checks to make sure everything is wired up properly.

Check the runtime version:

java -version

Expected output:

java version "21.0.x" 2024-xx-xx LTS
Java(TM) SE Runtime Environment (build 21.0.x+xx-LTS-xxx)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.x+xx-LTS-xxx, mixed mode, sharing)

Check the compiler version:

javac -version

Test a quick compile-and-run cycle to confirm the full toolchain works:

cat <<'EOF' > /tmp/Hello.java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Oracle JDK 21 is working on " + System.getProperty("os.name"));
    }
}
EOF

javac /tmp/Hello.java
java -cp /tmp Hello

You should see: Oracle JDK 21 is working on Linux

Switching Between Multiple JDK Versions

If your workload requires both OpenJDK and Oracle JDK, install OpenJDK alongside:

sudo apt install openjdk-21-jdk -y

Then switch between them any time with update-alternatives:

sudo update-alternatives --config java

Remember to update JAVA_HOME whenever you switch, or use a wrapper script that reads the alternatives symlink dynamically.

Wrapping Up

You now have Oracle JDK 21 installed and configured on Ubuntu 24.04 or 22.04. The alternatives system makes it straightforward to maintain multiple Java versions side by side, and the JAVA_HOME profile script keeps build tools and application servers pointed at the correct JDK. If your project does not specifically require Oracle’s build, OpenJDK 21 from the Ubuntu repositories is a solid and license-free alternative worth considering.

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here