How to Install OpenJDK 21 on Ubuntu 24.04

This article explains how to install OpenJDK 21 on Ubuntu 24.04.

OpenJDK (Open Java Development Kit) is the open-source implementation of the Java Platform, Standard Edition (Java SE). It provides a full suite of development tools, including the Java compiler (javac) and the Java Runtime Environment (JRE), making it easier for developers to create, test, and run Java applications.

By installing OpenJDK on Ubuntu, you can easily integrate Java applications with other software and services running on the same system.

Install OpenJDK 21

All the current versions of OpenJDK packages are available from Ubuntu’s default repositories, so there’s no need to install additional packages.

To install OpenJDK 21, run the command below.

sudo apt update
sudo apt install openjdk-21-jdk

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.

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.

sudo -s
source /etc/profile.d/java.sh

Test OpenJDK

Now that OpenJDK is installed and configured, you can validate and test to see if everything works by running the command below.

java --version

It should output similar lines shown below.

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 multiple versions of OpenJDK installed, you can use the [update-alternatives] command to switch and set the default version to use.

sudo update-alternatives --config java

Select the Java version you wish to use as the 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:

Also run the command below to choose the default javac version.

sudo update-alternatives --config javac

Choose the javac version you want to set as default.

  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.

Frequently Asked Questions

How do I install OpenJDK 21 on Ubuntu 24.04?

To install OpenJDK 21 on Ubuntu 24.04, open your terminal and run the command 'sudo apt update' followed by 'sudo apt install openjdk-21-jdk'. This will install all necessary packages from the default repositories.

How can I set the JAVA_HOME variable after installing OpenJDK?

You can set the JAVA_HOME variable by creating a script in '/etc/profile.d/java.sh' with the command 'sudo tee -a /etc/profile.d/java.sh

How do I check if OpenJDK is installed correctly?

To verify that OpenJDK is installed correctly, run the command 'java –version' in your terminal. You should see output indicating the version of OpenJDK installed, confirming that it is ready to use.

What should I do if I have multiple versions of OpenJDK installed?

If you have multiple versions of OpenJDK installed, you can switch the default version using the command 'sudo update-alternatives –config java'. This allows you to select which version you want to use as the default for your system.

Can I install OpenJDK without additional packages?

Yes, you can install OpenJDK 21 on Ubuntu 24.04 without needing to install additional packages, as all required versions are available in Ubuntu's default repositories. Simply use the standard installation commands provided in the tutorial.

Categories:

3 responses to “How to Install OpenJDK 21 on Ubuntu 24.04”

  1. […] Install OpenJDK on Ubuntu […]

  2. […] Install Java Runtime Environment on Ubuntu […]

  3. […] previous post showed you how to install the open-source (OpenJDK) on Ubuntu. You can read it from the link […]

Leave a Reply

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