Skip to content
Follow
Ubuntu Linux

How to Install OpenJDK 21 on Ubuntu 24.04

Richard
Written by
Richard
Feb 27, 2025 Updated Jul 13, 2026 3 min read
How to Install OpenJDK 21 on Ubuntu 24.04
How to Install OpenJDK 21 on Ubuntu 24.04

Installing OpenJDK 21 on Ubuntu 24.04 uses the APT package manager to add the latest free Java Development Kit.

OpenJDK is the open-source version of the Java Development Kit. It gives you the tools to write and run Java programs.

This guide helps you install version 21, which is the current Long-Term Support (LTS) release. This ensures you have a stable and strong foundation for your Java projects.

You can easily add OpenJDK 21 to your Ubuntu 24.04 system by following these steps. This gets you the right Java tools for what you want to build.

⚡ Quick Answer

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 can install OpenJDK 21 on Ubuntu 24.04 using your terminal. This command downloads the latest Java Development Kit from Ubuntu’s official software sources. Running the update and install commands is all you need to do to get OpenJDK 21 ready on your computer.

To install OpenJDK 21, run the command below.

🐧Bash / Shell
sudo apt update
sudo apt install openjdk-21-jdk
📝Good to Know
After installing all the packages, run the command below to configure Ubuntu so that OpenJDK can function properly, including setting the JAVA_HOME variable and the environment PATH.
🐧Bash / Shell
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

Activate the script by running the command below.

🐧Bash / Shell
sudo -s
source /etc/profile.d/java.sh

Test OpenJDK

After you install OpenJDK 21, it’s a good idea to test it to make sure everything is working correctly. Open your terminal and type `java –version`. This command will show you the details of your installed Java version, confirming that OpenJDK 21 is now properly set up on your Ubuntu system.

💻Code
java --version

It should output similar lines shown below.

💻Code
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 you have more than one Java version on your Ubuntu computer, you can easily choose which one to use as the default. By running the command `sudo update-alternatives –config java` in your terminal, you can manage and switch between your installed Java versions, controlling which one applications will use automatically.

🐧Bash / Shell
sudo update-alternatives --config java

Select the Java version you wish to use as the default.

💻Code
  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:
💡Tip
Also run the command below to choose the default javac version.
🐧Bash / Shell
sudo update-alternatives --config javac

Choose the javac version you want to set as default.

💻Code
  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!

Conclusion:

In summary, installing OpenJDK 21 on Ubuntu 24.04 is a straightforward process that allows developers to harness the power of Java for application development. Here are the key takeaways:

  • OpenJDK 21 provides a comprehensive set of tools necessary for Java development, including the Java compiler and runtime.
  • The installation involves simple commands executed through the terminal, utilizing Ubuntu’s default repositories.
  • Configuring the JAVA_HOME variable and updating the environment PATH ensures proper functionality of the OpenJDK installation.
  • You can verify the installation with the java --version command to ensure everything is set up correctly.
  • Managing multiple OpenJDK versions is seamless using the update-alternatives command, allowing for flexibility in development environments.

Following these steps, you can effectively set up and manage OpenJDK on your Ubuntu system, which is ready for your Java development needs.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

📚 Related Tutorials

How to Install Additional Software on Ubuntu
Ubuntu Linux How to Install Additional Software on Ubuntu
Advanced Windows 11 Guide For Developers
Windows Advanced Windows 11 Guide For Developers
How to Install RabbitMQ with Erlang/OTP on Ubuntu Linux
Ubuntu Linux How to Install RabbitMQ with Erlang/OTP on Ubuntu Linux
How to Install ClamAV Linux Antivirus on Ubuntu Linux
Ubuntu Linux How to Install ClamAV Linux Antivirus on Ubuntu Linux

3 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *