How do I install Java on Ubuntu?
Although Ubuntu does not come with a pre-installed Java runtime, it does come packaged 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 installation path is based apt is the easiest to follow.
How to install Java on Ubuntu
To quickly configure and install Java on Ubuntu, follow these steps:
- Check that you have not yet installed Java
- Perform a sudo apt update command
- Install the default Ubuntu JDK with apt
- Run Java on the command line
- Set JAVA_HOME
Do you even need to configure Java in Ubuntu?
You do not need to configure Java on Ubuntu if someone has already installed it.
In the name of due diligence, first check that 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 has not been found, then it is not installed and you can proceed to the next steps.
Update the package list with apt update
As with all Ubuntu software installations, update the list of Ubuntu packages with the latest information in the following order:
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 with apt
To install the complete Java suite, which includes the Java Runtime Environment (JRE), Java Virtual Machine (JVM), and Java source code development utilities, install the Java Development Kit (JDK) in the following order:
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
JDK provides everything a user needs to run Java applications. It has some additional features bundled with it to facilitate software development.
To install Java on Ubuntu without any additional software development tools, you can only install the JRE with the following command:
sudo apt install default-jre Setting up default-jre (2:1.11-72build2) ...
Run Java on an Ubuntu terminal
To validate the correct installation 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 generates information about the version of Java installed, along with information about the JVM and runtime environment, then you have successfully installed Java on Ubuntu with apt.
Configure JAVA_HOME on Ubuntu
JAVA_HOME configuration is not absolutely necessary, but many Java enterprise applications such as Tomcat or Kafka consult the JAVA_HOME directory when starting and using this information at runtime, so it is wise to configure it.
The JAVA_HOME environment variable simply points to the folder where Java is installed.
Locate your Java installation on Ubuntu
To find out where you installed apt Java in Ubuntu, issue the following command and copy the provided location:
$ 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 installation on the clipboard, open the server environment file with Nano:
sudo nano /etc/environment
Paste the JAVA_HOME task at the bottom of the file:
Then force the Ubuntu terminal to reload the environment configuration file:
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
Ubuntu Java installation commands
As a quick fix, here are all the commands that were issued in this tutorial to install Java on Ubuntu with apt:
sudo apt-get update
sudo apt install default-jdk
update-alternatives --config java
sudo nano /etc/environment
And that’s how easy it is to install Java on Ubuntu with apt.