OpenJDK 21 is the latest Long-Term Support release of the free, open-source Java Development Kit. This version brings virtual threads, pattern matching for switch expressions, and record patterns to production environments. Released in September 2023 with community support extending through at least September 2031, OpenJDK 21 represents a significant step forward for Java development. By the end of this guide, you will have OpenJDK 21 installed on Ubuntu using the default APT repository, with the ability to verify your installation, manage multiple Java versions, and configure your development environment.
Compare OpenJDK LTS Releases for Ubuntu
Selecting the right Java version affects long-term maintenance and access to language features. The table below compares current LTS releases available in Ubuntu’s default repositories, helping you choose based on your project’s requirements.
| Java Version | LTS Support Until | Choose It When | Trade-offs |
|---|---|---|---|
| OpenJDK 8 | December 2030 (community) | Legacy applications, older frameworks like Spring 4.x, or vendor-certified deployments requiring Java 8 | Missing modern language features (no var, no records, no virtual threads); some libraries dropping Java 8 support |
| OpenJDK 11 | September 2027 (community) | Enterprise applications, build servers, projects needing stable LTS with module system support and modern TLS | Lacks Java 21 features; some newer frameworks require 17+ as minimum |
| OpenJDK 17 | September 2029 (community) | Spring Boot 3.x, Jakarta EE 10+, or workloads benefiting from records and sealed classes | Missing virtual threads and newer pattern matching features from Java 21 |
| OpenJDK 21 | September 2031 (community) | New projects, high-concurrency applications using virtual threads, or development requiring the latest stable LTS features | Newest LTS release; verify all dependencies support Java 21 before migrating production workloads |
OpenJDK 21 is ideal for new projects, microservices architectures that benefit from virtual threads, and teams wanting access to the most recent stable language improvements. However, if your project relies on frameworks that have not yet certified Java 21 support, consider OpenJDK 17 as a stable alternative until your stack is ready.
Update Ubuntu Before OpenJDK 21 Installation
Before installing new software, refresh your package index to ensure you receive the latest available version and avoid dependency conflicts. Run the following commands to update your system:
sudo apt update
sudo apt upgrade
The first command downloads current package information from configured repositories, while the second upgrades installed packages to their newest versions. Depending on how recently you last updated, this process typically takes a few minutes.
Install OpenJDK 21 on Ubuntu
OpenJDK 21 is available directly from Ubuntu’s default repositories, so no additional configuration or third-party sources are required. First, confirm the available packages by searching the repository:
apt-cache search openjdk-21
This command lists all OpenJDK 21 related packages. You should see output similar to:
openjdk-21-jdk - OpenJDK Development Kit (JDK) openjdk-21-jdk-headless - OpenJDK Development Kit (JDK) (headless) openjdk-21-jre - OpenJDK Java runtime, using Hotspot JIT openjdk-21-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless) openjdk-21-source - OpenJDK Development Kit (JDK) source files
On Ubuntu 22.04 LTS, OpenJDK 21 comes from the universe repository, which is enabled by default on standard installations. On Ubuntu 24.04 LTS and newer releases, OpenJDK 21 resides in the main repository. Either way, the installation commands work identically on all supported LTS releases.
Choose Between JRE and JDK
Your choice depends on whether you need to run Java applications only or also develop them. The Java Runtime Environment (JRE) provides the minimum required to execute Java programs:
sudo apt install openjdk-21-jre
For development work, compiling source code, or using build tools like Apache Maven, install the full Java Development Kit (JDK) instead. The JDK package includes the JRE plus development utilities such as the Java compiler, debugger, and documentation tools:
sudo apt install openjdk-21-jdk
When in doubt, install the JDK. It includes everything from the JRE while adding development tools you may need later. The additional disk space is minimal compared to the flexibility gained.
Verify the Installation
After installation completes, confirm Java is accessible by checking the installed version. OpenJDK 21 uses the double-hyphen version flag available since Java 9:
java --version
The output confirms OpenJDK 21 is installed and active:
openjdk 21.0.x 20xx-xx-xx OpenJDK Runtime Environment (build 21.0.x+x-Ubuntu-x) OpenJDK 64-Bit Server VM (build 21.0.x+x-Ubuntu-x, mixed mode, sharing)
If you installed the JDK, also verify that the Java compiler is available:
javac --version
javac 21.0.x
Manage Multiple Java Versions
Development environments often require multiple Java versions for different projects. Ubuntu’s update-alternatives system manages symlinks in /usr/bin/, allowing you to switch between installed versions without modifying PATH variables or manually creating symlinks.
Switch the Default Java Runtime
To view all installed Java versions and interactively select the system default, run:
sudo update-alternatives --config java
The command displays a numbered list of available Java installations along with their filesystem paths and priority values. If multiple Java versions are installed, the output looks similar to:
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:
Type the number corresponding to your preferred Java version and press Enter. The asterisk marks the currently active selection. After switching versions, confirm the change by running java --version again.
Switch the Default Java Compiler
When using multiple JDK versions, you should also switch the Java compiler to match the runtime. Compiling code with one Java version while running it on another can cause bytecode compatibility issues:
sudo update-alternatives --config javac
Select the same version number you chose for the runtime. Both commands return immediately to the shell prompt after making your selection.
Configure the JAVA_HOME Environment Variable
Build tools, IDEs, and application servers frequently require the JAVA_HOME environment variable to locate your Java installation. For comprehensive configuration instructions covering system-wide and per-user setups, see our guide on setting the Java environment path in Ubuntu.
To quickly identify the installation path of your currently active Java version, use this command:
dirname $(dirname $(readlink -f $(which java)))
With OpenJDK 21 active on a 64-bit system, the command returns:
/usr/lib/jvm/java-21-openjdk-amd64
Next, add this path to your shell configuration for persistence. For bash users, append it to your ~/.bashrc file:
echo 'export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64' >> ~/.bashrc
source ~/.bashrc
Finally, verify the variable is configured correctly:
echo $JAVA_HOME
/usr/lib/jvm/java-21-openjdk-amd64
Test the Installation with a Sample Program
Verifying your installation goes beyond checking version numbers. Compiling and running a test program confirms the full toolchain works correctly and demonstrates Java 21’s new features in action.
Create a Test Program
First, create a new Java source file using any text editor:
nano Hello.java
Then, add the following code, which uses Java 21’s record pattern matching feature to demonstrate the installation is fully functional:
record SystemInfo(String javaVersion, String osName) {}
public class Hello {
public static void main(String[] args) {
var info = new SystemInfo(
System.getProperty("java.version"),
System.getProperty("os.name")
);
if (info instanceof SystemInfo(String version, String os)) {
System.out.println("Hello from OpenJDK 21!");
System.out.println("Running Java " + version + " on " + os);
}
}
}
Save the file by pressing Ctrl+O, then exit nano with Ctrl+X.
Compile and Run the Program
Compile the source file into Java bytecode:
javac Hello.java
Successful compilation produces no output and creates a Hello.class file in the current directory. If the compilation fails, verify that OpenJDK 21 is the active version using javac --version.
Next, execute the compiled program:
java Hello
Expected output:
Hello from OpenJDK 21! Running Java 21.0.x on Linux
This output confirms your OpenJDK 21 installation compiles and executes Java code correctly, including modern language features like records and pattern matching.
Troubleshoot Common Issues
This section addresses problems you may encounter when installing or using OpenJDK 21 on Ubuntu.
Package Not Found on Ubuntu 22.04
If apt install openjdk-21-jdk fails with “Unable to locate package” on Ubuntu 22.04, the universe repository may be disabled. This can occur on minimal installations or customized systems. First, check if universe is enabled:
grep -r "universe" /etc/apt/sources.list /etc/apt/sources.list.d/
On a standard installation, you should see multiple lines containing “universe”:
deb http://archive.ubuntu.com/ubuntu/ jammy universe deb http://archive.ubuntu.com/ubuntu/ jammy-updates universe deb http://security.ubuntu.com/ubuntu/ jammy-security universe
If you see no output or only commented lines (starting with #), enable the universe repository:
sudo add-apt-repository universe
sudo apt update
After enabling universe, retry the installation command. This step is unnecessary on Ubuntu 24.04 and later, where OpenJDK 21 is in the main repository.
Wrong Java Version Active
If java --version reports a different version than expected after installation, multiple Java versions exist on your system with a higher priority assigned to another version. To fix this, list available alternatives:
sudo update-alternatives --config java
Select the entry corresponding to OpenJDK 21 and confirm your selection. Also update the compiler using update-alternatives --config javac to maintain consistency between runtime and compiler.
JAVA_HOME Not Recognized
Build tools failing with “JAVA_HOME is not defined” or pointing to an incorrect path indicates the environment variable needs configuration. First, identify the correct path:
dirname $(dirname $(readlink -f $(which java)))
Then, add the export statement to your shell configuration file:
echo 'export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64' >> ~/.bashrc
source ~/.bashrc
Verify the variable is set by running echo $JAVA_HOME. The path should match your OpenJDK 21 installation directory.
Bytecode Version Mismatch Errors
Errors mentioning “unsupported major.minor version” or “class file version X.Y” occur when code compiled with a newer Java version runs on an older JRE. To resolve this, verify your compiler and runtime versions match:
java --version
javac --version
Both commands should show Java 21. If they differ, align them using update-alternatives for both java and javac.
Remove OpenJDK 21
If you no longer need OpenJDK 21, remove it along with automatically installed dependencies to free disk space:
sudo apt remove openjdk-21-jdk openjdk-21-jre
sudo apt autoremove
The first command removes the main OpenJDK 21 packages. The apt autoremove command then cleans up orphaned dependencies, such as font packages and documentation, that were installed alongside Java but are no longer required by other software.
After removal, verify by checking for remaining Java installations:
java --version
If no other Java versions are installed, this command returns “command not found.” Otherwise, the output shows which Java version is now active.
If you configured
JAVA_HOMEin your~/.bashrcfile, remember to update or remove that export line after uninstalling OpenJDK 21. Applications relying onJAVA_HOMEwill fail if the path points to a removed installation.
Conclusion
You have installed OpenJDK 21 on Ubuntu and verified the installation works correctly. The update-alternatives system provides clean version switching when multiple Java installations coexist, and the JAVA_HOME configuration enables compatibility with build tools and application servers. For Java development workflows, consider installing Apache Maven to manage project dependencies and builds. If your stack requires an earlier LTS release, explore OpenJDK 17 for broad framework compatibility or OpenJDK 11 for mature enterprise environments.
Useful Resources
For additional Java documentation and alternative distributions, these resources provide authoritative information:
- OpenJDK 21 Project Page contains official release notes, JEP documentation, and specifications for all features introduced in Java 21.
- Eclipse Adoptium offers free, enterprise-quality OpenJDK builds with long-term support across multiple platforms.
- Setting the Java Environment Path in Ubuntu provides detailed JAVA_HOME configuration for both single-user and system-wide setups.