How to Install Java on Ubuntu Linux?
Although Ubuntu does not come with a Java runtime preinstalled, it comes with an easy way to install Java.
Java can be installed in a variety of ways on Ubuntu, but if all you need is to run a Java program or even develop and compile some Java source code, the apt-based installation path is the best.
Install Java on Ubuntu / Linux
Follow these steps to quickly setup and install Java on Ubuntu:
- Make sure Java has not already been installed.
- The command sudo apt update should be issued.
- Install Ubuntu’s default JDK by using the apt command.
- To test the installation, run Java on the command line.
- JAVA_HOME should be set globally for all Ubuntu users.
Do you really need to install Java on Ubuntu/Linux?
Java may not be needed on your machine for the following reasons:
- Windows is the operating system on your computer. Please refer to our article for instructions on how to install Java on Windows.
- Your computer runs on Mac OS. We have an article explaining how to install Java on Mac.
- If your OS already includes Java, you do not need to set it up.
Verify that Java has already been installed to be on the safe side. Run the following command:
java - version
You can proceed with the next steps if this command indicates that Java is not found.
Update package listing with apt update:
Whenever you install Ubuntu packages, make sure the package list is updated with the latest information by running 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.
Install Java on ubuntu/Linux with apt
There are three components of the Java suite: the Java Runtime Environment (JRE), the Java Virtual Machine (JVM), and utilities for developing Java source code.
JDK (Java Development Kit) provides everything a Java developer needs to develop and run Java applications.
You can install the JDK by running 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
A few additional features are also included with the JDK to facilitate software development.
Using the following command, you can install just the JRE on Ubuntu without any extra software development tools:
sudo apt install default-jre Setting up default-jre (2:1.11-72build2) ...
Run Java Through Ubuntu/Linux Terminal
In order to verify that Java has been successfully installed 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)
Java is successfully installed on Ubuntu if the java -version command outputs information about the version of Java, along with information about the runtime environment and JVM.
Configure JAVA_HOME on Ubuntu/Linux
Even though it isn’t absolutely necessary to configure JAVA_HOME, many enterprise Java applications, such as Tomcat or Kafka, query JAVA_HOME when they start up and utilize this information at runtime.
JAVA_HOME points to the directory where Java is installed.
Find your Java installation Path:
The following command will display the location where Java is installed on Ubuntu Linux:
$ 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
Open the server’s environment file with Nano after copying the Java installation location to the clipboard:
sudo nano /etc/environment
The JAVA_HOME path should be pasted at the bottom of the file:
Reload the environment configuration file by issuing below command in Ubuntu terminal:
In an Ubuntu terminal window, echo the JAVA_HOME environment variable:
echo $JAVA_HOME /lib/jvm/java-11-openjdk-amd64/bin/java
Commands When install Java on Ubuntu.
This tutorial showed you how to install Java on Ubuntu using apt. Here are all the commands we used:
sudo apt-get update
sudo apt install default-jdk
update-alternatives --config java
sudo nano /etc/environment
It’s that simple to install Java on Ubuntu with apt.