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:


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

About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee

About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

Leave a Reply

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