How to Install Java in Linux?


Java is widely used as a strong and high-level programming language. The Java Development Kit (JDK) is the environment required to execute and develop Java applications. However, a Java IDE is an integrated development environment for Java, like Eclipse and IntelliJ Idea. With its user interface and many additional capabilities, including a debugger and automation tools, IDEs make it simple to design and code complicated applications.

Just as JDK has the ‌tools for Java, in the same way, all the libraries are present in the Java Runtime Environment (JRE). The JRE is the foundation that allows Java programs to communicate with the operating system during the execution of Java applications.

These programs are compiled in Java using the same Java Virtual Machine (JVM). Many operating systems can run Java, including Linux, macOS, and Windows. The selling point of Java is its portability (and bytecode). This article explains how to install Java on a Linux-based system (How to Install Java in Linux?). But first things first:

Which Java Should You Install?

Java is mainly divided into two major categories. JVM (Java Virtual Machine) is one of the runtime components of Java Runtime Environment (JRE). This engine enables Java applications to be run and launched on the user’s computer.

The Java Development Kit (JDK) comes in its second category, a development toolkit. A Java Runtime Environment (JRE) is included with the JDK.

Before installing it,‌ decide what kind of Java you want to install. We recommend you follow these pointers before installing Java in your system:

  • As a regular user, you can only use JRE to run Java, which includes a JVM.
  • If you want to use a Java program as a developer, you must download JDK, including the JRE library and JVM.

Thus, the developers need tools like the compiler, JRE, JVM, debugger, and so on, to run the Java program. All these things are available in JDK. For this reason, in this article, we will install JDK.

How to Install Java in Linux?

Let’s divide this section into multi parts to explain the step-by-step process in brief:

Check If Java is Already Installed

First, check if Java is available in your system using the common Linux command

java –version

If the system detects the Java, the terminal will show the details about the currently installed version: 

If Java is unavailable in the system, it will show you the following output.

Installation Process of JRE

To install Java in Linux, you must first install the JRE from the Ubuntu repositories. The JRE downloads all its tools, including the Java class library, JVM, and Java. Execute the following command to install it:

sudo apt install default-JRE

On running the above command, it asks for permission to download some package, so press Y on the keyboard.

Thus, it installs the JRE in your system.

Installation Process of JDK

JDK on your Linux system is also easy, just as we have installed JRE. Now, run the following command to install the latest version of JDK.

sudo apt install default-JDK

The above command successfully installs the default version of the JRE available in your system or installs the default JDK (OpenJDK). On running the above command, it takes permission from you to download some packages. Permit by typing ‘Y.’

The above command installs JRE and Java on your machine. The following command can be used to verify the installed version of Java:

java –version


$ javac -version
javac 11.0.16.

In this way, you can install your Java compiler. Since the compiler is part of the JDK, it confirms the installation of the JDK.

Set the Default Java Program

Many Java versions are available by default in Java, like Oracle JDK and Default JDK, which you can easily install. Enter the following command to determine which default Java version is suitable for your Linux system:

sudo update-alternatives –config java

All the installations on the server are configured and displayed together with this command. You can choose any ‌of these. This is the only option you see when no Java installation is available on the server.

Through the JAVA_HOME variable, you can set the location of the Java installation in your Linux system. Many programs are written in this, which refers to the JRE, i.e. Java Runtime Environment.

Set JAVA_HOME Environment Variable

The Java program does not run if the JAVA_HOME variable is not set because the OS does not know where Java is in the system.

You can set JAVA_home using the vim editor in the following way. For this, use the following command:

sudo vi /etc/environment

OpenJDK is the default program of Java which you can install using the following path. Add the following to your file by pressing ‘i’ to enter insert mode:


Save the file by pressing ‘Esc’ and entering ‘:wq.’ In this way, you can set the JAVA_HOME variable. You can ‘echo’ the JAVA_HOME variable to validate if it’s correctly set. For this, first, go to the environment file by using the following command.

source /etc/environment

Now, you can check the path of your JAVA_HOME variable with the help of the following command.


You will ‌see the path of your environment variables as output if the above command is executed successfully in the terminal. It proves that you have set your environment variable correctly. Now you can run and develop Java applications in your Linux system.

How to Remove Java on Linux?

You can remove any Java packages you have installed. To remove the default JDK, you must enter any of the following commands in the terminal:

sudo apt purge openjdk-11-*
sudo apt remove –autoremove openjdk-11-*


This article explains how you can install Java on your Linux system. Installing Java isn’t a big deal whether you are a developer or a general user. You can install any variant of Java as per your requirement. Apart from this, here we have told you how you can run and develop your Java program by setting up Java variables.

Share Your Thoughts, Queries and Suggestions!