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.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
Install Java on Ubuntu: Quick guide
- Launch terminal
- Update package index
- Check if Java is already installed on Ubuntu
- Request and confirm the current version via sudo apt install default-jre
- 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.
- Launch the terminal with the key combination [Ctrl] + [Alt] + [T].
- Update the package index to install the latest version of Java on Ubuntu using the command:
- Check if Java is already installed on Ubuntu. To do this, enter the following:
- If no prior version has been installed the following is shown:
- To install Java on Ubuntu enter the following command:
- You’ll be asked if you wish to continue. Confirm with “Y” or “N” and hit Enter. This installs the Java Runtime Environment.
- Finally, check if Java has been installed on Ubuntu by using the command again:
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:
Check if installation was successful by entering the following command:
If installing Java 8 on Ubuntu was successful, you’ll see the following:
Install Java 11 on Ubuntu
You can also install Java 11 on Ubuntu using the following commands:
Check if the installation was successful using the command:
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):
- Before creating a new repository, enter the following command:
- Add the repository:
- You’ll receive a message with details about your repository. Confirm this by hitting Enter.
- Update the list of packages with the following command:
- Now install Java 11 on Ubuntu:
- Before installation, you’ll be prompted to agree to the licensing agreement. Agree to continue.
- Check if the installation was successful:
- 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:
You can set a default version to be used on the command line. Use the update-alternatives command to do so:
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”:
- All installation paths are shown under “Path”. Select the path of the Java version you need and copy it.
- Open the /etc/environment using the text editor or the following command in nano:
- Paste the copied path to the end of the file:
- If a path is already set, replace it.
- To apply the changes, use:
- Check the set path:
The settings are applied to all users.