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.
To install Java OpenJDK via your terminal, follow the following steps:
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:
Open your terminal and edit the /etc/environment file using the nano command:
Append the following lines to the environment file and save it:
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:
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:
The above command displays the current version of Java compiler that is installed on your computer:
Installation of the Oracle Java is complete.