How to Install OpenJDK 21 on Ubuntu 24.04
OpenJDK 21 on Ubuntu 24.04 gives you the official Java tools to run and build Java programs on your system. It is the free, open-source build of the Java Development Kit (JDK). The JDK includes the Java runtime, the javac compiler and the other tools you need to write and compile Java code.
Version 21 is a Long-Term Support (LTS) release, so you get stability and security updates for your projects through September 2031. You can set it up on Ubuntu with a few commands in the terminal. This guide covers installing it, setting up the environment variables, checking the version, and choosing the default Java and javac versions if you have more than one installed.
Install OpenJDK 21 on Ubuntu 24.04 by running `sudo apt update` followed by `sudo apt install openjdk-21-jdk`. After installation, configure your environment by sourcing the `java.sh` script. You can verify the installation by checking the version with `java –version`.
Install OpenJDK 21
You install OpenJDK 21 from the terminal with Ubuntu’s standard package manager. Ubuntu 24.04 includes OpenJDK 21 in its default repositories, so you don’t need to add a Personal Package Archive (PPA) or download anything by hand. You update the package list first, then run the install command.
Before you start, make sure you are signed in with an account that can use sudo, and that the computer is connected to the internet. To open a terminal, press Ctrl+Alt+T, or search for Terminal in the Activities overview.
Run the command below to install OpenJDK 21. Enter your password if Ubuntu asks for it. Nothing shows on screen while you type it, which is normal. If Ubuntu asks you to confirm the download, type Y and press Enter.
sudo apt update
sudo apt install openjdk-21-jdk
When the installation finishes, you should be back at the command prompt with no error messages. If you see an error saying the package can’t be found or the download failed, check your internet connection and run the package list update again before retrying.
Next, run the command below. It configures Ubuntu so OpenJDK works properly, including setting the JAVA_HOME variable and adding Java to the environment PATH. Many build tools and IDEs read JAVA_HOME to find the JDK, so it is worth setting even if you don’t need it right away.
sudo tee -a /etc/profile.d/java.sh <<'EOF'
export JAVA_HOME=$(dirname $(dirname $(readlink $(readlink $(which java)))))
export PATH=$PATH:$JAVA_HOME/bin
EOF
Now activate the script by running the command below. This applies the new settings to the terminal window you have open. Other terminal windows that were already open won’t pick up the change, so close them and open new ones. If a program still can’t find Java later, log out and back in once so every session loads the new settings.
sudo -s
source /etc/profile.d/java.sh
Test OpenJDK
Checking the Java version confirms that OpenJDK 21 installed correctly and is ready to use. Open your terminal and run the version check command below. It prints the installed build details, and the version number should start with 21.
java --version
You should see output similar to the lines below. The exact build number after 21 will differ, because Ubuntu ships security updates regularly. If the output shows a different major version, such as 17, another Java version is currently the default. The last section of this guide shows how to switch.
openjdk 21.0.6 2025-01-21
OpenJDK Runtime Environment (build 21.0.6+7-Ubuntu-124.04.1)
OpenJDK 64-Bit Server VM (build 21.0.6+7-Ubuntu-124.04.1, mixed mode, sharing)
OpenJDK is installed and ready to use.
Choose the default
If more than one Java version is installed, Ubuntu lets you pick which one runs by default when you start a Java program. Run the alternatives configuration command below to list all installed versions and choose the one you want. If OpenJDK 21 is the only version installed, the list will have just one entry and there is nothing to change.
sudo update-alternatives --config java
Select the Java version you want to use as the default:
- Find the OpenJDK 21 entry in the numbered list.
- Type the number shown in the Selection column for that entry.
- Press Enter. The terminal returns to the prompt once the default is changed.
If you press Enter without typing a number, Ubuntu keeps the current default.
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:
The Java runtime and the compiler are set separately, so run the command below to choose the default javac version as well. Keeping both on the same version avoids confusing errors, such as code compiled with one version failing to run on another.
sudo update-alternatives --config javac
Choose the javac version you want to set as the default. Type the number of the OpenJDK 21 entry and press Enter, the same as before. To undo this later, run the same command again and pick a different number.
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-21-openjdk-amd64/bin/javac 2111 auto mode
1 /usr/lib/jvm/java-17-openjdk-amd64/bin/javac 1711 manual mode
2 /usr/lib/jvm/java-21-openjdk-amd64/bin/javac 2111 manual mode
Press <enter> to keep the current choice[*], or type selection number:
That should do it! Run the version check once more in a new terminal window to confirm that the default is the one you chose.
Conclusion:
In summary, installing OpenJDK 21 on Ubuntu 24.04 is a straightforward process that gives developers a current LTS version of Java for application development. Here are the key takeaways:
- OpenJDK 21 provides the tools needed for Java development, including the Java compiler and runtime, and it is supported through September 2031.
- The installation uses a few terminal commands and Ubuntu’s default repositories, so no extra PPA is needed.
- Configuring the JAVA_HOME variable and updating the environment PATH lets your tools find the JDK. Open a new terminal, or log out and back in, so the settings apply everywhere.
- You can verify the installation with the
java --versioncommand to confirm that version 21 is the one running. - Managing multiple OpenJDK versions is straightforward with the
update-alternativescommand, which lets you choose the default Java and javac versions and change them later.
Following these steps, you can set up and manage OpenJDK on your Ubuntu system and start building Java applications.
Have a question about this guide?
Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.
Did this guide fix your problem?
About the Author
Richard
Tech Writer, IT Professional
Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.
[…] Install OpenJDK on Ubuntu […]
[…] Install Java Runtime Environment on Ubuntu […]
[…] previous post showed you how to install the open-source (OpenJDK) on Ubuntu. You can read it from the link […]