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 19 on Linux systems regardless of the distribution. Oracle JDK 19.0.1 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 19

Download the latest JDK(jdk-19_linux-x64_bin.tar.gz) from this official site:

If you want to download to a remote server or simply prefer the terminal to download the file, use the following command:

wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.tar.gz

Install Oracle JDK 19

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-19-linux-x64_bin.tar.gz file in the jvm directory using this command.

sudo tar -xvzf ~/Downloads/jdk-19_linux-x64_bin.tar.gz

Above command assumes, the JDK filename is jdk-19_linux-x64_bin.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.

Run the ls command to make sure that the jdk-19.0.1 folder is there. Prior to Java 17, the tar file used to have the minor version in the filename but from Java 17 onwards the tar filename doesn’t have the minor version. Therefore, make sure that you have the expected Java version and change the following commands according to the minor version.


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 folder to the existing PATH variable. Note that the PATH variables must be separated by a colon.

/usr/lib/jvm/jdk-19.0.1/bin

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

JAVA_HOME="/usr/lib/jvm/jdk-19.0.1"

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/jdk-19.0.1/bin"
JAVA_HOME="/usr/lib/jvm/jdk-19.0.1"

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/jdk-19.0.1/bin/java" 0
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk-19.0.1/bin/javac" 0
sudo update-alternatives --set java /usr/lib/jvm/jdk-19.0.1/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/jdk-19.0.1/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 19.0.1 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