Install Oracle JDK 8 on Linux

Oracle Java is the proprietary, reference implementation for Java. This article shows you the way to manually install Oracle Java Development Kit 8 (Oracle JDK) on Linux.


Note: This article is using JDK8_Update_$java_update_no to demonstrate the installation. In the provided commands, replace the version specific paths and file names to your downloaded version.

Oracle JDK 16 has been released
Version specific installation guides are available here:


Step 1:
Download the latest JDK(jdk-8u$java_update_no-linux-x64.tar.gz) from this official site.
This article assumes that you have a x64 bit Linux distribution as most of the famous Linux distributions have dropped the support for x86 architecture. However, if you are using an x86 bit Linux distribution, download the jdk-8u$java_update_no-linux-i586.tar.gz from the official site and change the following commands according to the filename



If you want to download to a remote server or if you simply prefer wget, use the command given in this Stackoverflow answer: Downloading JDK

Step 2:
Open the terminal (Ctrl + Alt + T) and enter the following command.
sudo mkdir /usr/lib/jvm
If the /usr/lib/jvm folder does not exist, this command will create the directory. If you already have this folder, you can ignore this step and move to next step.

Step 3:
Enter the following command to change the directory.
cd /usr/lib/jvm

Step 4:
Extract the jdk-8u$java_update_no-linux-x64.tar.gz file in that directory using this command.
sudo tar -xvzf ~/Downloads/jdk-8u$java_update_no-linux-x64.tar.gz
According to this command, the JDK filename is jdk-8u$java_update_no-linux-x64.tar.gz and which is located in the ~/Downloads folder. If your downloaded file is in any other location, change the command according to your path.

Step 5:
Enter the following command to open the environment variables file.
sudo gedit /etc/environment

Step 6:
In the opened file, add the following bin folders to the existing PATH variable.
/usr/lib/jvm/$java_version/bin
/usr/lib/jvm/$java_version/db/bin
/usr/lib/jvm/$java_version/jre/bin
The PATH variables have to be separated by a colon.
Notice that the installed JDK version is 1.8 update $java_update_no. Depending on your JDK version, the paths can be different.
Add the following environment variables at the end of the file.
J2SDKDIR="/usr/lib/jvm/$java_version"
J2REDIR="/usr/lib/jvm/$java_version/jre"
JAVA_HOME="/usr/lib/jvm/$java_version"
DERBY_HOME="/usr/lib/jvm/$java_version/db"

The environment file before the modification:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
The environment file after the modification:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/$java_version/bin:/usr/lib/jvm/$java_version/db/bin:/usr/lib/jvm/$java_version/jre/bin"
J2SDKDIR="/usr/lib/jvm/$java_version"
J2REDIR="/usr/lib/jvm/$java_version/jre"
JAVA_HOME="/usr/lib/jvm/$java_version"
DERBY_HOME="/usr/lib/jvm/$java_version/db"

Save the changes and close the gedit. To learn more about setting environment variables and/or to set the environment variable without root privilege, check How to Set Environment Variables in Linux?.

Step 7:
Enter the following commands to inform the system about the Java's location. Depending on your JDK version, the paths can be different.
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/$java_version/bin/java" 0
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/$java_version/bin/javac" 0
sudo update-alternatives --set java /usr/lib/jvm/$java_version/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/$java_version/bin/javac

Step 8:
To verify the setup enter the following commands and make sure that they print the location of java and javac as you have provided in the previous step.
update-alternatives --list java
update-alternatives --list javac

Step 9:
Restart the computer (or just log-out and login) and open the terminal again.

Step 10:
Enter the following command.
java -version

If you get the installed Java version as the output, you have successfully installed the Oracle JDK in your system.
Previous
Next Post »

41 comments

Write comments
wizard_romania
AUTHOR
September 16, 2016 at 6:23 AM delete

Not a bad idea to also update default-java sym link:

sudo ln -sfn /usr/lib/jvm/jdk1.8.0_102/ /usr/lib/jvm/default-java

Reply
avatar
زياد هاني
AUTHOR
September 5, 2017 at 10:02 AM delete

Thanks

Reply
avatar
Muhammad Naeem
AUTHOR
May 9, 2019 at 5:27 AM delete

Great article. Thanks

Reply
avatar
ian cikal
AUTHOR
June 1, 2019 at 8:25 PM delete

Work on a first shoot, thanks..

Reply
avatar
RICARDO MENDEZ
AUTHOR
August 9, 2019 at 8:28 PM delete

This was very useful for me. Thank..

Reply
avatar
Nick00255
AUTHOR
August 18, 2019 at 10:22 AM delete

Thanks a lot, very useful.!!!!!!!!

Reply
avatar
Luiz Fellype Cassago de Souza
AUTHOR
August 22, 2019 at 11:23 AM delete

My environment file, had nothing in it.
The PATH variable was not there.

Reply
avatar
Gobinath
AUTHOR
August 22, 2019 at 7:54 PM delete

You can add the path to the environment. However to be on the safe side, I'd recommend you to add the following to ~/.bash_profile:



export PATH=$PATH:/usr/lib/jvm/jdk1.8.0_201/bin:/usr/lib/jvm/jdk1.8.0_201/db/bin:/usr/lib/jvm/jdk1.8.0_201/jre/bin
export J2SDKDIR=/usr/lib/jvm/jdk1.8.0_201
export J2REDIR=/usr/lib/jvm/jdk1.8.0_201/jre
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_201

Reply
avatar
Luiz Fellype Cassago de Souza
AUTHOR
August 28, 2019 at 6:13 PM delete

Can I just execute
sudo tar xvzf ~/Downloads/jdk-8u201-linux-x64.tar.gz

so, if I type just in home instead of /user/...
java -version
I got the response like it was installed.
is that a problem?

Reply
avatar
Gobinath
AUTHOR
August 28, 2019 at 7:59 PM delete

It's not a problem as long as you use it only to execute .java files. Some Java applications require JAVA_HOME and PATH. So if you set JAVA_HOME and PATH pointing to the current extracted folder then you can use Java from anywhere as the current user.


I recommend to extract it in /usr/lib/jvm because its the standard folder and it can be accessed by any users (including root).

Reply
avatar
Matthew
AUTHOR
September 25, 2019 at 2:19 PM delete

This is what I had been looking for. I got how to get the tar.gz file from oracle and unpack it in the proper directory from reading Oracle's instructions, but I needed to see steps 6 and 7 about setting up the environment to install jdk 8 in a usable form. Thanks for taking the time to share this, it helped me a lot.

Reply
avatar
-TalkingHead-
AUTHOR
December 2, 2019 at 9:58 PM delete

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_202/bin/java" 0

update-alternatives: error: alternative path is not absolute as it should be: usr/lib/jvm/jdk1.8.0_202/bin/java

Reply
avatar
Njoroge Anderson
AUTHOR
January 3, 2020 at 4:10 AM delete

Best method https://media3.giphy.com/media/TdfyKrN7HGTIY/giphy.gif

Reply
avatar
Barry
AUTHOR
January 18, 2020 at 7:30 PM delete

hey what happened between step 4 and 5 , unzip on Download then nothing ? how can we get to /bin ?

Reply
avatar
Gobinath
AUTHOR
January 20, 2020 at 9:27 AM delete

Hi,
The tar file contains the folder hierarchy including jdk1.8.0_231/bin

Reply
avatar
Peter795
AUTHOR
February 21, 2020 at 11:13 AM delete

How do I uninstall it?


Thanks and regards

Reply
avatar
Gobinath
AUTHOR
February 22, 2020 at 7:59 AM delete

1. Delete the alternatives using:

sudo update-alternatives --remove "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_231/bin/java" 0
sudo update-alternatives --remove "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_231/bin/java" 0



2. Revert the /etc/environmnet file


3. Delete the /usr/lib/jvm/jdk1.8.0_231 folder
sudo rm -rf /usr/lib/jvm/jdk1.8.0_231

Reply
avatar
Ronaldo Killergod
AUTHOR
March 17, 2020 at 4:19 PM delete

one problem I encountered was that my linux didn't have 32 bit backward compatibility package to install it, so I had to install the 32 bit package installer separately before installing it:
sudo apt-get install libstdc++6:i386 libgcc1:i386 zlib1g:i386 libncurses5:i386

Reply
avatar
Behnaz
AUTHOR
March 25, 2020 at 12:00 AM delete

Hi
I am trying to install Java version 8 using your guideline. The problem is I'm using MacOS version Catalina which does not allow writing to the root directory, so when I run
sudo mkdir /usr/lib/jvm
it gives me the error message:
mkdir: /usr/lib/jvm: Read-only file system
How can I fix this issue?

Reply
avatar
satwindersingh21
AUTHOR
March 30, 2020 at 6:18 AM delete

Thank You, Great Help.

Reply
avatar
joeyreseigne
AUTHOR
May 28, 2020 at 4:20 PM delete

hey im stuck on step 4. when i try to install it it says no such file or directory found and i cant install it

Reply
avatar
Gobinath
AUTHOR
June 1, 2020 at 9:25 AM delete

Steps 4 assumes that the downloaded file is in Downloads folder. Please change the path to the downloaded file location.

Reply
avatar
José Palacios
AUTHOR
July 1, 2020 at 10:25 AM delete

Thanks

Reply
avatar
Flor
AUTHOR
August 27, 2020 at 4:11 PM delete

Hi! I got stuck on step 7, it says the javac alternative is not registered, I installed the same version and followed all steps, what could be the issue?

Reply
avatar
Gobinath
AUTHOR
August 29, 2020 at 11:25 AM delete

Did you have any other versions of Java installed earlier? Can you run this command to delete the existing `/usr/bin/javac` and then try again.


sudo rm -rf /usr/bin/javac


Please let me know if this helps.

Reply
avatar
Fatih Anıl
AUTHOR
October 15, 2020 at 3:10 AM delete

I get an error message "bash: /usr/bin/java: can't find file or directory" although i implement all instructions here. What can i do to resolve this issue?

Reply
avatar
Will N
AUTHOR
November 26, 2020 at 3:41 PM delete

I also get the "bash: /usr/bin/java: can't find file or directory" error how would I go about fixing this?

Reply
avatar
Mahammad Hegazy
AUTHOR
February 24, 2021 at 4:21 AM delete

it is colon separating between path entries.. and not simicolon.. hope you update this.

Reply
avatar
Gobinath
AUTHOR
February 24, 2021 at 9:29 AM delete

Good catch. Thanks for pointing it out. I've updated the article.

Reply
avatar
Gobinath
AUTHOR
February 24, 2021 at 9:33 AM delete

Extremely sorry for the late reply. This usually happens if you install 32bit Java on 64bit machine. Please check if you've downloaded the jdk-8uxxx-linux-x64.tar.gz not jdk-8uxxx-linux-i586.tar.gz (assuming you are running 64bit Linux).

Reply
avatar
Gobinath
AUTHOR
February 24, 2021 at 9:34 AM delete

Extremely sorry for the late reply. This usually happens if you install 32bit Java on 64bit machine. Please check if you've downloaded the jdk-8uxxx-linux-x64.tar.gz not jdk-8uxxx-linux-i586.tar.gz (assuming you are running 64bit Linux). If it doesn't help, please see https://askubuntu.com/a/634028/421047

Reply
avatar
Dragonizedpizza
AUTHOR
March 2, 2021 at 11:35 AM delete

I could follow along through the whole guide, but I got this error.
zsh: exec format error: java
I'm attempting this on Linux Kali, but I assume this will be the same cos they're both debian based distros.

Reply
avatar
Gobinath
AUTHOR
March 2, 2021 at 11:07 PM delete

Hi, Whats the OS architecture and the Java architecture you've installed. Please note that this article is using 64bit Java but if your system is 32bit, you have to install jdk-8uxxx-linux-i586.tar.gz

Reply
avatar
Dragonizedpizza
AUTHOR
March 5, 2021 at 6:31 AM delete

Nevermind, I found the error.

I was using arm64 instead of x64.

Reply
avatar
Dragonizedpizza
AUTHOR
May 4, 2021 at 2:18 PM delete

The thing I downloaded was all the same, but instead had 291 at the end, instead of 281. What difference does this make?

Reply
avatar
Gobinath
AUTHOR
May 6, 2021 at 8:12 AM delete

For most of the Java developers it makes no difference. The last 3 digits are the minor version changes within JDK 8. Usually they contain small patches or performance improvements but no massive changes will be introduced in those minor version upgrades. Unless you have a problem with Java 8.281 and it is fixed in the next release, there is no reason to upgrade for those minor version upgrades.


In fact in my computer I have the 2.281 (the version available at the time I reinstalled my system) and I will not upgrade until I reinstall the system again :-)

Reply
avatar
Suhas Samprathi Jayathirtha
AUTHOR
May 6, 2021 at 3:27 PM delete

I have the package installed already
package jdk1.8-2000:1.8.0_291-fcs.x86_64 is already installed

But when i check on java -version, i still get the older java version itself.

java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b25)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b25, mixed mode)

How to change the java version to the newer version

Reply
avatar
Gobinath
AUTHOR
May 10, 2021 at 7:17 PM delete

Execute from Step 05 to 10 using the latest JDK installation path. Optionally you can remove the older version.

Reply
avatar
Ashwini
AUTHOR
June 28, 2021 at 9:09 AM delete

This is a very helpful explanation. Great Thanks!

Reply
avatar
October 18, 2021 at 1:00 AM delete

Worked for me 100%
https://youtu.be/zS5z2mWm1FU

Reply
avatar

Contact Form

Name

Email *

Message *