Installing Java on Ubuntu Latest Version

  • Last updated Apr 25, 2024

Since Ubuntu Operating System is based on Debian Linux, the process for installing Java on Ubuntu and Linux are the same.

In this tutorial, we will show you how to install Oracle Java and OpenJDK. You can install any one of them.

OpenJDK is open source and is free. Whereas Oracle Java is optimized and starting from Java 11, Oracle uses a new commercial license for Java. You may need to buy a license from Oracle if you want to use the official Oracle JDK in a commercial setting.

Installing Java OpenJDK

To install Java OpenJDK via your terminal, follow these steps:

  • Open a terminal, and run the following command to update the package manager. It ensures that your system has the latest information about available software packages and their versions. This allows you to later use the "apt" command to install or upgrade packages based on the updated package lists.
sudo apt update
  • Next, run the following command to install OpenJDK. Replace 16 with the Java version that you wish to install. For example, if you wish to install Java 19 then your command should look like this sudo apt install openjdk-19-jdk openjdk-19-jre:
sudo apt install openjdk-17-jdk openjdk-17-jre
  • After the installation is completed, you can verify if the JDK is installed correctly. To do this, open the Terminal. Type java -version and press Enter. It should display the installed Java version information as shown in the example below:
java version "17.0.2_231"
Java(TM) SE Runtime Environment (build 17.0.2_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)
  • Next, set JAVA_HOME, JAVA_JRE environment variables to your system path so that Java becomes accessible from anywhere in your computer. To do this, open a terminal and edit /etc/environment file. For example:

First command:

cd /etc/

Second command:

sudo nano environment

When the files opens, scroll down to the bottom and append the following lines (Replace java-11-openjdk-amd64 with your java installation folder name):

JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
JRE_HOME=/usr/lib/jvm/java-11-openjdk-amd64/jre

Finally, save the file (CTRL+O saves a Nano file. CTRL+X exits Nano).


Installing Java Oracle

To install Oracle Java on Ubuntu/Linux, follow these steps:

  • The new policy from Oracle now says that you need to have an Oracle account in order to download Oracle Java. If you don't have an Oracle account yet, you can create a new one by going to the link provided. Once you have created your Oracle account, make sure to verify your email address.
  • Open a browser and go to https://www.oracle.com/java/technologies/downloads/archive.
  • Look for Java SE downloads list. From that list, select the Java version that you wish to download.
  • Find Linux x64 Compressed Archive  (.tar.gz ) file and click on the download link.
  • Next, review and accept the Oracle Technology Network License Agreement for Oracle Java SE and then click on the download button.
  • You'll be redirected to the oracle login page. Sign-in into your Oracle account. When the download dialog box appears prompting you to run or save the file, choose to save the file to install it later.
  • Once the download finishes, open the command terminal on your computer. Then, go to the folder or place where you saved the downloaded file. Here's an example to help you understand better:
cd /home/my-username/Downloads/
  • After that, use the following command to extract the downloaded file with the .tar.gz extension. In this example, the downloaded file is named "jdk-11.0.12_linux-x64_bin.tar.gz". Make sure to replace this filename with the name of the file you downloaded.
sudo tar xvf jdk-11.0.12_linux-x64_bin.tar.gz
  • Once the extraction is finished, you need to create a directory called "jdk" inside the /opt location. You can do this by using the following command:
sudo mkdir -p /opt/jdk
  • Now, you need to move the extracted file to the /opt/jdk/ location. To do this, use the following command, replacing "jdk-11.0.12" with the name of your extracted file:
sudo mv jdk-11.0.12 /opt/jdk/
  • After moving the extracted file, you need to run a command in the terminal to set Java alternatives. Replace "jdk-11.0.12" with the name of your extracted file and replace the last number with the version of Java you are installing. In this example, we are using Java 11, so you will see "11" in the command below:
sudo update-alternatives --install /usr/bin/java java /opt/jdk/jdk-11.0.12/bin/java 11
  • Next, you need to set Java alternatives for the Java compiler. To do this, use the following command. Replace "jdk-11.0.12" with the name of your extracted file and replace the last number with the version of Java you are installing.
sudo update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk-11.0.12/bin/javac 11
  • Once the Java installation is finished, you can check the version of Java installed on your computer by using the following command in the terminal:
java -version
  • It should display the installed Java version information as shown in the example below:
java version "11.0.12" 2021-07-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.12+8-LTS-237)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.12+8-LTS-237, mixed mode)

Congratulations, you have successfully downloaded and installed the JDK on your Ubuntu/Linux. You can now start using Java for development purposes.