How to Install Java in Linux? A Comprehensive Guide

How to Install Java in Linux?

The simplest method to install Java in Linux is through the apt package manager since Linux is compatible with the standard Java version.

Generally, to install Java in Linux, you should:

  1. Check whether Java is already installed on Linux
  2. Update the package list
  3. Install the standard Java version (Default JRE/JDK) and the Oracle JDK version
  4. Verify that Java is successfully installed

After completing these steps, and in case you have more than one version of Java installed on your Linux machine, you can choose the default version of Java.

Prerequisites for Installing Java on Linux

There are not many prerequisites needed to install Java in Linux.
To do so, you need:

  • A linux VPS or machine
  • A root user account with sudo privileges and access to the terminal

How to install Java on Debian, Ubuntu, and Kali Linux

Before installing Java in Ubuntu, first, check whether it is necessary to install it or not.

Java might have been previously installed on your Ubuntu/Debian/ Kali system, so installing it again might not be necessary.

1. Check the installed Java versions

Run the following command to check whether your system is already equipped with Java.

java - version


Command 'java' not found, but can be installed with:
sudo apt install default-jdk
sudo apt install default-jre
apt install [first version available]
apt install [second version available]

As you can see in the output, Java is unavailable in the Ubuntu system, so now is the time to install it.

2. Update Package List

To use the latest versions and ensure your system is up to date, update the package list using the following command:

apt update


Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

3. Install Java with apt in Ubuntu

OpenJDK and OpenJRE are the primary open-source options for Java, and they can be installed using the apt package manager.

Ubuntu 22.04, by default, supports OpenJDK 11, which has the same functionality as JRE and JDK.

Run the following command to download and install Java Runtime Environment (JRE) from OpenJDK 11:

apt install default-jre

To confirm the installation, type ‘y‘ and then press Enter.

Also, to install the latest version of the Java Development Kit (JDK) on Debian, run the following command:

apt install default-jdk

To confirm the installation, type ‘Y‘ and press Enter. The installation may take some time.

You can install a specific version of OpenJDK in Ubuntu/ Kali/ Debian based on your needs. For example, to install OpenJDK 15, you can run the command below:

apt install openjdk-15-jdk

To install another version of OpenJDK, replace the desired version number (e.g., 8, 13, 16, etc.) with 15.

How to install Java on Linux Mint

Linux Mint already has the OpenJDK version of Java libraries. First, we need to uninstall the OpenJDK by running the following command:

apt-get update && apt-get remove openjdk*

Now, download the appropriate Linux version of Oracle JDK containing tar.gz extension for your system from Java Downloads.

Pay attention to the 32-bit (x86) and 64-bit (x64) versions.

After downloading the file, go to the download directory (Downloads folder in this guide) in the terminal:

cd ~/Downloads

Extract the tarball and rename it to the downloaded file. Write the following command and press Tab to autocomplete the name of the file:

tar -zxvf jdk-

Create a folder in the /opt where you want the JDK to be stored:

mkdir -p /opt/java

Now, move the extracted folder to /opt/java.

mv jdk1.7.0_261 /opt/java

The version in this tutorial is 1.7.0_261. In this stage, you need to make the JDK system default:

update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk1.7.0_25/bin/java" 1
update-alternatives --set java /opt/java/jdk1.7.0_25/bin/java

Now that the installation is complete, it’s better to verify the Java installation on Linux Mint:

java -version

Install Java on CentOS and Fedora

Here are the steps to download and install Java on CentOS and Fedora. As always, let’s update the packages first:

yum update

Run the following command to download the Java Development Kit:

yum install java-11-openjdk-devel

Now, to install the Java Runtime Environment 11 (Open JRE 11), execute this command:

yum install java-11-openjdk

Remember that you can download and install any version of Java following this tutorial.

To install the Oracle Java 11, download the .rpm package (Java SE 11.0.22 (LTS)) from Java Downloads.

Install the package with the following command upon download completion:

yum localinstall jre-VERSION-linux-x64.rpm

You can verify the installation of Java on CentOS and Fedora by executing the following command:

java –version

How to install Java on AlmaLinux

Like the steps to install Java in Linux distributions, let’s first update the system packages:

yum update

In this section, we will install the LTS version of Java 17 on AlmaLinux. To do so, run:

yum install java-17-openjdk-devel

You can install the JRE instead of JDK by running this command:

yum install java-17-openjdk

Run this command to verify Java installation on AlmaLinux:

java -version


openjdk version "17.0.1" 2021-10-19 LTS
OpenJDK Runtime Environment 21.9 (build 17.0.1+12-LTS)
OpenJDK 64-Bit Server VM 21.9 (build 17.0.1+12-LTS, mixed mode, sharing)

Your Java is installed successfully.

Instal Java OpenJDK on Red Hat Linux (RHEL8)

The first step to install OpenJDK on Red Hat Enterprise Linux 8 (RHEL8) is to update packages as follows:

dnf update

Now install the OpenJDK 11 using this command:

dnf install java-11-openjdk-devel

When the installation process is complete, check the Java version:

java -version

Install Java on Arch Linux and Manjaro

In the Java installation on Arch Linux and Manjaro process, we start by checking the installed versions of Java:

java -version

Now, let’s check the available versions of JRE for download:

pacman -sS java | grep jre

To download the latest version of JRE, run this command:

pacman -S jre-openjdk

If the terminal pops up a confirmation message during the installation, type (-y) to continue installing.

To install JDK on Arch Linux/Manjaro, run the following command:

pacman -S jdk-openjdk

How to check the installed versions of Java on Linux

You can check the installed versions of Java on Linux distributions by executing the following as the root user:

java -version

How to change default Java version on linux

If you’ve installed multiple Java versions, you can change the default version you’re using and manage Java versions by running the following command:

update-alternatives --config java


There are 2 choices for the alternative java (providing /usr/bin/java). 
Selection    Path                                         Priority          Status 
0           /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111               auto mode 
1           /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111             manual mode 
*2          /usr/lib/jvm/java-17-oracle/bin/java         1091              manual mode 
3           /usr/lib/jvm/jdk-17-oracle-x64/bin/java      285278208           auto mode 
Press <enter> to keep the current choice[*], or type selection number:

After the output is shown, you can type the number of the Java version you wish to set as default and press Enter.

Now you can check the default version of Java on your Linux server or system by the following command:

java --version

How to configure JAVA_HOME environment variable on Ubuntu

The JAVA_HOME environment, in simple terms, points to the directory where Java is installed.

Usually, Java programs need information about the JAVA_HOME directory during setup.

Since, configuring the JAVA_HOME environment variable is useful when setting up and running Java-based software.

To configure the JAVA_HOME environment variable, you first need to locate the Java installation path. To do this, execute the following command:

update-alternatives --config java

Sample Output:

There are 2 choices for the alternative java (providing /usr/bin/java).
Selection    Path                                         Priority        Status
0           /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111           auto mode
1           /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111           manual mode
*2          /usr/lib/jvm/java-17-oracle/bin/java          1091           manual mode
3           /usr/lib/jvm/jdk-17-oracle-x64/bin/java       285278208      auto mode
Press <enter> to keep the current choice[*], or type selection number:

As you can see, the previous command displays the installation path of Java in the ‘path’ section.

Copy the relevant path for your desired Java version, then open the /etc/environment file with your preferred text editor:

nano /etc/environment

At the end of the file, paste the copied path for the Java version, but remove the ‘/bin‘ portion of the path, as follows:


For example:

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

After adding the Java installation path to the file, save your changes in the editor and exit. As a result, you have configured the JAVA_HOME path on your Ubuntu system.

To apply the changes in your current session, reload the /etc/environment file:

source /etc/environment

To confirm the environment variable is configured, run the following command:


Displaying the Java installation path ensures that you have configured the environment variable correctly.

How to uninstall Java in Ubuntu Linux

If, for any reason, you want to remove the Java package from your Ubuntu system, you can use apt remove command.

If you want to uninstall the default version of OpenJDK, run the following command:

apt remove default-jdk

To uninstall the specific version of OpenJDK, use the syntax below:

apt remove openjdk-[Java version number]-jdk

After executing the previous command, you will be prompted to confirm the removal of Java. Therefore, you need to press ‘y‘ to remove the Java package.


Java is a popular and versatile programming language that caters to the needs of software developers.

The standard version of Java (OpenJDK) is widely used on Linux distributions, making it easier to use and run Java-based software like Jenkins, Jetty, Glassfish, and more.

While a broad spectrum of programming languages is available today, Java is extensively used in server and desktop environments due to its valuable features.

Community support and the availability of resources have made the Java language a popular choice for developing various software projects on Ubuntu.

Leave a Reply

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