Java, a ubiquitous programming language and platform, plays a critical role in a vast array of applications, from large-scale enterprise systems to the development of mobile apps and sophisticated backend services. For Linux users, particularly those involved in software development, system administration, or running applications that depend on the Java Virtual Machine (JVM), understanding how to install and manage Java is a fundamental skill. This guide delves into the most common and effective methods for installing Java on various Linux distributions, ensuring a smooth and efficient setup for your development or operational needs.

Understanding Java Editions and Installation Methods
Before proceeding with the installation, it’s crucial to distinguish between the different editions of Java and the various approaches to getting them onto your Linux system. The primary editions you’ll encounter are Java SE (Standard Edition), which is the core platform, and Java EE (Enterprise Edition), which is geared towards enterprise-level applications. For most development and general use, Java SE is what you’ll need.
Within Java SE, you’ll primarily deal with the Java Development Kit (JDK) and the Java Runtime Environment (JRE). The JDK includes the JRE, along with development tools like compilers, debuggers, and other utilities essential for writing and testing Java programs. The JRE, on the other hand, provides only the necessary components to run Java applications. If you are developing Java applications, you will need the JDK. If you are merely running pre-compiled Java applications, the JRE might suffice, though installing the JDK is generally recommended for its broader utility.
Linux distributions offer several avenues for installing Java:
- Package Managers: This is the most common, straightforward, and recommended method for most users. Distributions like Ubuntu, Debian, Fedora, CentOS, and Arch Linux all provide Java packages through their respective repositories. This method simplifies installation, updates, and uninstallation.
- Manual Installation from Oracle/OpenJDK Tarballs: For specific versions or to have finer control over the installation path, you can download the official JDK tarballs from Oracle or the OpenJDK project and install them manually. This method requires more manual configuration but offers maximum flexibility.
- SDKMAN!: This is a powerful tool for managing multiple Software Development Kits, including various versions of Java, on Unix-like systems. It’s an excellent choice for developers who need to switch between different Java versions frequently.
Using Package Managers for Seamless Installation
Leveraging your Linux distribution’s package manager is the most user-friendly and robust way to install Java. This approach ensures that Java is integrated properly with your system, and updates can be managed effortlessly through your system’s update mechanisms.
Installation on Debian/Ubuntu-based Distributions
Debian and Ubuntu, along with their derivatives like Linux Mint, offer OpenJDK as their primary Java implementation, which is open-source and fully compliant.
Installing OpenJDK:
- Update Package Lists: Before installing any new software, it’s good practice to update your local package index to ensure you’re getting the latest information about available packages.
bash
sudo apt update
- Search for Available Java Versions: You can search for available OpenJDK versions.
bash
apt search openjdk
This will list various OpenJDK packages, often with version numbers. Common packages includeopenjdk-11-jdk,openjdk-17-jdk,openjdk-21-jdk, etc. - Install a Specific OpenJDK Version: To install the latest available OpenJDK development kit, you can use the following command. Replace
Xwith the desired major version number (e.g., 11, 17, 21).
bash
sudo apt install openjdk-X-jdk
For instance, to install OpenJDK 17:
bash
sudo apt install openjdk-17-jdk
If you only need the Java Runtime Environment (JRE) to run applications, you can installopenjdk-X-jreinstead. However, installing the JDK is generally recommended. - Verify Installation: After the installation completes, check the installed Java version.
bash
java -version
You should see output indicating the installed OpenJDK version.
Managing Multiple Java Versions (if installed):
If you have multiple Java versions installed, you can manage which one is the default using the update-alternatives command.
- List Installed Java Versions:
bash
sudo update-alternatives --config java
- Select Default Java Version: The command will present a list of installed Java executables. Enter the number corresponding to the version you want to set as default and press Enter. This command also works for
javacand other Java-related executables.
Installation on Fedora/CentOS/RHEL-based Distributions
Fedora, CentOS Stream, and Red Hat Enterprise Linux (RHEL) also provide OpenJDK through their package managers (dnf for Fedora, yum or dnf for CentOS/RHEL).
Installing OpenJDK:
- Update Package Lists (Fedora):
bash
sudo dnf check-update
Update Package Lists (CentOS/RHEL):
bash
sudo yum check-update
# or
sudo dnf check-update
- Search for Available Java Versions:
bash
sudo dnf search java-latest-openjdk
# or
sudo yum search java-latest-openjdk
This will typically show packages likejava-11-openjdk-devel,java-17-openjdk-devel,java-21-openjdk-devel(the-develpackages include the JDK). - Install a Specific OpenJDK Version: To install the latest OpenJDK development kit, use:
bash
sudo dnf install java-X-openjdk-devel
For example, to install OpenJDK 17:
bash
sudo dnf install java-17-openjdk-devel
If you only need the JRE, installjava-X-openjdk. - Verify Installation:
bash
java -version
Managing Multiple Java Versions:
Similar to Debian/Ubuntu, you can use alternatives to manage multiple Java installations.
- List and Select Default Java:
bash
sudo alternatives --config java
Choose the desired version by entering its corresponding number.

Installation on Arch Linux
Arch Linux users can install OpenJDK from the official repositories.
Installing OpenJDK:
- Update Package Lists:
bash
sudo pacman -Syu
- Install OpenJDK: Arch typically offers the latest stable OpenJDK.
bash
sudo pacman -S jdk-openjdk
If you need a specific older version, you might need to use the Arch User Repository (AUR). - Verify Installation:
bash
java -version
Manual Installation of Oracle JDK or OpenJDK
While package managers are convenient, there are situations where you might prefer or need to install Java manually. This is often the case when you require a specific version not available in your distribution’s repositories or when you need to install Oracle’s proprietary JDK (though OpenJDK is generally preferred for its open-source nature).
Steps for Manual Installation:
-
Download the JDK:
- Oracle JDK: Visit the Oracle Java Downloads page (https://www.oracle.com/java/technologies/downloads/) and download the Linux
.tar.gzarchive for your desired version and architecture (x64 is common for modern systems). - OpenJDK: You can download OpenJDK tarballs from various sources, including Adoptium (https://adoptium.net/), which provides Temurin builds.
- Oracle JDK: Visit the Oracle Java Downloads page (https://www.oracle.com/java/technologies/downloads/) and download the Linux
-
Create an Installation Directory: It’s good practice to install Java in a dedicated directory. A common location is
/usr/local/java.sudo mkdir -p /usr/local/java -
Extract the Tarball: Navigate to the directory where you downloaded the tarball and extract it into your designated installation directory. Replace
jdk-X.Y.Z_linux-x64_bin.tar.gzwith the actual filename.cd /path/to/downloaded/file sudo tar -xvzf jdk-X.Y.Z_linux-x64_bin.tar.gz -C /usr/local/java/This will create a directory like
/usr/local/java/jdk-X.Y.Z. -
Configure Environment Variables: You need to tell your system where to find the Java executables. This is typically done by setting the
JAVA_HOMEand updating thePATHenvironment variables.-
For the current user (temporary):
bash
export JAVA_HOME=/usr/local/java/jdk-X.Y.Z
export PATH=$JAVA_HOME/bin:$PATH
-
For all users (permanent, recommended): Edit the system-wide environment configuration file. The exact file can vary, but
/etc/environmentor a new file in/etc/profile.d/are common.Create a new script file (e.g.,
java.sh) in/etc/profile.d/:sudo nano /etc/profile.d/java.shAdd the following lines, adjusting the
JAVA_HOMEpath to your extracted JDK version:export JAVA_HOME=/usr/local/java/jdk-X.Y.Z export PATH=$JAVA_HOME/bin:$PATHSave and close the file. Then, make the script executable:
sudo chmod +x /etc/profile.d/java.shTo apply the changes without logging out and back in, you can source the file:
source /etc/profile.d/java.sh
-
-
Verify Installation:
bash
java -version
javac -version
echo $JAVA_HOME
Ensure the output correctly reflects your manually installed Java version and thatJAVA_HOMEis set.
Using SDKMAN! for Flexible Java Version Management
For developers who frequently switch between different Java versions for various projects, SDKMAN! (Software Development Kit Manager) is an indispensable tool. It allows you to install, manage, and switch between multiple SDKs, including various OpenJDK builds, Groovy, Scala, and more, with ease.
Installation of SDKMAN!:
- Install SDKMAN!: Run the official installation script from your terminal.
bash
curl -s "https://get.sdkman.io" | bash
- Initialize SDKMAN!: Follow the on-screen instructions, which usually involve sourcing the SDKMAN! initialization script.
bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
- Verify SDKMAN! Installation:
bash
sdk version
Installing Java with SDKMAN!:
- List Available Java Versions:
bash
sdk list java
This command displays a comprehensive list of available Java versions and vendors (e.g., Temurin, Zulu, Corretto, Oracle). You’ll see identifiers like17.0.8-temor11.0.19-zulu. - Install a Specific Java Version: Use the
installcommand with the identifier of the Java version you want. For example, to install OpenJDK 17 from Temurin:
bash
sdk install java 17.0.8-tem
You can also install a specific vendor’s build. - Set a Default Java Version: Once installed, you can set a specific version as your default for the current shell or system-wide.
- For the current shell:
bash
sdk use java <version_identifier>
Example:sdk use java 17.0.8-tem - Set a permanent default:
bash
sdk default java <version_identifier>
Example:sdk default java 17.0.8-tem
- For the current shell:
- Verify Java Installation:
bash
java -version
SDKMAN! automatically handles setting up JAVA_HOME and the PATH for the selected Java version, making it incredibly convenient for managing multiple Java environments.

Conclusion
Installing Java on Linux is a straightforward process, especially when utilizing the robust package managers provided by your distribution. For developers needing flexibility, manual installation or tools like SDKMAN! offer powerful alternatives. By understanding these methods and choosing the one that best suits your needs, you can ensure your Linux system is properly equipped to run and develop Java applications efficiently. Maintaining updated Java installations is also crucial for security and access to the latest features and performance enhancements.
