Oct 5, 2023 6 min read

How to Install Java on Ubuntu 18.04

Install Java on Ubuntu 18.04 with our step-by-step tutorial. It is the most extensively used programming language in the world.

Install Java on Ubuntu 18.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Java on Ubuntu 18.04, let’s briefly understand - What is Java?

Java is the most extensively used programming language in the world. It is used to build various cross-platform applications. It provides a flexible and versatile environment for developing computer programs, web and mobile applications, and embedded systems. With its "write once, run anywhere" principle, Java code can be executed on different devices without the need for recompiling.

Java's object-oriented nature, extensive library of pre-built components, and strong memory management make it a preferred choice for developers worldwide. Learn more about the fundamentals and benefits of Java for your programming needs.

In this tutorial, you will install OpenJDK and Oracle Java on Ubuntu 18.04. You can follow the same guidelines for Ubuntu 16.04 and other Ubuntu-based distribution, including Kubuntu, Linux Mint, and Elementary OS. We will also address some of the FAQs related to the Java installation.

Advantages of Java

  1. Platform Independence: Java's "write once, run anywhere" feature allows code to be executed on multiple devices and operating systems.
  2. Object-Oriented: Its object-oriented programming approach provides modular and reusable code, making development efficient and easier to maintain.
  3. Robust Memory Management: Java's automatic memory management eliminates memory leaks and makes it more reliable and crash-resistant.
  4. Vast Library Support: Java offers a rich set of libraries, frameworks, and APIs that accelerate development and provide solutions for various functionalities.
  5. Scalability: Java's ability to handle large-scale projects and its support for multi-threading enable the creation of high-performance applications that can scale seamlessly.

Prerequisites

You need to be logged in as a user with sudo privileges in order to successfully complete this tutorial.

Java Variants

Java has three different editions which are distributed: The Standard Edition (SE), The Enterprise Edition (EE), and The Micro Edition (ME). Here we will proceed with installing the Java SE (Standard Edition).

The two main implementations of Java are OpenJDK and Oracle Java. These are almost similar to one another except for the fact that Oracle Java has a few additional commercial features.

Ubuntu repositories have two different Java packages: Java Runtime Environment (JRE), and the Java Development Kit (JDK).

JRE consists only of the Java Runtime Environment, which is apt for running Java programs. JDK on the other hand includes development/debugging tools and libraries which is suitable for Java developers.

In case you don't know which Java version to use, you should stick to the default OpenJDK version available on Ubuntu 18.04.

Step 1 - Installing the Default OpenJDK

1) You need to update the apt package index:

sudo apt update

2) Then, install the default Java OpenJDK package:

sudo apt install default-jdk

3) You can then verify the installation with the help of the following command, which will show the Java version:

java -version

4) You should see an output similar to the one given below:

Output

openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)

This confirms Java installation on your Ubuntu system.

ℹ️
JRE is included in the JDK package. If you need only JRE, install the default-jre package.

Step 2 - Installing OpenJDK

Since Java 8 is still the most widely used version of Java, you can install it by using the following command if your application requires it:

sudo apt update
sudo apt install openjdk-8-jdk

Step 3 - Installing Oracle Java

It is recommended that you read the Oracle JDK  License before installing. This is because the license only allows non-commercial use of the software (personal use & development use)

You can install Oracle Java from the Linux Uprising PPA.

1) First, install the necessary dependencies for adding a new repository:

sudo apt install software-properties-common

2) After that, enable the Linux uprising PPA:

sudo add-apt-repository ppa:linuxuprising/java

3) Next, update the package list and install the oracle-java17-installer package:

sudo apt update
sudo apt install oracle-java17-installer

4) Accept the Oracle License when you are prompted to. Then, Verify the installation using the following command. You'll obtain the R version as the output:

java -version
Output

java version "17.0.2" 2021-09-08 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)

Step 4 - Set the default Java Version

Use the following command to check the default Java version:

java -version
Output

openjdk version "17.0.2" 2021-09-08
OpenJDK Runtime Environment (build 16.0.2+9-Ubuntu-3ubuntu118.04.3)
OpenJDK 64-Bit Server VM (build 16.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)

You can use the update-alternatives tool, if you have multiple Java versions installed, then to change the default version use:

sudo update-alternatives --config java
Output

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

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-16-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-16-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:

All you need to do is enter the version number and hit Enter to change the default version.

Step 5 - Set the JAVA_HOME Environment Variable

the JAVA_HOME environment variable is used by certain applications written in Java to determine the Java installation location.

1) In order to set the JAVA_HOME environment variable, you'll have to identify the Java installation paths using the update-alternatives command:

sudo update-alternatives --config java

The installation path in the tutorial are:

  • OpenJDK 16 is found in /usr/lib/jvm/java-16-openjdk-amd64/bin/java
  • OpenJDK 8 is found in /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

2) Copy the path of your preferred installation and then, open the /etc/environment file:

sudo nano /etc/environment

3) At the end of the line, add the following:

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

Ensure that you insert the path of your preferred Java version.

4) To apply the changes you have made, you can either use the given source command or simply log out and log in again:

source /etc/environment

5) You can check whether the JAVA_HOME environment variable is correctly set by using the given echo command:

echo $JAVA_HOME
Output

/usr/lib/jvm/java-16-openjdk-amd64
Note: /etc/environment is a system-wide configuration file, which is used by all users. If you want to set the JAVA_HOME variable on a per-user basis, add the line to the .bashrc or any other configuration file which is loaded when the user logs in.

Step 6 - Uninstalling Java

Uninstalling Java is just like uninstalling any other package, and can be done with the help of apt

For instance, you can run the following command to uninstall the openjdk-8-jdk package:

sudo apt remove openjdk-8-jdk

FAQs to Install Java on Ubuntu 18.04

Which Java version should I install on Ubuntu 18.04?

It's recommended to install OpenJDK, the open-source implementation of Java. Use OpenJDK 8 or OpenJDK 11 (LTS versions).

How can I verify that the Java installation is successful? 

Open a terminal and type java -version again. It should display the Java version without any errors.

How can I switch between different Java versions on Ubuntu 18.04? 

Use the update-alternatives command. Run sudo update-alternatives --config java and select the desired Java version from the list.

Can I install Oracle JDK on Ubuntu 18.04? 

Yes, but it requires additional steps. Download the Oracle JDK from their website, extract it, and set the necessary environment variables manually.

How do I uninstall OpenJDK from Ubuntu 18.04? 

Run sudo apt purge default-jdk to remove OpenJDK from your system.

Are there any alternative Java implementations available for Ubuntu 18.04? 

Yes, you can install other implementations like AdoptOpenJDK, Amazon Corretto, or Zulu OpenJDK using their respective installation methods.

How do I update Java to a newer version on Ubuntu 18.04? 

First, update your package index by running sudo apt update. Then, use sudo apt upgrade to upgrade Java to the latest version available in the repositories.

Conclusion

We hope this detailed guide helped you understand how to install Java on Ubuntu 18.04. To learn more about Java installation on Ubuntu 18.04, check out the official Java installation document.

If you have any queries, please leave a comment below and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.