Install Java on Ubuntu or any other Linux Distro

Ubuntu is stable, secure, and flexible, making it a good match for Java programmers who value reliability and customization.

Install Java on Ubuntu or any other Linux Distro
Image by Michiel Leunens / Unsplash

Java is a popular programming language known for its versatility and scalability, making it an excellent choice for enterprise-level applications. Java enthusiasts often prefer Ubuntu and other Linux distributions due to their reliability and efficiency for development.

Ubuntu has a command-line interface, package management systems, and a supportive community, making it a streamlined and productive environment for Java developers.

How to Install Java on Ubuntu or Any Other Linux Distro

Whether you're a beginner or an experienced programmer, Ubuntu simplifies the Java installation process. In this tutorial, we'll guide you step by step, ensuring you confidently set up Java on your Ubuntu machine.

Installation is simple and only requires a few Java commands. Before proceeding, please check if Java is already installed by running the following command in your terminal.

java -version
If this command indicates that Java is not found, it means that it is not installed, and you can proceed with the next steps.

As with all Ubuntu software installs, use the following command to refresh your package list with the latest information.

sudo apt update

JDK or JRE

The complete Java suite consists of the Java Runtime Environment (JRE), Java Virtual Machine (JVM), and tools for creating Java source code.

The Java Development Kit (JDK) includes all the necessary components for running Java applications. To install the JDK, use the following command:

sudo apt install default-jdk

The JDK also includes some extra features that are helpful for software development, but you also have the option to install the JRE instead.

If you only want to install Java on Ubuntu without the additional development tools, you can use the following command to install only the JRE.

sudo apt install default-jre

Run Java in an Ubuntu terminal

java -version
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.

Set environment variable

Some Java programs use the JAVA_HOME environment variable to find the installation location. It is a good idea to set it beforehand.

Setting JAVA_HOME is not mandatory, but many enterprise Java applications like Tomcat or Kafka check the JAVA_HOME directory when they start up and use that information while running, so it is wise to configure it.

The JAVA_HOME environment variable points to the folder where Java is installed.

To locate your Java installation on Ubuntu, use the following command and copy the provided location:

sudo update-alternatives --config java

All installation paths will be displayed under "Path". Choose the path of the required Java version and copy it.

To add JAVA_HOME to the environment, open /etc/environment using a text editor or the following command in nano.

sudo nano /etc/environment

Paste the JAVA_HOME assignment at the bottom of the file:

JAVA_HOME="[preferred path]"

If a path is already specified, replace it and then prompt 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

The settings are applied to all users. This is how simple it is to install Java on Ubuntu using apt.