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:
- Open your terminal and run the following commands one by one:
- After installation, verify the version of Java using the following command:
- Next, set JAVA_HOME, JAVA_JRE environment variables to your system path so that Java becomes accessible from anywhere in your computer:
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
On running the above command, you should see the version of installed Java as shown in the example below:
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)
Open your terminal and edit the /etc/environment file using the nano command:
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:
- Go to the official Oracle Java 11 download page. Find other Java JDK version here
- Look for Linux x64 Compressed Archive and click on the corresponding .tar.gz file download link on the right.
- Next, accept the license agreement and then click on the download button.
- After the download is complete, open the command terminal and navigate to the location where you have downloaded the file. Example as shown below:
- 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.
- After the extraction is complete, create a directory, jdk inside the /opt location using the following command:
- Now, move the extracted file to the /opt/jdk/ location, using the following command. Replace jdk-11.0.12 with your extracted file name.
- 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).
- 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.
- After the Java installation is complete, verify the Java version that is installed on your computer by running the following command in the terminal:
- Next, you can verify the version of installed java compiler by running the following command in the terminal:
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.
sudo tar xvf jdk-11.0.12_linux-x64_bin.tar.gz
sudo mkdir -p /opt/jdk
sudo mv jdk-11.0.12 /opt/jdk/
sudo update-alternatives --install /usr/bin/java java /opt/jdk/jdk-11.0.12/bin/java 11
sudo update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk-11.0.12/bin/javac 11
This above command displays the current version of Java that is installed as shown in the example below:
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)
The above command displays the current version of Java compiler that is installed on your computer:
Installation of the Oracle Java is complete.