How to Install Java on Ubuntu 24.04
Learn how to install java on ubuntu 24.04 with OpenJDK 21 LTS and 17 LTS, manage multiple versions with update-alternatives, and configure JAVA_HOME.

5 min read
Learn how to install java on ubuntu 24.04 with OpenJDK 21 LTS and 17 LTS, manage multiple versions with update-alternatives, and configure JAVA_HOME.
- Prerequisites and Supported Releases
- JDK vs JRE: Choosing the Right Package
- Step 1: Installing OpenJDK 21 LTS (Default Java)
Java remains one of the world's most ubiquitous programming languages and enterprise runtime platforms, powering big data systems, cloud microservices, Android development, and web frameworks.
Whether you are deploying Spring Boot microservices, running Elasticsearch clusters, hosting Jenkins automation servers, or developing enterprise applications, having a properly configured Java development kit is indispensable.
Ubuntu 24.04 LTS includes OpenJDK 21 (the current Long Term Support release) and OpenJDK 17 (the previous LTS release) natively in its official package archives.
In this guide, we walk through how to install java on ubuntu 24.04, choose between JRE and JDK packages, manage multiple coexisting Java versions, and configure the JAVA_HOME environment variable.
Comprehensive developer tutorial on installing OpenJDK 21 LTS and OpenJDK 17 LTS on Ubuntu 24.04 LTS (Noble Numbat). Explains the difference between JDK and JRE, switching default runtime versions with update-alternatives, configuring the JAVA_HOME environment variable, and compiling your first Java program.
Prerequisites and Supported Releases
This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.
Before proceeding, ensure you have the following requirements satisfied:
- A server or workstation running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
- A non-root user account with
sudoadministrative privileges. - Access to a bash terminal shell.
- At least 1 GB of available disk space.
JDK vs JRE: Choosing the Right Package
Before installing Java, determine whether your system requires the JRE (Java Runtime Environment) or JDK (Java Development Kit):
Review this architectural comparison to select the appropriate package for your server:
| Component | Included Tools | Ideal Target | APT Package Name |
|---|---|---|---|
| JRE (Java Runtime Environment) | Java Virtual Machine (JVM), core libraries | Production servers that only execute pre-compiled .jar files | openjdk-21-jre / openjdk-21-jre-headless |
| JDK (Java Development Kit) | Everything in JRE plus javac compiler, debugger, javadoc, jar packager | Developer workstations and build/CI servers (Jenkins, Maven, Gradle) | openjdk-21-jdk / openjdk-21-jdk-headless |
Step 1: Installing OpenJDK 21 LTS (Default Java)
OpenJDK 21 is the default Java runtime in Ubuntu 24.04 LTS, offering 5 years of upstream support with advanced features like virtual threads and patterned switch expressions.
First, update your local APT repository index:
Install the full OpenJDK 21 Development Kit:
Verify the installation by checking the runtime version and compiler:
# Refresh package index:
sudo apt update
# Install OpenJDK 21 JDK:
sudo apt install -y openjdk-21-jdk
# Verify runtime version:
java -version
# Expected output:
# openjdk version "21.0.4" 2024-07-16
# OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu224.04)
# OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)
# Verify compiler version:
javac -version
# Expected output: javac 21.0.4
Step 2: Installing OpenJDK 17 LTS (Legacy Compatibility)
Many existing enterprise applications, Apache Spark jobs, or legacy Gradle builds still require OpenJDK 17 LTS.
You can safely install OpenJDK 17 alongside OpenJDK 21 without conflicts on Ubuntu 24.04:
Install the OpenJDK 17 package using apt:
# Install OpenJDK 17 JDK:
sudo apt install -y openjdk-17-jdk
# Verify OpenJDK 17 binary presence on disk:
ls -d /usr/lib/jvm/java-17-openjdk-amd64
# Output: /usr/lib/jvm/java-17-openjdk-amd64
Step 3: Managing Multiple Java Versions with update-alternatives
When multiple versions of Java are installed, Ubuntu uses the update-alternatives system to manage symbolic links pointing to the default binary.
Run the interactive configuration utility to view all installed Java runtimes and select your preferred default version:
Type the selection number corresponding to the version you wish to make active, then press Enter:
# Configure default Java runtime:
sudo update-alternatives --config java
# Output:
# There are 2 choices for the alternative java (providing /usr/bin/java).
#
# Selection Path Priority Status
# ------------------------------------------------------------
# * 0 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 auto mode
# 1 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
# 2 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode
#
# Press <enter> to keep the current choice[*], or type selection number: 1
# Configure matching Java compiler (javac):
sudo update-alternatives --config javac
Step 4: Setting the JAVA_HOME Environment Variable
Enterprise development tools like Apache Maven, Gradle, Apache Kafka, and Tomcat require the JAVA_HOME environment variable to identify the JVM installation path.
Identify the exact path of your desired Java installation:
Set JAVA_HOME system-wide by appending the declaration to /etc/environment:
# Find active Java installation path:
sudo update-alternatives --list java
# Output:
# /usr/lib/jvm/java-17-openjdk-amd64/bin/java
# /usr/lib/jvm/java-21-openjdk-amd64/bin/java
# Add JAVA_HOME to /etc/environment:
echo 'JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"' | sudo tee -a /etc/environment
# Load the updated environment in current session:
source /etc/environment
# Verify JAVA_HOME variable:
echo $JAVA_HOME
# Output: /usr/lib/jvm/java-21-openjdk-amd64
Step 5: Testing with a Sample Hello World Java Program
Verify your Java installation and compiler by writing, compiling, and running a simple Java class:
Create a file named HelloWorld.java in your terminal:
Compile the source code into bytecode using javac, then execute it with java:
# Create Hello World Java file:
cat << 'EOF' > HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Java is running successfully on Ubuntu 24.04 LTS!");
System.out.println("Java Version: " + System.getProperty("java.version"));
}
}
EOF
# Compile Java class:
javac HelloWorld.java
# Execute compiled class:
java HelloWorld
# Output:
# Java is running successfully on Ubuntu 24.04 LTS!
# Java Version: 21.0.4
Troubleshooting and Common Errors
When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:
javac: command not found
Cause: Only the JRE (runtime) was installed instead of the JDK (development kit).
Resolution: Install the full JDK package using sudo apt install -y openjdk-21-jdk.
sudo apt install -y openjdk-21-jdk
JAVA_HOME not set error in Maven or Gradle
Cause: The JAVA_HOME environment variable is not defined or points to a non-existent directory.
Resolution: Define JAVA_HOME in /etc/environment pointing to /usr/lib/jvm/java-21-openjdk-amd64 and run source /etc/environment.
echo $JAVA_HOME
UnsupportedClassVersionError: has been compiled by a more recent version of the Java Runtime
Cause: Bytecode was compiled with a newer Java version (e.g. Java 21) but executed on an older runtime (e.g. Java 17).
Resolution: Switch the active Java runtime to match the compiled bytecode using sudo update-alternatives --config java.
sudo update-alternatives --config java
Security Best Practices and Hardening
Keep Java updated with automated security patches by regularly executing sudo apt update && sudo apt upgrade.
For headless cloud production servers, use the -headless package variants (e.g. openjdk-21-jdk-headless) to avoid pulling unnecessary X11 desktop GUI dependencies.
Never run production Java application processes as the root user; create dedicated system user accounts (e.g. useradd -r -s /bin/false tomcat).
Related Ubuntu Tutorials and Documentation
Deepen your Linux system administration skills with our related production-tested guides:
- Initial Server Setup on Ubuntu 24.04 LTS — Set up base user permissions and firewall security for Java servers.
- How to Install and Configure PostgreSQL on Ubuntu 24.04 — Configure relational databases for Spring Boot and Java enterprise applications.
- How to Deploy a Node.js Application on Ubuntu 24.04 — Explore reverse proxy and systemd service architectures for backend services.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
.deb) from Oracle's website and install them using sudo dpkg -i jdk-21_linux-x64_bin.deb, but OpenJDK is the community standard.openjdk-21-jre-headless) omit graphics, audio, and keyboard input libraries (such as AWT and Swing), resulting in a substantially smaller installation footprint ideal for cloud servers and Docker images.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides


Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!