How to Install Java for Linux

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:

  1. 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
  2. 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 include openjdk-11-jdk, openjdk-17-jdk, openjdk-21-jdk, etc.
  3. Install a Specific OpenJDK Version: To install the latest available OpenJDK development kit, you can use the following command. Replace X with 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 install openjdk-X-jre instead. However, installing the JDK is generally recommended.
  4. 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.

  1. List Installed Java Versions:
    bash
    sudo update-alternatives --config java
  2. 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 javac and 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:

  1. Update Package Lists (Fedora):
    bash
    sudo dnf check-update

    Update Package Lists (CentOS/RHEL):
    bash
    sudo yum check-update
    # or
    sudo dnf check-update
  2. Search for Available Java Versions:
    bash
    sudo dnf search java-latest-openjdk
    # or
    sudo yum search java-latest-openjdk

    This will typically show packages like java-11-openjdk-devel, java-17-openjdk-devel, java-21-openjdk-devel (the -devel packages include the JDK).
  3. 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, install java-X-openjdk.
  4. Verify Installation:
    bash
    java -version

Managing Multiple Java Versions:

Similar to Debian/Ubuntu, you can use alternatives to manage multiple Java installations.

  1. 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:

  1. Update Package Lists:
    bash
    sudo pacman -Syu
  2. 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).
  3. 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:

  1. Download the JDK:

    • Oracle JDK: Visit the Oracle Java Downloads page (https://www.oracle.com/java/technologies/downloads/) and download the Linux .tar.gz archive 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.
  2. 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
    
  3. 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.gz with 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.

  4. Configure Environment Variables: You need to tell your system where to find the Java executables. This is typically done by setting the JAVA_HOME and updating the PATH environment 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/environment or 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.sh
      

      Add the following lines, adjusting the JAVA_HOME path to your extracted JDK version:

      export JAVA_HOME=/usr/local/java/jdk-X.Y.Z
      export PATH=$JAVA_HOME/bin:$PATH
      

      Save and close the file. Then, make the script executable:

      sudo chmod +x /etc/profile.d/java.sh
      

      To apply the changes without logging out and back in, you can source the file:

      source /etc/profile.d/java.sh
      
  5. Verify Installation:
    bash
    java -version
    javac -version
    echo $JAVA_HOME

    Ensure the output correctly reflects your manually installed Java version and that JAVA_HOME is 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!:

  1. Install SDKMAN!: Run the official installation script from your terminal.
    bash
    curl -s "https://get.sdkman.io" | bash
  2. Initialize SDKMAN!: Follow the on-screen instructions, which usually involve sourcing the SDKMAN! initialization script.
    bash
    source "$HOME/.sdkman/bin/sdkman-init.sh"
  3. Verify SDKMAN! Installation:
    bash
    sdk version

Installing Java with SDKMAN!:

  1. 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 like 17.0.8-tem or 11.0.19-zulu.
  2. Install a Specific Java Version: Use the install command 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.
  3. 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
  4. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

FlyingMachineArena.org is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. Amazon, the Amazon logo, AmazonSupply, and the AmazonSupply logo are trademarks of Amazon.com, Inc. or its affiliates. As an Amazon Associate we earn affiliate commissions from qualifying purchases.
Scroll to Top