Java is a widely used programming language and runtime environment that is essential for many software applications. OpenJDK (Open Java Development Kit) is an open-source implementation of the Java Platform, Standard Edition. If you’re running Ubuntu 22.04 and need Java for development or application deployment, this guide will walk you through the installation process.

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

Installing OpenJDK on Ubuntu 22.04 is a straightforward process. Whether you need Java for software development, running applications, or server-side processes, following the steps above will ensure you have a properly configured Java environment.