How to Install Java on Ubuntu 18.04

This article will teach you how to install Java on Ubuntu 18.04. The commands will work on any private server or computer running the latest version, Ubuntu 18.04 Bionic Beaver, as well as 16.04, and any other Long Time Support (LTS) releases. Java is usually distributed in three editions, namely Micro (ME), Enterprise (EE), and Standard Edition (EE). For this guide, we’ll use Java SE 11 (LTS).

Overall, Java is one of the leading programming languages in the world. It’s created to have as little dependencies as possible, which allows application developers to apply the “write once, run anywhere” philosophy. As a result, a compiled Java code can operate on any platform which supports Java. You can utilize it to build anything from simple web applications to more advanced pieces of software.

Without further ado, let’s see how we can install Java on Ubuntu 18.04.

JDK or JRE?

The OpenJDK (Java Development Kit) and Oracle JRE (Java Runtime Environment) are the two main kits you’ll work with. Oracle Java contains the JVM, the Java program, alongside other infrastructures, while the JDK is a superset of JRE. The JDK is essentially an SDK (software development kit), as it gives you access to everything in JRE plus tools such as jdb, javadoc, and the compiler itself – javac.

If you’re not interested in compiling Java software, the JRE will do just fine. If this changes, you can update it later.

The following commands imply you are already using a root user, but in case you’re not, you should add sudo at the beginning of your commands.

Install Java on Ubuntu Using the Default Package Manager

The first method to install Java on Ubuntu is through the default packages. Begin the process by updating the current packages to the latest version:

apt-get update && apt-get upgrade

Once it finishes, install the latest version of Java Runtime Environment (JRE) by executing this following command:

apt-get install default-jre

It is also possible to install the Java Development Kit (JDK) instead. It is required by specific software or used to compile Java programs. JDK includes JRE, therefore there’s no disadvantage if you choose this method instead.

apt-get install default-jdk

That’s it, Java is ready for use on your Ubuntu system. You can double-check if it was properly set up with these commands:

java -version
javac -version

The output will be similar to:

root@vps42681194:~# java -version
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)
root@vps42681194:~# javac -version
javac 11.0.6

Install Java on Ubuntu via Oracle JDK

Alternatively, it is possible to install Java on Ubuntu using the official Oracle JDK. Begin by updating your current packages to the latest version:

apt-get update && apt-get upgrade

For this example, we’ll use a third party library managed by WebUpd8. In order to implement it easier, install the following package first:

apt-get install software-properties-common

Next, get the Java Uprising PPA with the following command:

add-apt-repository ppa:linuxuprising/java

You might even want to run a quick update while you’re at it:

apt-get update

Finally, you can install Java on your Ubuntu system by executing the following command and following the on-screen instructions:

apt update; apt-get install oracle-java17-installer

Then make sure to choose “Yes” using your keyboard to accept the license terms and your installation will begin.

That’s it! Java is successfully installed. You may verify it by checking the version with these two commands:

java -version
javac -version

A similar output will appear:

root@vps42681194:~# java -version
java version "14" 2020-03-17
Java(TM) SE Runtime Environment (build 14+36-1461)
Java HotSpot(TM) 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)
root@vps42681194:~# javac -version
javac 14

Managing Java

A single server can have multiple Java installations. You can set the default version using the following command line:

update-alternatives --config java

The following output will appear:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
* 2            /usr/lib/jvm/java-14-oracle/bin/java          1091      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Enter the number from the selection menu which you wish to use as the default one. You can also do this with other Java commands, such as:

update-alternatives --config javac
update-alternatives --config javadoc
update-alternatives --config jarsigner

Setting the Java Home Environment

Another useful thing to know is how to set the JAVA_HOME variable. Most applications require it to find the location of your Java installation. The previously used command can help you locate the Java installation path:

update-alternatives --config java
here are 2 choices for the alternative java (providing /usr/bin/java).

 Selection  Path                     Priority  Status
------------------------------------------------------------
 0      /usr/lib/jvm/java-11-openjdk-amd64/bin/java  1111   auto mode
 1      /usr/lib/jvm/java-11-openjdk-amd64/bin/java  1111   manual mode
* 2      /usr/lib/jvm/java-14-oracle/bin/java     1091   manual mode

Press <enter> to keep the current choice[*], or type selection number: 

Once you’ve copied the installation path, you will need to edit the environment file located in the etc directory:

nano /etc/environment

Add the following line at the end to set the JAVA_HOME value:

JAVA_HOME="/usr/lib/jvm/java-14-oracle/bin/java"

Important! Don’t forget to update it with the actual path to your Java installation.

Press CTRL+X to finish editing and save the changes. Next, make the changes by applying this command:

source /etc/environment

You can double-check if it’s active by entering:

echo $JAVA_HOME

Conclusion

Congratulations, you now know how to install Java on Ubuntu 18.04. You’ve also learned some basics, such as setting the default Java version and defining the JAVA_HOME variable.

If you found this article useful, feel free to share it with the world. If you have any tips, suggestions, or ideas of your own, we eagerly await them in the comment section below.

Author
The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.