Guide to Install OpenJDK on Linux

How To Install OpenJDK On Linux

When working with Java applications we need Java Development Kit or JDK for short. There are various JDKs that you can use like Oracle JDK, OpenJDK, IBM J9 JDK, etc. A JDK contains collections of libraries and tools that are needed when developing Java-based applications. It is used along with a JVM (Java Virtual Machine) and JRE (Java Runtime Environment). It is implemented on either of the following Java platforms- Java Enterprise Edition (Java EE), Standard Edition (Java SE), or Micro Edition (Java ME).

OpenJDK is implemented on the Standard Editing of Java aka Java SE. It is an open source and many people collaborate to improve the JDK. OpenJDK is composed of JVM, JRE, JDK, and development tools. It contains the core features of Java and its Standard API that are used in the development of Java applications. 

The decision to make an open-source JDK was taken by Sun Microsystems in 2006. OpenJDK source code was released in the next year in 2007 and the project was officially launched by Sun Microsystems. The latest release of OpenJDK was OpenJDK JDK 20.0.2 which is available for general use from 21 March 2023.

Prerequisites before installing OpenJDK

Following are some prerequisites before installing OpenJDK on Linux:-

  • You should have root or sudo permissions on your system.
  • Check from the official site if your Linux distro is supported by OpenJDK. Some of the popular distros it supports are:- Ubuntu, Oracle Linux, RHEL, and Suse Linux Enterprise Server 15.
  • Check if any other JDK or other versions of OpenJDK is already installed in your system. Installing multiple JDKs can create conflicts.
  • A stable internet connection to download the OpenJDK.
  • Update your package repositories. For example:-
    • sudo apt update  # For Ubuntu/Debian
    • sudo dnf update  # For Fedora
  • Decide which OpenJDK version you want to install. We recommend installing the latest LTS (Long Term Support) version. You can check the version from here.

How to Install OpenJDK on Linux?

You can install OpenJDK through your default package managers like apt, dnf, yum, etc. The package managers like snap can be used as well. If the snap package manager is installed in your system then use the following command:

sudo snap install openjdk

Below we’ve listed methods of installing OpenJDK on different Linux distros.

How to Manually Install OpenJDK from the .tar.gz file?

The manual installation allows you to select the version you want to install. The process can be a bit longer than other methods. The steps to manually install are:-

  • Download the .tar.gz file from the official download page of OpenJDK
  • Select the .tar.gz according to your system and download it.
  • Extract the archive using the following command:-
tar -xvf openjdk-<version>.*
OpenJDK Extracting
  • Navigate to the directory where the files are extracted and use the following commands:-
sudo mkdir -p /usr/local/openjdk-<version>
sudo mv * /usr/local/openjdk-<version>
  • Now we have to set up the environment variables. To do so add the following code to your .bash_profile file:-
export JAVA_HOME=/usr/local/openjdk-17
export PATH=$JAVA_HOME/bin:$PATH
  • Then use the following command to source it:-
source ~/.bashrc

That’s it the installation and environment variable setup will be done. 

How to Install OpenJDK on Ubuntu/Debian-based Linux?

We can use the default apt package managers to Install OpenJDK. To do so use the following command:-

sudo apt install openjdk-17-jre
sudo apt install openjdk-17-jdk
OpenJDK 17 JRE And JDK Install

How to Install OpenJDK on Arch Linux?

OpenJDK is available on the extra repositories on Arch Linux. We can use the Pacman package manager to install OpenJDK through those repositories. Use the following commands:-

To install OpenJDK 20 

# pacman -S jdk-openjdk

To install the latest OpenJDK Oracle build:- 

# pacman -S java-openjdk-bin

To install the latest OpenJDK Early-Access Oracle build for development:-

# pacman -S java-openjdk-ea-bin

How to Install OpenJDK on RHEL-based Linux?

To install OpenJDK on RHEL, we’ll be using the yum package manager. The command to do so is as follows:-

sudo yum install java-17-openjdk-devel

Here we’ve installed the latest LTS (Long Term Support) version of JDK. If you want the latest version, you can change the version. But we recommend sticking to the LTS version.

How to switch to Other OpenJDK versions?

To switch to another OpenJDK version use the following command:-

sudo update-alternatives --config java

You’ll get an output like this-

Command To Switch JDK Version

You can change the current version by selecting the number in front of your preferred version.

How to check the current JDK version installed in my system?

Use the following command to check the current version of your JDK:-

Java –version
Checking Java Version

In the above output, the installed version is 17.0.2.

Conclusion 

OpenJDK is a very popular Java Development Kit. Due to its open-source nature, anyone can contribute to it. The open-source nature creates trust, transparency, and openness for the users of OpenJDK. It has been constantly updated with new features and bug fixes. 

In the above sections, we’ve listed the methods of installation. We recommend using snap or default package managers. Manual installation has chance of errors. Also, before installation check out the prerequisites sections to make your system is compatible.Â