How to Install Java on Ubuntu

This guide will show you how to install Java on Ubuntu. Java is everywhere, on your laptop, phone, server, and even some smart appliances. So, you should know how to install it. This guide will also show you how to remove Java and even change the default version of Java.

Something to note

There are many different implementations of Java. But, Oracle Java and OpenJDK are the two main implementations of Java. Amazon Web Services users can choose Amazon Corretto as an alternative.

Oracle trademarks the word “Java”. Thus, an implementation wishing to use that trademark must get an Oracle license. You can read more about it here.

So, most Java implementations instead choose to use OpenJDK or Amazon Corretto. Unlike Oracle Java, OpenJDK uses an open-source licensing model. As a result, OpenJDK places no restrictions on your ability to run OpenJDK. Or, you could use Corretto. Amazon Corretto is a no-cost distribution of OpenJDK with long-term support.

Install OpenJDK 11 on Ubuntu

Run the following commands to install Java 11 on Ubuntu:

sudo apt update
sudo apt install openjdk-11-jdk

Install OpenJDK 8 on Ubuntu

Run the following commands to install Java 8 on Ubuntu:

sudo apt update
sudo apt install openjdk-8-jdk

Verify your Java install

You can verify your Java install using the following command on Ubuntu:

java -version

This command will produce output like this:

openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2, mixed mode, sharing)

Remove OpenJDK from Ubuntu

You can uninstall or remove OpenJDK with the command below. However, you must change openjdk-11-jdk to openjdk-8-jdk if you wish to remove OpenJDK 8 instead of OpenJDK 11.

sudo apt remove openjdk-11-jdk

Set the default version of Java

You can install multiple versions of Java, and a such, you must set the default version for Ubuntu to use.

The following command will allow you to do this:

sudo update-alternatives --config java

You will see the following message if you only have one version of Java installed:

There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-11-openjdk-amd64/bin/java
Nothing to configure.

But, you will see this when you have many versions of Java installed:

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-8-openjdk-amd64/jre/bin/java   1081      manual mode

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

Next, provide the number of the version you want to use as the default and press enter.

Set your JAVA_HOME environment variable

You can use the update-alternatives command mentioned previously to get the installation path of Java.

As per the previous example:

  • OpenJDK 11 is installed at: /usr/lib/jvm/java-11-openjdk-amd64/bin/java
  • OpenJDK 8 is installed at: /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

Next, open the /etc/environment file to set the JAVA_HOME variable:

sudo nano /etc/environment

Then set the JAVA_HOME variable to the OpenJDK path you require:

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"

Note that you can remove the “/jre/bin/java” of the path.

Finally, run the following command for changes to take effect on your current shell:

source /etc/environment

You may also be interested in

YouTube player

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.