Installing Java Using apt on Ubuntu
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.
Prerequisites Copy link
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.
Installing JRE/JDK Copy link
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 updateBefore installing Java on Ubuntu, check if it has already been installed:
java -versionIf 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.1Now, install the OpenJDK package:
sudo apt install default-jreAfter installation, verify the JRE presence:
java -versionYou 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-jdkVerify the compiler version:
javac -versionYou should see:
javac 11.0.23Installing Oracle JDK Copy link
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.debNow install:
sudo apt install ./jdk-21_linux-x64_bin.debCheck the Java version:
java -versionYou 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)Managing Java Versions Copy link
You can install multiple versions of Java on a single machine. To see the default version, use:
sudo update-alternatives --config javaIf 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 javacSetting the JAVA_HOME Variable Copy link
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 javaCheck 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/environmentAdd 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/environmentCheck if the changes were applied:
echo $JAVA_HOMEYou should see:
/usr/lib/jvm/java-11-openjdk-amd64/bin/This change applies to the current user, while others need to restart the system.
Conclusion Copy link
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.