Java can be implemented in multiple ways. When it comes to Linux, there are two primary Java implementations: OpenJDK and Oracle Java JDK. These two are identical, except for Oracle Java, which has few commercial features and comes with some restrictions on how to use the software. Oracle Java license limits Java for personal and development usage only. You cannot use it for commercial purposes. OpenJDK is a free, open-source Java software that allows anyone from anywhere to freely use it as they see fit. OpenJDK consists of Java Runtime Environment (JRE) and Java Development Kit (JDK). JRE consists of the Java virtual machine (JVM), classes, and binaries that allow you to run Java programs. JDK includes JRE and development/debugging tools and libraries necessary to build Java applications. When you’re ready to install both Java software, use either method below: If you prefer to go with the open-source version, then install OpenJDK. If you instead want to install Oracle Java, then use the second method below.

How to install OpenJDK on Ubuntu

OpenJDK is the open-source version of Java. It’s highly compatible with Ubuntu and if you don’t know which version of Java to install, the open-source version should use selected. At the time of this writing, OpenJDK 17 is the latest stable version that can be installed on Ubuntu. OpenJDK (java.net) To install OpenJDK 17 on Ubuntu, run the commands below. If there’s a newer version available from the link above, then install it instead. Once OpenJDK is installed, you can verify and validate that Java is installed, by running the command below: That should display similar lines as shown below: OpenJDK is installed and validated. If you also want to install Java JRE, part of the OpenJava suite, then simply run the commands below: If you only want to run the bare minimum of Java Runtime, then run the commands below: Some applications still don’t fully support the latest OpenJDK 17. For those, they can install the previous Java LTS which was version 8. If you have multiple versions of Java installed, simply use the commands below to set which one should be the default for your system. You will be presented with a list of all installed Java versions. Enter the number of the version you want to be used as the default and press Enter Some programs require that JAVA_HOME is configured on the system. You can set the default home by using the lines above in the config file. For Java 17, it displays /usr/lib/jvm/java-17-openjdk-amd64 and Java 8, it’s /usr/lib/jvm/java-8-openjdk-amd64. To set their homes, run the commands below to open the system environment file. Then add a line for Java 17 as below: Run the commands below to save your changes. Verify that the JAVA_HOME environment variable was correctly set: That should output a similar line as shown below: That should do it! You have successfully installed OpenJDK on Ubuntu Linux.

How to install Oracle JDK on Ubuntu

If prefer to install Oracle JAVA instead of the open-source version above then use the steps below. At the time of this writing, the latest Oracle Java version is 17. Download the latest Java SE Development Kit 17 LTS release from it from the link below. Download Oracle Java Take notes of the version number you’re downloading. If there’s a newer version number than the one below, select it instead. You can also easily install the Java DEB package by running the commands below. At the time of this writing, the current latest version of Java JDK is jdk-17 You may have to replace the highlighted line in the commands below with the latest as it becomes available. Now that you’ve downloaded the correct archive package for your system, run the commands below to install Oracle Java. Again, the current latest version is jdk-17 After that, run the commands below to add Oracle Java as an alternative on Ubuntu. The commands below configure Ubuntu to use Java alternatives. Now when you run the commands again to list the available Java packages, you should see Oracle Java as an alternative. Oracle Java is # 4 on the list. You can enter it to make it the default. If you don’t have another version of Java installed, then the commands will return nothing. Next, run the commands below to make Java 17 to be the default Java compiler for your Ubuntu desktop. That should get Java installed and configured. To set JAVA environment variables, create a new file in the /etc/profile.d directory for Java JDK. Then copy and paste the lines into the end of the file and save. Next, run the commands below The commands above should configure Java to work and function with Ubuntu. To test if Java is installed correctly, run the commands below. You can now verify that Java is installed and configured by outputting its home directory. It should output a similar line as below: Conclusion: This post showed you how to install Java on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.