How to Install JAVA on Linux?

Linux and JAVA, What a good combination and most required combination for running different softwares, databases, servers, etc.

There are several ways of installing Java in linux. Here, I will tell you three different ways of installing Java.

1- Using Default Package:

This will install the latest available java version in your package list. This will automatically set the Environment Variable. Run the following two commands only for installing JAVA by using default package:

sudo apt-get update

sudo apt-get install default-jre

Verify the installation by running the command “java -version”.

2- Using ppa repository:

You can install specific version of java by this method. This will automatically set the environment variable. I am going to install java 1.8 by this method. Run the following commands:

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update

sudo apt-get install oracle-java8-installer

sudo apt install oracle-java8-set-default

Verify the installation by running the command “java -version”.

3- Downloading and Installing manually:

In this method, you have to download .tar file of java manually and then install it manually. After installation, you have to set the environment variable path. BY this method, you can install specific version of java. I am going to install java 1.8 by this method.

First download the tar file of required java by running the following command:

wget -c –header “Cookie: oraclelicense=accept-securebackup-cookie” http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz

Create a directory for installing java by running the following command:

sudo mkdir /usr/lib/jvm

Change your directory to above created directory by running the following command:

cd /usr/lib/jvm

Extract the tar file by following command:

sudo tar -xvf ~/jdk-8u131-linux-x64.tar.gz

Now, add the environment variable. For this, open a file by the following command:

sudo vim /etc/environment

Add the path “/usr/lib/jvm/jdk1.8.0_131/bin” to the variable “PATH” and JAVA Home at the end of the file. For adding Java home, add the line “JAVA_HOME=”/usr/lib/jvm/jdk1.8.0_131” at the end of file.

Now run the following commands:

sudo update-alternatives –install “/usr/bin/java” “java” “/usr/lib/jvm/jdk1.8.0_131/bin/java” 0

sudo update-alternatives –install “/usr/bin/javac” “javac” “/usr/lib/jvm/jdk1.8.0_131/bin/javac” 0

sudo update-alternatives –set java /usr/lib/jvm/jdk1.8.0_131/bin/java

sudo update-alternatives –set javac /usr/lib/jvm/jdk1.8.0_131/bin/javac

update-alternatives –list java

update-alternatives –list javac

Now, Verify the installation by running the command “java -version”.

 

I hope this will be helpful for lot of people who still have confusion! Please comment if you find any confusion or need any help. And stay tunned for next post!

Thanks!

 

One thought on “How to Install JAVA on Linux?

Leave a comment