How to install Java in Linux

Java is one of the most popular programming languages across the world. It is used to develop different types of applications. For every programming language, installation is the basic and very first step. After the successful installation, we can use it on our machine. In this section, we will discuss how to install Java in Linux ( Ubuntu 18.04 LTS and 16.04 LTS), and other Linux based systems. Further, we will see the installation of different versions of Java.

We can install Java on Ubuntu via multiple packages such as JRE, OpenJDK and Oracle JDK. Also, we will discuss the difference between different types of installation packages. Before proceeding further, let’s have a look at the quick index of the topic:

Prerequisites of Installing Java in Linux

There are different hardware requirements for the Java programming languages. But, Java does not require much more heavy processors or Ram. So most of the modern systems are capable to run Java. In this section, we are not discussing the hardware requirement of the Java programming language instead we are going to discuss the software or access privilege.

To install Java in a Linux system, all we need to have the right of sudo privilege. You must be a root user to install any kind of tool in your Linux system.

JRE vs OpenJDK vs Oracle JDK

Before proceeding further, let’s understand different types of installation packages so that you can choose the right one. Let’s see the brief difference between JRE, OpenJDK, and Oracle JDK.

JRE (Java Runtime Environment): The JRE helps in executing normal Java-based applications. This is the package for you if you are not a developer. If you just want to run the Java application.

JDK (Java Development Kit): The JDK package is useful for developing and executing the Java applications. If you want to code in Java and develop the Java applications, this one is the right package for you.

Open JDK is the open-source package of the Java application.

Oracle JDK

Oracle JDK is the official Oracle version of the Java Development Kit.However, the OpenJDK is enough capable for most of the cases, but, some other programs like Android Studio suggests using Oracle JDK to enhance the UI/performance.

Now that you are familiar with Java packages, so let’s proceed further.

How to check whether Java is installed on my machine?

Before proceeding with the installation process, you must verify whether Java is installed on your machine or not. It is also useful for displaying the installed Java version. So that if you need to update it or not.

To check the installed version of the Java, execute the below command:

java -version

If the Java is installed on your machine, it will display the output as:

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~18.04-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

From the above output, we can see the Java 8 is installed on our machine.

Install Java in Ubuntu and Linux Mint

Let’s see how to install Java on our machine.

Install JRE in Ubuntu and Linux System

To install JRE in Ubuntu, execute the below command:

sudo apt install default-jre

The above command will install the latest JRE version. But to install a specific JRE version, execute the following commands:

sudo apt install openjdk-8-jre
sudo apt install openjdk-7-jre
sudo apt install openjdk-6-jre

The above commands will install the specified Java versions.

Install OpenJDK in Ubuntu and Linux Mint

To install the OpenJDK, execute the below command:

sudo apt install default-jdk

The above command will install the latest OpenJDK version. But to install a specific Java version, execute the following commands:

sudo apt install openjdk-8-jdk
sudo apt install openjdk-7-jdk
sudo apt install openjdk-6-jdk.

The above commands will install the Java8, Java7, Java6 respectively.

Install Oracle JDK in Ubuntu and Linux Mint

To install Oracle JDK in Ubuntu and Linux Mint, execute the following commands. Make sure you are connected to the internet because it will download plenty of files.

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

To install Java10 and make it default, execute the following commands:

sudo apt install oracle-java10-installer
sudo apt install oracle-java10-set-default

To install Java8 and make it default, execute the following commands:

sudo apt install oracle-java8-installer
sudo apt install oracle-java8-set-default

Similarly we can install any other Java version such as Java7, Java12, and more.

Verify the installation

To verify the installation, execute the following command. It will display the default version of the Java programming language.

java -version

How to set the default version of Java in Linux

If there are more than one Java packages installed on your machine you can change the default version by executing the below command:

sudo update-alternatives --config java

The above command will produce the output like:


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-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press <enter> to keep the current choice[*], or type selection number:

To change the default version enter the version number and press ENTER key.

How to set the Java environment variable in Linux

The JAVA_HOME environment variable is the default location for most of the Linux systems. To set the JAVA_HOME environment variable, first, we need to find out the Java installation paths. To find the Java installation path, execute the below command:

sudo update-alternatives --config java

The above command will display the installation paths of Java something like this:

Java installation paths in Linux

The next step is to set this path environment file. Copy the default path of your choice. and, open the /etc/environment file by executing the below command:

emacs /etc/environment

and add the “/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java” line into the last of the file. You can use any text editor like nano, vi, and more to do so. To verify whether the variable value set properly, execute the below command:

echo $JAVA_HOME

How to uninstall Java from Linux

To uninstall any Java version, execute the below command:

sudo apt remove openjdk-8-jdk

The above command will remove the specified Java version from your system. Similarly, you can remove any of Java versions like Java 10, Java7.

3 thoughts on “How to install Java in Linux”

Comments are closed.