Even though OpenJDK is available in most of the official Linux repositories, some applications require Oracle Java Development Kit for better performance. This article shows you how to manually install Oracle JDK 8 on Linux systems regardless of the distribution. Oracle JDK 1.8.0_351 is used in this article to demonstrate the installation. In the provided commands, replace the version-specific paths and file names according to your downloaded version.

If you came here for a different version of Java, we got you covered. Here is the list of articles on how to install different versions of the Oracle Java Development Kit on Linux:

Download Oracle JDK 8

Download the latest JDK(jdk-8u351-linux-x64.tar.gz) from this official site:


Install Oracle JDK 8

Step 1:
Open the terminal (Ctrl + Alt + T) and enter the following command to create the parent directory to deploy the JDK. If the directory already exists, you can ignore this command and move to the next step.

sudo mkdir /usr/lib/jvm

Step 2:
Enter the following command to change the directory.

cd /usr/lib/jvm

Step 3:
Extract the jdk-8u351-linux-x64.tar.gz file in the jvm directory using this command.

sudo tar -xvzf ~/Downloads/jdk-8u351-linux-x64.tar.gz

The above command assumes the JDK filename is jdk-8u351-linux-x64.tar.gz and it is located in the ~/Downloads folder. If your downloaded file is in any other location, change the command according to your path.


Setup The Environment

Step 1:
Enter the following command to open the environment variables file. According to your personal preference, you can choose any text editor instead of nano.

sudo nano /etc/environment

Step 2:
In the opened file, add the following bin folders to the existing PATH variable. Note that the PATH variables must be separated by a colon.

/usr/lib/jvm/jdk1.8.0_351/bin
/usr/lib/jvm/jdk1.8.0_351/db/bin
/usr/lib/jvm/jdk1.8.0_351/jre/bin

Append the following environment variable to the end of the file.

J2SDKDIR="/usr/lib/jvm/jdk1.8.0_351"
J2REDIR="/usr/lib/jvm/jdk1.8.0_351/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_351"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_351/db"

A sample environment file before making these changes would look like the following:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

The same file after making the changes should look like this:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_351/bin:/usr/lib/jvm/jdk1.8.0_351/db/bin:/usr/lib/jvm/jdk1.8.0_351/jre/bin"
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_351"
J2REDIR="/usr/lib/jvm/jdk1.8.0_351/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_351"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_351/db"

Save the changes and close the editor. If you are using nano as in this article, the shortcut to save the changes is Ctrl + O; the shortcut to close nano is Ctrl + X. To learn more about setting environment variables and/or setting the environment variable without root privilege, check How to Set Environment Variables in Linux.


Step 3:
Sometimes an existing JDK installation might have a shortcut added to the /usr/bin directory. Therefore setting the PATH environment variable alone may not change the default Java compiler and runtime. To update these alternative shortcuts, run the following commands:

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

Step 4:
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 5:
Restart the computer (or just log out and log in) and open a new terminal.


Step 6:
Enter the following command.

java -version

If you get the Java version 1.8.0_351 as the output, you have successfully installed the Oracle JDK on your Linux system. If you have any questions or feedback, please comment below.

Share.
Exit mobile version