Java’s enduring presence in the software development landscape makes its installation on Linux a fundamental skill for many developers, system administrators, and even advanced users. Whether you’re setting up an environment for web server backends, desktop applications, or leveraging specialized tools that rely on the Java Virtual Machine (JVM), having Java installed and configured correctly on your Linux distribution is paramount. This guide will walk you through the process, covering common installation methods and essential post-installation steps.
Understanding Java Development Kit (JDK) and Java Runtime Environment (JRE)
Before diving into the installation, it’s crucial to understand the core components you’ll be dealing with:

Java Runtime Environment (JRE)
The JRE is the minimum requirement to run Java applications. It includes the Java Virtual Machine (JVM), core libraries, and supporting files. If your primary goal is to execute pre-compiled Java programs, the JRE is sufficient. However, for development purposes, you’ll need more.
Java Development Kit (JDK)
The JDK is a superset of the JRE and includes everything needed to develop, compile, debug, and run Java applications. Key components of the JDK include:
- Compiler (javac): Translates Java source code (.java files) into bytecode (.class files) that the JVM can understand.
- Debugger (jdb): A command-line tool for debugging Java applications.
- Archiver (jar): Used to package Java classes and resources into a single archive file.
- Documentation Generator (javadoc): Creates API documentation from source code comments.
- Other Development Tools: Including tools for code signing, serialization, and more.
For most users looking to install Java on Linux with the intention of development, installing the JDK is the recommended approach, as it encompasses the JRE.
Installation Methods on Linux
Linux distributions offer several avenues for installing Java, each with its advantages. The most common methods involve using the distribution’s package manager or downloading and installing the official binaries directly from Oracle or an open-source alternative like OpenJDK.
Using the Distribution’s Package Manager
This is often the simplest and most recommended method for many users, especially those new to Linux or who prefer a streamlined approach. Package managers handle dependencies and updates seamlessly.
Debian/Ubuntu-based Distributions (apt)
Debian and Ubuntu, along with their derivatives (like Linux Mint, Pop!_OS), use the apt package manager. You can install OpenJDK, the open-source implementation of Java, with just a few commands.
1. Update Package Lists:
It’s always a good practice to update your local package index before installing new software.
sudo apt update
2. Install OpenJDK:
You can install various versions of OpenJDK. The most common ones are OpenJDK 11 (LTS), OpenJDK 17 (LTS), and OpenJDK 21 (LTS). It’s generally advisable to install an LTS (Long-Term Support) version for stability.
-
To install OpenJDK 17 (a popular LTS):
sudo apt install openjdk-17-jdk -
To install OpenJDK 11 (another LTS):
sudo apt install openjdk-11-jdk -
To install the latest stable OpenJDK (might not be LTS):
sudo apt install default-jdk
3. Verify Installation:
After the installation completes, you can verify it by checking the installed Java version.
java -version
javac -version
These commands should output the version of the JDK you installed.
Fedora/CentOS/RHEL-based Distributions (dnf/yum)
Fedora, CentOS, and Red Hat Enterprise Linux (RHEL) use dnf (in newer versions of Fedora and RHEL) or yum (in older versions) as their package managers.
1. Update Package Lists:
-
For DNF:
sudo dnf update -
For YUM:
sudo yum update
2. Install OpenJDK:
Similar to Debian/Ubuntu, you can install different OpenJDK versions.
-
To install OpenJDK 17:
sudo dnf install java-17-openjdk-devel # Or for YUM: # sudo yum install java-17-openjdk-develThe
develpackage typically includes the JDK. -
To install the latest stable OpenJDK:
sudo dnf install java-latest-openjdk-devel # Or for YUM: # sudo yum install java-latest-openjdk-devel
3. Verify Installation:

java -version
javac -version
Manual Installation from Oracle JDK or OpenJDK Binaries
For users who require a specific version not readily available in their distribution’s repositories, or who need the official Oracle JDK (which has different licensing terms for commercial use), manual installation is an option. This method gives you more control but requires manual management of environment variables and updates.
1. Download the JDK:
Visit the official Oracle Java SE Downloads page or the Adoptium (formerly AdoptOpenJDK) website for OpenJDK binaries. Choose the appropriate Linux .tar.gz archive for your system architecture (e.g., x64).
2. Extract the Archive:
Create a directory where you want to store your Java installations. A common convention is /usr/local/java or /opt/java.
# Create a directory for Java installations
sudo mkdir -p /usr/local/java
# Navigate to the download directory and extract
cd /path/to/your/downloads
sudo tar -xvzf jdk-XX_linux-x64_bin.tar.gz -C /usr/local/java/
Replace jdk-XX_linux-x64_bin.tar.gz with the actual downloaded filename and XX with the version number.
3. Configure Environment Variables:
To make the java and javac commands available system-wide and to inform applications about your Java installation, you need to set environment variables. This is typically done by editing your shell’s profile file.
-
Edit the system-wide profile:
sudo nano /etc/profile -
Add the following lines to the end of the file:
export JAVA_HOME=/usr/local/java/jdk-XX export PATH=$PATH:$JAVA_HOME/binReplace
/usr/local/java/jdk-XXwith the actual path to the extracted JDK directory. -
Save and exit the editor (Ctrl+X, Y, Enter in nano).
-
Apply the changes:
source /etc/profileFor individual user profiles, you would edit
~/.bashrcor~/.profile.
4. Verify Installation:
java -version
javac -version
Managing Multiple Java Versions (using update-alternatives)
Many Linux distributions use the update-alternatives system to manage multiple versions of the same command. This is incredibly useful if you have multiple JDKs installed and need to switch between them easily.
1. Install Java (if not already done):
Make sure you have at least two different Java versions installed, preferably using the package manager for simplicity.
2. List Available Java Alternatives:
sudo update-alternatives --config java
This command will display a list of all installed Java executables and prompt you to select which one to use as the default java command.
3. Configure the Compiler:
Do the same for the Java compiler:
sudo update-alternatives --config javac
4. Select Your Preferred Version:
Enter the number corresponding to the Java version you want to set as the default. After making your selection, verify by running java -version and javac -version again.
Setting the JAVA_HOME Environment Variable
The JAVA_HOME environment variable is crucial for many Java-based applications and build tools (like Maven, Gradle, Tomcat) to locate your Java installation. While update-alternatives makes java and javac commands available, many tools explicitly look for JAVA_HOME.
If you installed Java using your distribution’s package manager, JAVA_HOME might already be set or can be set easily.
1. Find the JDK Installation Path:
If you installed via apt, the JDKs are usually located in /usr/lib/jvm/. You can find the exact path by looking at the output of update-alternatives --config java or by browsing the /usr/lib/jvm/ directory.
For example, if you installed openjdk-17-jdk, the path might be /usr/lib/jvm/java-17-openjdk-amd64.
2. Set JAVA_HOME:
-
For temporary use (current session):
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export PATH=$PATH:$JAVA_HOME/bin -
For permanent use (system-wide):
Edit/etc/environment(for all users) or~/.bashrc/~/.profile(for your user). Add the following lines:JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 PATH=$PATH:$JAVA_HOME/binThen, run
source /etc/environmentorsource ~/.bashrcrespectively to apply the changes.
3. Verify JAVA_HOME:
echo $JAVA_HOME
This should print the path you set.

Troubleshooting Common Issues
- “java command not found”: This usually means Java is not installed, or the
PATHenvironment variable is not correctly set to include the Javabindirectory. Double-check your installation andPATHconfiguration. - Incorrect Java Version: If
java -versionshows a different version than expected, ensure yourupdate-alternativesconfiguration is correct or that yourPATHis pointing to the desired installation. - Permissions Issues: Ensure you have the necessary permissions to install software and modify configuration files (usually requires
sudo). - Dependency Conflicts: Sometimes, installing Java manually can lead to conflicts with system packages. Using the distribution’s package manager is generally safer in this regard.
By following these steps, you can successfully install and configure Java on your Linux system, preparing it for development, application execution, and a wide range of other tasks.
