How to Install OpenJDK 11 on Ubuntu 20.04 LTS

Java is a general-purpose, class-based, object-oriented multipurpose programming language that is popular due to the design of having lesser implementation dependencies, meaning that the compiled Java code can be run on all platforms that support Java without the need for recompilation. Java is also fast, secure, and reliable, therefore. It is widely used for developing Java applications in laptops, data centers, game consoles, scientific supercomputers, cell phones, etc.

The tutorial will look at installing the OpenJDK version instead of the default Oracle JDK. The difference between these two is licensing. OpenJDK is an entirely free open-source Java with a GNU General Public License, and Oracle JDK requires a commercial license under the Oracle Binary Code License Agreement. Other differences are release schedules and other factors that come into play; however, performance is pretty much the same.

In the following tutorial, you will learn how to install OpenJDK 11 LTS or better known as Java 11 LTS on Ubuntu 20.04 LTS Focal Fossa with the standard APT installation from Ubuntu’s repository along with the PPA version which may suit some users better. The tutorial will also demonstrate how to switch Java alternative default versions.

Update Ubuntu

Update your system to ensure all existing packages are up to date to avoid any conflicts during the installation.

sudo apt update && sudo apt upgrade -y

Install OpenJDK 11 – APT Method with Ubuntu

The first and most recommended option is to install OpenJDK using the default Ubuntu 20.04 repository. First, search to find what is available.

In your terminal, use the following command.

apt-cache search openjdk-11

Example output:

How to Install OpenJDK 11 LTS on Ubuntu 20.04 LTS

As the above output states, OpenJDK 11 JDE and JRE are available to install.

To begin the installation, use the following terminal command.

Install OpenJDK 11 LTS – JRE

sudo apt-get install openjdk-11-jre -y

Install OpenJDK 11 LTS – JRE

sudo apt-get install openjdk-11-jdk -y

Updates are handled with the standard apt update and upgrade commands. However, you can remove them separately or altogether if you no longer require JDK or JRE.

Example:

sudo apt-get remove openjdk-11-jre openjdk-11-jdk --purge

Note, this will remove any unrequited leftover dependencies and thoroughly wipe the installation and data as much as possible from your system.

Install OpenJDK 11 – APT Method with PPA

Ubuntu 20.04 repository often has the latest up-to-date and secure versions. An alternative method is to add the ppa:openjdk-r/ppa repository, which is always up to date and may see updates deployed quicker than Ubuntu’s default archives.

The PPA is untrusted, in short term, has no affiliation with the Ubuntu core team. However, it has been around for a long time and is widely used in larger communities.

Note, you do not need to remove and default OpenJDK installations. You can install this PPA directly. If any updates are available, it will simply prompt you to update your version from its repository.

First, add the PPA using the following command.

sudo add-apt-repository ppa:openjdk-r/ppa -y

Next, you can install JDK or JRE.

Install OpenJDK 11 LTS – JRE

sudo apt-get install openjdk-11-jre -y

Install OpenJDK 11 LTS – JRE

sudo apt-get install openjdk-11-jdk -y

Users that already have the above installed just run an update to check if any newer versions are available.

sudo apt update && sudo apt upgrade

For users that no longer wish to use this PPA, you will need to remove the OpenJDK versions off your system first.

sudo apt-get remove openjdk-11-jre openjdk-11-jdk

Then remove the PPA with the –remove syntax for removal.

sudo add-apt-repository --remove ppa:openjdk-r/ppa -y

Switching Alternative Java Versions

Once you installed Java you may have tried to check the version and see a newer version in the output, use the following command to first list the alternatives that are available to switch to.

sudo update-alternatives --config java

Example output:

How to Install OpenJDK 11 LTS on Ubuntu 20.04 LTS

So as above, you see the Java 11 in the listing with the number 1, type this and press enter key.

Example output:

How to Install OpenJDK 11 LTS on Ubuntu 20.04 LTS

Once done, re-check the default version available using the following command.

java --version

Example output:

How to Install OpenJDK 11 LTS on Ubuntu 20.04 LTS

To switch back to any other installed versions, use the same process.

Test Java – Create Hello World Application

To finish off, it is always handy to test installations of these kinds to confirm everything is working correctly after being installed. The easy way is to create a small test using the famous Hello World example.

First, create the Java program file as follows

sudo nano hello.java

Next, add the following Java code to the file below.

public class hello {
  public static void main(String[] args) {
    System.out.println("G'day from LinuxCapable!");
  }
}

Save the file CTRL+O, then exit CTRL+X.

Next, compile the code using the following terminal command javac.

javac hello.java

You may still encounter issues compiling, delete the file and use the following instead with the –release 11 <filename>.java.

javac --release 11 hello.java

Finally, run the Java code with the following command:

java hello

Example output:

How to Install OpenJDK 11 LTS on Ubuntu 20.04 LTS

Comments and Conclusion

In this tutorial, you have learned how to install Java 11 LTS on Ubuntu 20.04 LTS Focal Fossa. You should now be able to run Java applications on your system that require Java 11 or better yet compile code.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!