How to Install Java 21 on Ubuntu

With the release of Java 21, developers around the globe are eager to upgrade and leverage the new features it brings. If you're an Ubuntu user, this guide will lead you through the process of installing Java 21 seamlessly.

Why Java 21? 

Before we dive into the installation steps, let's highlight the benefits of Java 21:

Performance Enhancements: Java 21 is built to boost the efficiency of your applications. 

Innovative Features: Each Java version introduces tools and features to simplify the developer's job. 

Improved Security: Upgrading ensures you have the latest security patches, safeguarding your system from known vulnerabilities.


  • An Ubuntu operating system.
  • Internet connectivity for downloading Java.
  • Access to the terminal with sudo privileges.

Installation Steps

1. Update and Upgrade System:

Start by updating the system's package index to ensure you're getting the latest software. Open the terminal and run:

sudo apt update && sudo apt upgrade -y

2. Add Oracle JDK Repository:

Before downloading Java, add the Oracle JDK repository to your system by executing:

sudo add-apt-repository ppa:linuxuprising/java

3. Install Java 21:

With the repository added, proceed to install Java 21:

sudo apt install oracle-java21-installer

4. Set Java 21 as Default:

Ensure that your system recognizes Java 21 as the default version by running:

sudo apt install oracle-java21-set-default

5. Setting JAVA_HOME on Ubuntu

It's a good practice to set the JAVA_HOME environment variable:

1. Open /etc/environment File

sudo nano /etc/environment

2. Add the JAVA_HOME Variable 

At the end of this file, add the following line (replace the path if your installation is located elsewhere):


3. Reload the File and Activate the Changes

source /etc/environment

4. Verify the Configuration


5. Verify the Installation:

To confirm the successful installation of Java 21, type: java -version If the displayed version matches Java 21, you've successfully completed the installation.

Reference Links