OpenJDK 17 is a widely-used open-source implementation of the Java Platform, Standard Edition (SE). As a long-term support (LTS) release, OpenJDK 17 is designed for stability and performance, making it an excellent choice for developers and enterprises. Installing OpenJDK 17 on Ubuntu allows you to develop and run Java applications with the latest features and improvements.
On Ubuntu 24.04, 22.04, or 20.04, there are two main methods to install OpenJDK 17. The easiest method is using Ubuntu’s default APT repository, which provides a straightforward installation process. Alternatively, for users who prefer manual installation or need the absolute latest version, you can download the OpenJDK 17 archive and install it manually. This guide will also cover how to switch between different OpenJDK versions on your system, ensuring that you can manage multiple Java environments with ease.
Update Ubuntu before OpenJDK 17 Installation
Before installing OpenJDK, updating Ubuntu to ensure all system packages are current with the “apt update” command is essential. This helps to prevent any potential conflicts during the installation process. To update your Ubuntu system, run the following command in your terminal:
sudo apt update
sudo apt upgrade
Method 1: Install OpenJDK 17 via Ubuntu Repository
The first and most recommended method for installing OpenJDK is through the default Ubuntu repository. You can search for available packages using the “grep” command. To do this, run the following command in your terminal:
apt-cache search openjdk | grep openjdk-17
Example output:
Depending on your version of Ubuntu, you may be able to install OpenJDK 17 JDE and JRE using the following command. This is only applicable if these packages are available in your distribution:
sudo apt install openjdk-17-jre
sudo apt install openjdk-17-jdk
Confirm the installation by running the following command.
java --version
Updates are handled with the standard apt update and upgrade commands. However, you can remove them separately or altogether if you no longer require JDK or JRE.
Example:
sudo apt remove openjdk-17-jre openjdk-17-jdk --purge
Note that this will remove any unrequited leftover dependencies and thoroughly wipe the installation and data from your system as much as possible.
Method 2: Install OpenJDK 17 Manually via Archive
The second method for installing OpenJDK 17 is downloading the .tar.gz archive package. While this option is often more up-to-date than the version in the Ubuntu repository, it requires more manual maintenance, as you’ll need to monitor new updates and repeat the installation process.
Visit the downloads page to obtain the latest build version link. Then, use the following wget command to download the package.
Here’s an example of downloading OpenJDK 17 version 17.0.2 for x64 (however, it is recommended to obtain a new link from the downloads page rather than copying this example):
wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz
Note: Remember, the above link is an example, so ensure you grab the latest build version.
Once the download process is complete, the archive will be extracted.
tar -xvf openjdk-17.*
Next, navigate to the directory.
cd <directory_name>
Run the following command to install the OpenJDK 17 files.
sudo mkdir -p /usr/local/openjdk-17
sudo mv * /usr/local/openjdk-17
The next step is to set up the environment variables by adding the following lines to your .bashrc or .bash_profile file.
export JAVA_HOME=/usr/local/openjdk-17
export PATH=$JAVA_HOME/bin:$PATH
Source the .bashrc or .bash_profile file to apply the changes.
source ~/.bashrc
To verify that the installation was successful, run the following command.
java --version
echo $JAVA_HOME
If installed correctly, you should see the following output:
As demonstrated, the version displayed is the latest OpenJDK 17 build.
Switching Alternative Java Versions
After installing Java, you may want to check for newer versions and switch to them if necessary. To do this, use the following command to list the available alternatives:
sudo update-alternatives --config java
Example output:
The previous output shows that Java 11 is listed with “1”. To switch to this version, type the following and press the enter key, and you should see a similar printout:
update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode
Once the switch is complete, you can verify the default version using the following command.
java --version
Example output:
To switch to any other installed versions, repeat the same process.
Conclusion
In conclusion, installing OpenJDK 17 on Ubuntu is straightforward and can be accomplished using several methods. Whether you use the default Ubuntu repository, download the .tar.gz archive package, or switch to a different version, the steps outlined in this article will help you successfully install and manage OpenJDK 17 on your Ubuntu system.