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
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

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 in­stal­la­tion.

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

Although numerous programs use or require Java under Linux, the cor­re­spond­ing packages often still need to be installed manually. To install Java on Ubuntu, a few re­quire­ments 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 En­ter­prise 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 rec­om­mend­ed for in­stalling Java on Ubuntu. Open JDK is an open-source variant of the Java Runtime En­vi­ron­ment (JRE) and Java Developer Kit (JDK).

Java for Ubuntu: In­stal­la­tion

In­stal­la­tion is simple and takes just a few Java commands.

  1. Launch the terminal with the key com­bi­na­tion [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 En­vi­ron­ment.
  2. Finally, check if Java has been installed on Ubuntu by using the command again:
$ java -version

If in­stal­la­tion was suc­cess­ful, the terminal will display your chosen version and you can start using Java straight away.

Install specific versions

While in­stal­la­tion using the default option is rec­om­mend­ed as this installs the latest version, you can select specific versions. This may be necessary in case you’re running programs or ap­pli­ca­tions 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 in­stal­la­tion was suc­cess­ful by entering the following command:

$ java -version

If in­stalling Java 8 on Ubuntu was suc­cess­ful, 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 in­stal­la­tion was suc­cess­ful 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 re­stric­tions on com­mer­cial use. In­stal­la­tion works as follows (using Oracle Java 11 as an example):

  1. Before creating a new repos­i­to­ry, enter the following command:
$ sudo apt install software-properties-common
  1. Add the repos­i­to­ry:
$ sudo add-apt-repository ppa:linuxuprising/java
  1. You’ll receive a message with details about your repos­i­to­ry. 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 in­stal­la­tion, you’ll be prompted to agree to the licensing agreement. Agree to continue.
  2. Check if the in­stal­la­tion was suc­cess­ful:
$ 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-al­ter­na­tives 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 al­ter­na­tive 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 in­stalling Java on Ubuntu: Set en­vi­ron­ment variable

Since some programs that work with Java use the en­vi­ron­ment variable JAVA_HOME to determine the in­stal­la­tion location, it makes sense to set it in advance. You do this using the command “update-al­ter­na­tives”:

$ sudo update-alternatives --config java
  1. All in­stal­la­tion paths are shown under “Path”. Select the path of the Java version you need and copy it.
  2. Open the /etc/en­vi­ron­ment 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.

Go to Main Menu