How to install Java on Ubuntu step by step

It’s well known that you can install Java on Windows 11 but adding it to a machine running Linux is just as easy. To install Java on Ubuntu all you need is the command sudo apt install default-jre.

Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups

Install Java on Ubuntu: Quick guide

  1. Launch terminal
  2. Update package index
  3. Check if Java is already installed on Ubuntu
  4. Request and confirm the current version via sudo apt install default-jre
  5. Check installation.

What to consider if you want to install Java on Ubuntu?

Although numerous programs use or require Java under Linux, the corresponding packages often still need to be installed manually. To install Java on Ubuntu, a few requirements must be met. For one, you should know your current version of Ubuntu so that you can download the correct packages. You will also need an Ubuntu server with a non-root sudo user. Simply install Open JDK.

Download Java for Ubuntu

Depending on your needs you can select from different versions: a Standard Edition (SE), the Enterprise Edition (EE), and a Micro Edition (ME). We recommend using the official website to install Java on Ubuntu. New versions of Java tend to be released every six months. Versions Open JDK 8, 11 and 17 are currently receiving extended support and are recommended for installing Java on Ubuntu. Open JDK is an open-source variant of the Java Runtime Environment (JRE) and Java Developer Kit (JDK).

Java for Ubuntu: Installation

Installation is simple and takes just a few Java commands.

  1. Launch the terminal with the key combination [Ctrl] + [Alt] + [T].
  2. Update the package index to install the latest version of Java on Ubuntu using the command:
$ sudo apt update
  1. Check if Java is already installed on Ubuntu. To do this, enter the following:
$ java -version
  1. If no prior version has been installed the following is shown:
Output
Command 'java' not found, but can be installed with:
apt install default-jre
apt install [first version available]
apt install [second version available]
usw.
  1. To install Java on Ubuntu enter the following command:
$ sudo apt install default-jre
  1. You’ll be asked if you wish to continue. Confirm with “Y” or “N” and hit Enter. This installs the Java Runtime Environment.
  2. Finally, check if Java has been installed on Ubuntu by using the command again:
$ java -version

If installation was successful, the terminal will display your chosen version and you can start using Java straight away.

Install specific versions

While installation using the default option is recommended as this installs the latest version, you can select specific versions. This may be necessary in case you’re running programs or applications that require older versions or Java operators.

Install Java 8 on Ubuntu

Java 8 is an older version but still fully supported. To install Open JDK 8, enter the following commands:

$ sudo apt install openjdk-8-jre

Check if installation was successful by entering the following command:

$ java -version

If installing Java 8 on Ubuntu was successful, you’ll see the following:

Output
openjdk version "1.8.0_162"
OpenJDK Runtime Environment (build 1.8.0_162-8u162-b12-1-b12)
OpenJDK 64-Bit Server VM (build 25.162-b12, mixed mode)

Install Java 11 on Ubuntu

You can also install Java 11 on Ubuntu using the following commands:

$ sudo apt install openjdk-11-jre

Check if the installation was successful using the command:

$ java -version

Install Oracle Java on Ubuntu

To install Oracle Java on Ubuntu, be sure to read the licensing agreement which contains restrictions on commercial use. Installation works as follows (using Oracle Java 11 as an example):

  1. Before creating a new repository, enter the following command:
$ sudo apt install software-properties-common
  1. Add the repository:
$ sudo add-apt-repository ppa:linuxuprising/java
  1. You’ll receive a message with details about your repository. Confirm this by hitting Enter.
  2. Update the list of packages with the following command:
$ sudo apt update
  1. Now install Java 11 on Ubuntu:
$ sudo apt install oracle-java11-installer
  1. Before installation, you’ll be prompted to agree to the licensing agreement. Agree to continue.
  2. Check if the installation was successful:
$ java -version
  1. The output shows you which version you’re using.

Set standard version

There are occasions when you may need to install multiple Java versions. This happens, for example, where some programs only support a certain version. To find out which Java version you’ve installed on your Ubuntu use the command:

$ java -version

You can set a default version to be used on the command line. Use the update-alternatives command to do so:

$ sudo update-alternatives --config java

A list of all installed versions will be displayed. Below the sentence “There are [number] choices for the alternative java (providing /usr/bin/java.)” you’ll find the versions (under “Path”), the priority (“Priority”), the status and a selection number (“Selection”). Confirm your settings with Enter. To set a different default version, enter the selection number and hit confirm.

After installing Java on Ubuntu: Set environment variable

Since some programs that work with Java use the environment variable JAVA_HOME to determine the installation location, it makes sense to set it in advance. You do this using the command “update-alternatives”:

$ sudo update-alternatives --config java
  1. All installation paths are shown under “Path”. Select the path of the Java version you need and copy it.
  2. Open the /etc/environment using the text editor or the following command in nano:
$ sudo nano /etc/environment
  1. Paste the copied path to the end of the file:
JAVA_HOME="[preferred path]"
  1. If a path is already set, replace it.
  2. To apply the changes, use:
$ source /etc/environment
  1. Check the set path:
$ echo $JAVA_HOME

The settings are applied to all users.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top