Java and the JVM (Java Virtual Machine) are often used in various applications. Therefore, it's important to know how to install free packages and alternative releases from Oracle, including JRE (Java Runtime Environment) and JDK (Java Development Kit).
This tutorial will guide you through installing Java on Ubuntu using apt
and selecting the preferred version.
You will need a system (your local machine or a cloud server) with Ubuntu 20.04 pre-installed, with root
and sudo
accounts, and a configured firewall.
The simplest way to get started is to use the release included in the standard Ubuntu package. Ubuntu 22.04 comes with OpenJDK 11, which includes open-source JRE and JDK packages.
First, download updates to the package index:
sudo apt update
Before installing Java on Ubuntu, check if it has already been installed:
java -version
If Java is not installed, you will see:
Command 'java' not found, but can be installed with:
apt install openjdk-11-jre-headless # version 11.0.20.1+1-0ubuntu1~22.04, or
apt install default-jre # version 2:1.11-72build2
apt install openjdk-17-jre-headless # version 17.0.8.1+1~us1-0ubuntu1~22.04
apt install openjdk-18-jre-headless # version 18.0.2+9-2~22.04
apt install openjdk-19-jre-headless # version 19.0.2+7-0ubuntu3~22.04
apt install openjdk-8-jre-headless # version 8u382-ga-1~22.04.1
Now, install the OpenJDK package:
sudo apt install default-jre
After installation, verify the JRE presence:
java -version
You should see:
openjdk version "11.0.23" 2024-04-16
OpenJDK Runtime Environment (build 11.0.23+9-post-Ubuntu-1ubuntu122.04.1)
OpenJDK 64-Bit Server VM (build 11.0.23+9-post-Ubuntu-1ubuntu122.04.1, mixed mode, sharing)
To compile and run even specific applications, you will also need the JDK package:
sudo apt install default-jdk
Verify the compiler version:
javac -version
You should see:
javac 11.0.23
In some cases, you may need the Oracle JDK package. This installation of Java on Ubuntu will differ from the methods described above, as it requires manual installation. You cannot install it from Ubuntu repositories using apt-get install
, but the process is still relatively easy.
The latest version is Java 22. However, Java 21 has the longest support. We will install Java 21 in Ubuntu using wget
and a download link copied from the Downloads section on the Oracle website.
Run:
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb
Now install:
sudo apt install ./jdk-21_linux-x64_bin.deb
Check the Java version:
java -version
You should see:
java version "21.0.3" 2024-04-16 LTS
Java(TM) SE Runtime Environment (build 21.0.3+7-LTS-152)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.3+7-LTS-152, mixed mode, sharing)
You can install multiple versions of Java on a single machine. To see the default version, use:
sudo update-alternatives --config java
If you've installed the previously mentioned versions, you will see:
Enter the desired version number or press Enter to keep the current settings (indicated by an asterisk). This also applies to other modules like the compiler javac
, keytool
, javadoc
, jarsigner
, etc.:
sudo update-alternatives --config javac
Most Java applications use the JAVA_HOME
environment variable to determine the installation directory.
In the previous chapter, we run this command:
sudo update-alternatives --config java
Check the paths in its output.
In our case, the directories are:
OpenJDK 11: /usr/lib/jvm/java-11-openjdk-amd64/bin/java
OpenJDK 8: /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Oracle Java 21: /usr/lib/jvm/java-8-oracle/jre/bin/java
Copy the desired path and edit the /etc/environment
file with nano
:
sudo nano /etc/environment
Add a new line with the copied path:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/bin/"
This sets the JAVA_HOME
variable for all user accounts. Save and exit the file, then reload the environment variables:
source /etc/environment
Check if the changes were applied:
echo $JAVA_HOME
You should see:
/usr/lib/jvm/java-11-openjdk-amd64/bin/
This change applies to the current user, while others need to restart the system.
We have covered the installation of Java in Ubuntu, including standard JRE/JDK versions and official Oracle releases. After installing the platform, you can use Java applications and dependencies without restrictions.