Java’s ubiquitous presence in software development makes its installation on Linux systems a common and necessary task for many developers, system administrators, and even hobbyists. Whether you’re setting up an environment for enterprise applications, Android development, or running specific Linux-based tools that depend on the Java Runtime Environment (JRE) or the Java Development Kit (JDK), understanding the installation process is crucial. Linux, with its diverse distributions like Ubuntu, Debian, Fedora, and CentOS, offers several methods to install Java, ranging from using the distribution’s package manager for simplicity to manually installing specific versions for granular control. This guide will walk you through the most prevalent and effective methods for installing Java on your Linux machine, ensuring you have the right tools for your projects.

Understanding Java Components: JRE vs. JDK
Before diving into the installation process, it’s important to distinguish between the Java Runtime Environment (JRE) and the Java Development Kit (JDK). This understanding will help you choose the correct package for your needs.
Java Runtime Environment (JRE)
The JRE is the software component that enables you to run existing Java applications. It includes the Java Virtual Machine (JVM), the core Java class libraries, and other supporting files. If you only need to execute Java programs and do not intend to develop them, the JRE is sufficient. It’s a lighter installation compared to the JDK.
Java Development Kit (JDK)
The JDK is a superset of the JRE and is intended for developers who need to write, compile, and debug Java applications. It includes all the components of the JRE, plus additional tools such as the compiler (javac), the debugger (jdb), and other development utilities. For anyone looking to create new Java software, the JDK is the essential choice.
The JDK itself often comes in two main flavors:
- OpenJDK: This is a free and open-source implementation of the Java Platform, Standard Edition (Java SE). It’s the default choice for many Linux distributions.
- Oracle JDK: This is Oracle’s proprietary implementation of Java SE. While it offers some features and performance optimizations not found in OpenJDK, it also comes with licensing considerations, especially for commercial use.
Installing Java via Package Managers
The most straightforward and recommended method for installing Java on Linux is through your distribution’s default package manager. This approach ensures that Java is integrated seamlessly with your system, handling dependencies and updates efficiently.
Ubuntu/Debian-based Distributions (apt)
Ubuntu and Debian-based systems utilize the Advanced Packaging Tool (apt) for software management. You can install OpenJDK packages with just a few commands.
Installing the Default OpenJDK Version
To install the default OpenJDK version available in your distribution’s repositories, open your terminal and execute the following commands:
-
Update your package list:
sudo apt updateThis command refreshes the list of available software packages and their versions from the configured repositories.
-
Install the default JDK:
bash
sudo apt install default-jdk
This will install the latest stable version of OpenJDK’s JDK as determined by your distribution.
Installing a Specific OpenJDK Version
If you require a particular version of OpenJDK (e.g., Java 11 or Java 17), you can search for available packages and install them specifically.
-
Search for available OpenJDK packages:
apt search openjdk-This command will list all packages related to OpenJDK. Look for packages named
openjdk-<version>-jdk. -
Install a specific version (e.g., OpenJDK 11):
bash
sudo apt install openjdk-11-jdk
Replaceopenjdk-11-jdkwith the package name corresponding to the version you wish to install.
Installing the JRE Only
If you only need to run Java applications and not develop them, you can install the JRE instead of the full JDK.
-
Install the default JRE:
sudo apt install default-jre -
Install a specific JRE version (e.g., OpenJDK 11 JRE):
bash
sudo apt install openjdk-11-jre
Fedora/CentOS/RHEL-based Distributions (dnf/yum)
Fedora, CentOS, and Red Hat Enterprise Linux (RHEL) systems use dnf (or yum on older versions) for package management.
Installing the Default OpenJDK Version
Similar to Debian-based systems, you can install the default OpenJDK.
-
For Fedora (using dnf):
sudo dnf update sudo dnf install java-latest-openjdk-develjava-latest-openjdk-develusually points to the most recent LTS (Long-Term Support) version. -
For CentOS/RHEL (using yum):
bash
sudo yum update
sudo yum install java-11-openjdk-devel
You might need to specify a version likejava-11-openjdk-develorjava-1.8.0-openjdk-develasjava-latestmight not be as readily available or may point to a different version depending on the distribution’s repositories.
Installing a Specific OpenJDK Version
To install a specific version on Fedora/CentOS/RHEL, you’ll again search for available packages.
- Search for available OpenJDK packages (Fedora):
bash
sudo dnf search openjdk

-
Search for available OpenJDK packages (CentOS/RHEL):
sudo yum search openjdk -
Install a specific version (e.g., OpenJDK 17 on Fedora):
sudo dnf install java-17-openjdk-devel -
Install a specific version (e.g., OpenJDK 8 on CentOS/RHEL):
bash
sudo yum install java-1.8.0-openjdk-devel
Installing the JRE Only
To install only the JRE on Fedora/CentOS/RHEL:
-
Fedora:
sudo dnf install java-latest-openjdkOr a specific version:
sudo dnf install java-17-openjdk -
CentOS/RHEL:
bash
sudo yum install java-11-openjdk
Or a specific version:
bash
sudo yum install java-1.8.0-openjdk
Verifying the Java Installation
After installation, it’s crucial to verify that Java has been installed correctly and that your system recognizes it.
-
Check the Java version:
java -versionThis command should output the version of the Java Runtime Environment installed.
-
Check the compiler version (if JDK was installed):
bash
javac -version
This command will display the version of the Java compiler (javac), confirming that the JDK is operational.
Managing Multiple Java Versions (Alternatives)
In many development scenarios, you might need to work with different Java versions simultaneously. For example, one project might require Java 8, while another necessitates Java 17. Linux offers robust mechanisms to manage these multiple installations.
Using update-alternatives (Debian/Ubuntu)
The update-alternatives system is a powerful tool for managing symbolic links in your system that define the default version of commands.
-
List available Java alternatives:
sudo update-alternatives --config javaThis command will present a list of installed Java environments, each with a corresponding number. You can then enter the number associated with the Java version you want to set as the default.
-
Configure the compiler (javac):
You can perform a similar configuration for thejavaccommand:sudo update-alternatives --config javac -
Adding new Java installations to
update-alternatives:
If you manually install Java or install a version not automatically registered, you can add it manually:
bash
sudo update-alternatives --install /usr/bin/java java /path/to/your/java/bin/java <priority>
sudo update-alternatives --install /usr/bin/javac javac /path/to/your/javac/bin/javac <priority>
Replace/path/to/your/java/bin/javaand/path/to/your/javac/bin/javacwith the actual paths to the Java executable and compiler. The<priority>is a number indicating the preference for this alternative (higher numbers have higher priority).
Setting JAVA_HOME Environment Variable
The JAVA_HOME environment variable is crucial for many Java-based tools and applications (like Apache Maven, Gradle, and build scripts) to locate the Java installation directory.
-
Find your Java installation path:
This often depends on how you installed Java. If you used package managers, it’s typically within/usr/lib/jvm/. For example:- OpenJDK 11:
/usr/lib/jvm/java-11-openjdk-amd64 - OpenJDK 17:
/usr/lib/jvm/java-17-openjdk-amd64
- OpenJDK 11:
-
Set
JAVA_HOMEtemporarily (for the current session):export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATHReplace the path with your actual Java installation directory.
-
Set
JAVA_HOMEpermanently:
To make this setting persistent across reboots, you need to add it to your shell’s configuration file. This is usually~/.bashrc,~/.zshrc, or~/.profile, depending on your shell.Open your shell’s configuration file (e.g.,
nano ~/.bashrc) and add the following lines at the end:export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATHSave the file and then reload your shell’s configuration:
source ~/.bashrcIf you manage multiple Java versions, you would typically adjust the
JAVA_HOMEpath and then useupdate-alternativesto ensure/usr/bin/javapoints to the desired version.
Installing Oracle JDK Manually
While OpenJDK is generally preferred for its open-source nature and widespread support on Linux, some users might require Oracle JDK for specific licensing or compatibility reasons. Oracle JDK is typically not available in standard distribution repositories and must be downloaded and installed manually.
Downloading Oracle JDK
- Visit the Oracle Java Downloads page: Navigate to the official Oracle Java SE Downloads website.
- Select the desired version: Choose the specific Java SE version (e.g., JDK 17 LTS, JDK 21 LTS).
- Download the Linux .tar.gz archive: Under the “Linux” tab, select the “x64 Compressed Archive” (usually a
.tar.gzfile).
Manual Installation Steps
-
Create a directory for Java installations: It’s good practice to have a dedicated directory for managing Java installations.
sudo mkdir -p /usr/local/java -
Extract the downloaded archive: Navigate to the directory where you downloaded the
.tar.gzfile and extract it to your newly created Java directory.cd ~/Downloads sudo tar -xvzf jdk-17.0.x_linux-x64_bin.tar.gz -C /usr/local/java/Replace
jdk-17.0.x_linux-x64_bin.tar.gzwith the actual name of your downloaded file and17.0.xwith the specific version number. -
Set up
update-alternatives(Recommended): This is the best way to manage the Oracle JDK alongside other Java installations.
First, identify thejdk.versionyou just installed (e.g.,jdk-17.0.x). Then, use theupdate-alternativescommand:sudo update-alternatives --install /usr/bin/java java /usr/local/java/jdk-17.0.x/bin/java 1 sudo update-alternatives --install /usr/bin/javac javac /usr/local/java/jdk-17.0.x/bin/javac 1 sudo update-alternatives --install /usr/bin/jar jar /usr/local/java/jdk-17.0.x/bin/jar 1 # Add other tools as neededThe
1at the end is the priority. You might want to use higher numbers for Oracle JDK if you intend to make it the default. -
Configure
JAVA_HOME: Set theJAVA_HOMEenvironment variable to point to the Oracle JDK installation directory.# Add to your shell's profile file (e.g., ~/.bashrc) export JAVA_HOME=/usr/local/java/jdk-17.0.x export PATH=$JAVA_HOME/bin:$PATHRemember to
sourceyour profile file after making changes. -
Verify the installation:
bash
java -version
javac -version
Ensure these commands now reflect the Oracle JDK version you installed.

Conclusion
Installing Java on Linux is a fundamental step for engaging with the vast ecosystem of Java-based applications and development tools. By leveraging your distribution’s package manager, you ensure a stable and well-integrated installation of OpenJDK. For advanced scenarios requiring specific versions or Oracle JDK, manual installation methods combined with tools like update-alternatives and careful management of environment variables provide the flexibility needed to cater to diverse project requirements. With Java properly set up, you are well-equipped to embark on your development journey or run the applications that power much of today’s digital landscape on your Linux system.
