How Do I Install Java on Linux?

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:

  1. Update your package list:

    sudo apt update
    

    This command refreshes the list of available software packages and their versions from the configured repositories.

  2. 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.

  1. Search for available OpenJDK packages:

    apt search openjdk-
    

    This command will list all packages related to OpenJDK. Look for packages named openjdk-<version>-jdk.

  2. Install a specific version (e.g., OpenJDK 11):
    bash
    sudo apt install openjdk-11-jdk

    Replace openjdk-11-jdk with 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.

  1. Install the default JRE:

    sudo apt install default-jre
    
  2. 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.

  1. For Fedora (using dnf):

    sudo dnf update
    sudo dnf install java-latest-openjdk-devel
    

    java-latest-openjdk-devel usually points to the most recent LTS (Long-Term Support) version.

  2. For CentOS/RHEL (using yum):
    bash
    sudo yum update
    sudo yum install java-11-openjdk-devel

    You might need to specify a version like java-11-openjdk-devel or java-1.8.0-openjdk-devel as java-latest might 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.

  1. Search for available OpenJDK packages (Fedora):
    bash
    sudo dnf search openjdk

  1. Search for available OpenJDK packages (CentOS/RHEL):

    sudo yum search openjdk
    
  2. Install a specific version (e.g., OpenJDK 17 on Fedora):

    sudo dnf install java-17-openjdk-devel
    
  3. 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-openjdk
    

    Or 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.

  1. Check the Java version:

    java -version
    

    This command should output the version of the Java Runtime Environment installed.

  2. 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.

  1. List available Java alternatives:

    sudo update-alternatives --config java
    

    This 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.

  2. Configure the compiler (javac):
    You can perform a similar configuration for the javac command:

    sudo update-alternatives --config javac
    
  3. 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/java and /path/to/your/javac/bin/javac with 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.

  1. 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
  2. Set JAVA_HOME temporarily (for the current session):

    export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
    export PATH=$JAVA_HOME/bin:$PATH
    

    Replace the path with your actual Java installation directory.

  3. Set JAVA_HOME permanently:
    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:$PATH
    

    Save the file and then reload your shell’s configuration:

    source ~/.bashrc
    

    If you manage multiple Java versions, you would typically adjust the JAVA_HOME path and then use update-alternatives to ensure /usr/bin/java points 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

  1. Visit the Oracle Java Downloads page: Navigate to the official Oracle Java SE Downloads website.
  2. Select the desired version: Choose the specific Java SE version (e.g., JDK 17 LTS, JDK 21 LTS).
  3. Download the Linux .tar.gz archive: Under the “Linux” tab, select the “x64 Compressed Archive” (usually a .tar.gz file).

Manual Installation Steps

  1. 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
    
  2. Extract the downloaded archive: Navigate to the directory where you downloaded the .tar.gz file 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.gz with the actual name of your downloaded file and 17.0.x with the specific version number.

  3. Set up update-alternatives (Recommended): This is the best way to manage the Oracle JDK alongside other Java installations.
    First, identify the jdk.version you just installed (e.g., jdk-17.0.x). Then, use the update-alternatives command:

    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 needed
    

    The 1 at the end is the priority. You might want to use higher numbers for Oracle JDK if you intend to make it the default.

  4. Configure JAVA_HOME: Set the JAVA_HOME environment 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:$PATH
    

    Remember to source your profile file after making changes.

  5. 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.

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