Java is one of the most popular languages today, and it is almost a necessity for any programmer since it is used everywhere on the Internet. And to stay updated with the web technologies, we will also need to get the latest version of Java in our system.
In this tutorial, we will look at how we can install Oracle’s latest edition of Java in our Ubuntu 18.04 System.
Step 1: Update System Packages
We first need to install the Java Development Kit (JDK) on our system. By installing this, we are also installing the Java Runtime Environment (JRE), which is necessary for compiling and executing any Java program.
Just to ensure that we do not have Java already on our system, type:
java --version
You’ll get an output similar to the below screenshot, indicating that it is not yet installed in the system.

There is a JDK version available from the apt
package manager, but it is of a slightly older version, so let’s use Oracle’s website directly to fetch Oracle’s latest JDK. As of the time of writing, the latest version is JDK 13.0.1.
NOTE: We need to download JDK manually since the software is NOT open source, but proprietary.
But before that, as always, update system critical packages first.
sudo apt update
Now, we are ready to download the JDK from Oracle’s mirror link.
Step 2: Download the JDK
We can download the file from a remote server using wget
.
You can get the links for the latest JDK from Oracle’s website.
Change the version of JDK according to your needs. I will be downloading version 13.0.1.
wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/13.0.1+9/cec27d702aa74d5a8630c65ae61e4305/jdk-13.0.1_linux-x64_bin.tar.gz

Step 3: Set Directories for our Java Installation
We need to set up a Directory for our new Java installation. I will install it at /usr/lib/jvm, which is also where the Java installation will take place if you use the apt
repositories.
Make the directory if it doesn’t exist, using:
sudo mkdir /usr/lib/jvm
Before entering the new directory, get the path of the current directory using:
pwd
We will extract the tar file from this directory to the new directory.
Enter the directory now using:
cd /usr/lib/jvm
Now, extract the downloaded tar.gz file, from your previous directory. (Eg, ~/Downloads)
sudo tar -xvzf ~/Downloads/jdk-13.0.1_linux-x64_bin.tar.gz
This will extract the tar.gz file and result in a single JDK folder, along with the downloaded version. For me, it is 13.0.1.

Now, we’re almost done! We just have to set some Environment Variables to make sure that the system recognizes java
based commands.
Step 4: Set Environment Variables
Open your favorite text editor, and go to the /etc/environment
file, which contains the list of system environment variables.
sudo vi /etc/environment
It would look something like this:

Add the following bin folder to the existing PATH variable, after adding a colon. We are appending to the PATH variable.
/usr/lib/jvm/jdk-13.0.1/bin
So basically we make the system recognize the Java binaries, located in the /usr/lib/jvm/jdk-13.0.1
directory. And binaries typically reside in the /bin
sub-directory.
Now, we also need to add one more line, to make the System be aware of the HOME directory for Java. Add this line to the end of the file
JAVA_HOME="/usr/lib/jvm/jdk-13.0.1"
Your file will now look something like this:

Exit the editor, after saving changes.
Step 5: Inform the System of Java’s location
Enter the following commands to inform the system about the Java’s location. Depending on your JDK version, the paths can be different.
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-13.0.1/bin/java" 0
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk-13.0.1/bin/javac" 0
sudo update-alternatives --set java /usr/lib/jvm/jdk-13.0.1/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/jdk-13.0.1/bin/javac

To verify the setup, enter the following commands and make sure that they print the location of java and javac
update-alternatives --list java
update-alternatives --list javac

Now, we are done with the necessary setup! We now simply need to restart our system to ensure that the changes are made. (We can also logout/login from the current Terminal Session)
Simply reboot using:
sudo reboot
After rebooting, you can now successfully work with the latest version of Java on your system!
To test your installation, check the java version using
java --version
An output similar to the below screenshot indicates that you are now ready to use Java!

Conclusion
In this tutorial, we saw how we could install and setup the latest version of Java on our Ubuntu 18.04 System.