Install Java on Ubuntu 18.04 | 20.04

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

You can either install Java OpenJDK or Oracle Java. Open JDK 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.

Install Java OpenJDK

To install Java OpenJDK via your terminal, follow the following steps:

  1. Open your terminal and run the following commands one by one:
    sudo apt update

    In this example, we are installing Java 11. You can install the other versions by replacing 11 with the Java version of your requirement.

    sudo apt install openjdk-11-jdk openjdk-11-jre
  3. After installation, verify the version of Java using the following command:
    java -version

    On running the above command, you should see the version of installed Java as shown in the example below:

    openjdk version "11.0.12"
    OpenJDK Runtime Environment (build 11.0.12_292-11u292-b10-0ubuntu1~20.04-b10)
    OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
  5. Next, set JAVA_HOME, JAVA_JRE environment variables to your system path so that Java becomes accessible from anywhere in your computer:
  6. Open your terminal and edit the /etc/environment file using the nano command:

    cd /etc/
    nano environment

    Append the following lines to the environment file and save it:


Install Oracle Java

To install Oracle Java basically involves downloading, installing, and verifying the Java version.

The new Oracle policy requires you to have an Oracle account before you can download Oracle Java. If you do not already have an Oracle account then you can Create a new Oracle Account here. After creating an Oracle account, verify your email.

Follow the steps below to install Oracle Java on Ubuntu/Linux:

  1. Go to the official Oracle Java 11 download page. Find other Java JDK version here
  2. Look for Linux x64 Compressed Archive and click on the corresponding .tar.gz file download link on the right.
  3. Next, accept the license agreement and then click on the download button.
  4. You'll be redirected to the oracle login page. Signin into your Oracle account. When the download dialog box appears prompting you to run or save the file, save the file to install it later.

  5. After the download is complete, open the command terminal and navigate to the location where you have downloaded the file. Example as shown below:
        cd /home/my-username/Downloads/
  7. Next, run the following command to extract the downloaded tar.zp file. Here, in this example, the downloaded file name is jdk-11.0.12_linux-x64_bin.tar.gz, replace this with the file name that you have downloaded.
        sudo tar xvf jdk-11.0.12_linux-x64_bin.tar.gz
  9. After the extraction is complete, create a directory, jdk inside the /opt location using the following command:
        sudo mkdir -p /opt/jdk
  11. Now, move the extracted file to the /opt/jdk/ location, using the following command. Replace jdk-11.0.12 with your extracted file name.
        sudo mv jdk-11.0.12 /opt/jdk/
  13. After moving the extracted file, execute the following command in the terminal to set java alternatives. Replace jdk-11.0.12 with your extracted file name and the last number with the Java version that you are installing (Since this example is based on Java 11, you'll see 11 in the command below).
        sudo update-alternatives --install /usr/bin/java java /opt/jdk/jdk-11.0.12/bin/java 11
  15. Next, set java alternatives for the Java compiler using the following command. Replace jdk-11.0.12 with your extracted file name and the last number with the Java version that you are installing.
        sudo update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk-11.0.12/bin/javac 11
  17. After the Java installation is complete, verify the Java version that is installed on your computer by running the following command in the terminal:
        java -version

    This above command displays the current version of Java that is installed 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)
  19. Next, you can verify the version of installed java compiler by running the following command in the terminal:
        javac -version

    The above command displays the current version of Java compiler that is installed on your computer:

        javac 11.0.12

Installation of the Oracle Java is complete.