How do I install Java on Ubuntu?

While Ubuntu does not come with a Java runtime preinstalled, it does come prepackaged with the ability to easily install Java with the apt command.

There are many different ways to install Java on Ubuntu, but if all you need is the ability to run a Java program or even develop and compile some Java source code, the apt-based installation path is the easiest one to follow.

How to install Java on Ubuntu

To quickly setup and install Java on Ubuntu, follow these steps:

  1. Verify that you have not already installed Java
  2. Issue the sudo apt update command
  3. Install Ubuntu’s default JDK with apt
  4. Run Java on the command line to test the install
  5. Set JAVA_HOME globally for all Ubuntu users

Do you even need to setup Java on Ubuntu?

You don’t need to setup Java on Ubuntu if someone has already installed it for you.

In the name of due diligence, first check to see if Java is already installed by issuing the following command:

java - version
Command 'java' not found, but can be installed with:
sudo apt install default-jdk
sudo apt install default-jre

If this command indicates that Java is not found, then it’s not installed and you can proceed with the next steps.

Update your package listing with apt update

As with all Ubuntu software installs, update your Ubuntu package list with the most recent information with the following command:

sudo apt-get update
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
Apache and Docker tutorials

Master the fundamentals of Apache, Docker and Kubernetes technology.

Install Java on ubuntu with apt

The full Java suite includes the Java Runtime Environment (JRE), Java Virtual Machine (JVM) and utilities to develop Java source code.

The Java Development Kit (JDK) provides everything a user needs to run Java applications. Install the JDK with the following command:

sudo apt install default-jdk
Setting up default-jdk-headless (2:1.11-72build2) ... 
Setting up openjdk-11-jdk:amd64 (11.0.15+10-0ubuntu0.22.04.1) ... 
using /usr/lib/jvm/java-11-openjdk-amd64/bin/jconsole 
Setting up default-jdk (2:1.11-72build2) ...

Optionally install the JRE instead

The JDK additionally has a few extra features packaged with it to facilitate software development.

To install Java on Ubuntu without any of the extra software development tools, you can install just the JRE with the following command:

sudo apt install default-jre
Setting up default-jre (2:1.11-72build2) ...

Run Java in an Ubuntu terminal

To validate the successful install of Java on Ubuntu with apt, issue the following command:

java -version
openjdk version "11.0.15" 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)

If the java -version command outputs information about the version of Java installed, along with information about the JVM and runtime environment, then you successfully installed Java on Ubuntu with apt.

Configure JAVA_HOME on Ubuntu

Configuring JAVA_HOME isn’t absolutely required, but many enterprise Java applications such as Tomcat or Kafka query the JAVA_HOME directory when they startup and use that information at runtime, so it’s prudent to configure it.

The JAVA_HOME environment variable simply points to the folder in which Java was installed.

Locate your Java installation on Ubuntu

To find out where apt installed Java on Ubuntu, issue the following command and copy the location provided:

$ update-alternatives --config java
There is only one alternative in link group java (providing /usr/bin/java): 
/usr/lib/jvm/java-11-openjdk-amd64/bin/java

Add JAVA_HOME to the environment

With the location of the Java install on the clipboard, open up the server’s environment file with Nano:

sudo nano /etc/environment

Paste the JAVA_HOME assignment at the bottom of the file:

JAVA_HOME="JAVA_HOME="/lib/jvm/java-11-openjdk-amd64/bin/java"

Then force the Ubuntu terminal to reload the environment configuration file:

source /etc/environment

You should then be able to echo the JAVA_HOME environment variable in an Ubuntu terminal window:

echo $JAVA_HOME
/lib/jvm/java-11-openjdk-amd64/bin/java
set JAVA_HOME ubuntu

Edit the environment file to set JAVA_HOME globally in Ubuntu.

Ubuntu Java install commands

As a quick review, here are all of the commands that were issued in this tutorial to get Java installed on Ubuntu with apt:

  • sudo apt-get update
  • sudo apt install default-jdk
  • java -version
  • update-alternatives --config java
  • sudo nano /etc/environment
  • source /etc/environment
  • echo $JAVA_HOME

And that’s how easy it is to install Java on Ubuntu with apt.

 

SearchAppArchitecture
SearchSoftwareQuality
SearchCloudComputing
SearchSecurity
SearchAWS
Close