How to Install OpenJDK on Ubuntu 22.04

OpenJDK is a powerful, open-source implementation of Java, widely used for developing and running robust applications. This concise guide simplifies the process of installing OpenJDK on Ubuntu 22.04, ensuring you have a reliable Java environment for development or deployment. We’ll also cover practical examples to help you get started.

Step 1: Update the Package Index

Before installing any new software, it is always recommended to update the package index to ensure you get the latest versions. Open a terminal and run the following command:

sudo apt update && sudo apt upgrade -y

This will update the package lists and upgrade installed packages to their latest versions.

Step 2: Install OpenJDK

Ubuntu 22.04 provides multiple versions of OpenJDK. You can choose to install OpenJDK 11 or OpenJDK 17, which are commonly used versions.

Install OpenJDK 11

To install OpenJDK 11, run:

sudo apt install openjdk-11-jdk -y

Install OpenJDK 17

To install OpenJDK 17, run:

sudo apt install openjdk-17-jdk -y

You can install multiple versions if needed, but only one can be set as the default.

Step 3: Verify the Installation

After installation, verify that Java has been installed correctly by checking the version:

java -version

This command should output something like:

openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Ubuntu-222.04)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Ubuntu-222.04, mixed mode, sharing)

Step 4: Set the Default Java Version

If you have multiple Java versions installed, you can set the default version using the following command:

sudo update-alternatives --config java

This will display a list of installed Java versions. Select the number corresponding to the version you want to set as the default.

To check the current default Java version, use:

java -version

Step 5: Set Up JAVA_HOME Environment Variable

Many applications require the JAVA_HOME environment variable to be set. To find out the Java installation path, run:

readlink -f $(which java)

Once you have the path, edit the environment file:

sudo nano /etc/environment

Add the following line at the end:

JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"

Replace the path with the actual installation path of your Java version. Save the file and reload it:

source /etc/environment

To verify, run:

echo $JAVA_HOME

Step 6: Uninstall OpenJDK (Optional)

If you need to remove OpenJDK for any reason, use the following command:

sudo apt remove --purge openjdk-17-jdk -y

Replace openjdk-17-jdk with the installed version you want to remove.

Conclusion

Setting up OpenJDK on Ubuntu 22.04 is quick and efficient, enabling you to develop or deploy Java applications seamlessly. With the steps above, practical examples, and additional tips, you can confidently manage your Java environment and troubleshoot common issues. Whether you’re building a simple program or a complex server application, OpenJDK has you covered.