AlmaLinuxRHEL Based

How To Install OpenJDK on AlmaLinux 9

Install OpenJDK on AlmaLinux 9

AlmaLinux 9 has emerged as a leading choice for developers seeking a stable and secure operating system for their applications. It is a community-driven, open-source platform that offers a robust environment to run Java applications. One of the essential tools for Java development is OpenJDK, the open-source implementation of the Java Platform, Standard Edition, which provides all the necessary libraries and tools to develop, test, and run Java applications.

This guide will take you through the step-by-step process of installing OpenJDK on AlmaLinux 9. Whether you are a beginner looking to learn Java or an experienced developer needing to set up your environment, this article will equip you with the knowledge required for a successful installation. We will cover everything from system prerequisites to verifying your installation, ensuring that you are well-prepared to get started with Java development on AlmaLinux 9.

Prerequisites

Before diving into the installation process, it’s crucial to ensure that your system meets certain prerequisites. Here are the requirements for installing OpenJDK on AlmaLinux 9:

  • Operating System: A server running AlmaLinux 9. Ensure that your system is up to date and compatible with the latest version of OpenJDK.
  • User Permissions: You need a non-root user account with sudo privileges. This allows you to install software packages and make necessary system changes. If you don’t have a non-root user, refer to online resources on how to create one.
  • Internet Connection: A stable internet connection is necessary as you will be downloading the OpenJDK packages from the repository during the installation process.

Having these prerequisites in place will ensure that the installation process goes smoothly and without any hiccups.

Step 1: Update System Packages

Before installing OpenJDK, it is vital to update your system packages to their latest versions. This ensures that you have the most recent security patches and performance improvements. Updating your system is a straightforward process. Here are the steps to follow:

  1. Open Terminal: Access the terminal on your AlmaLinux 9 system. You can do this through SSH if you are accessing a remote server.
  2. Run System Update Command: Execute the following command to update the package index and upgrade all installed packages to their latest versions:
    sudo dnf update
  3. Review Updates: After running the command, DNF will display a list of available package updates. Review the list to ensure all necessary updates are included.
  4. Proceed with Updates: If you agree to the updates, confirm the process when prompted by typing ‘y’ (yes) and hitting Enter. This will initiate the update process.
  5. Reboot System (If Necessary): Some updates, especially kernel updates, may require a system reboot. You can reboot your system using the following command:
    sudo reboot

By keeping your system updated, you minimize the risk of conflicts or issues during the installation of OpenJDK, paving the way for a smoother setup experience.

Step 2: Install OpenJDK 11

Installing OpenJDK 11 on AlmaLinux 9 is a crucial step for developers looking to run Java applications. OpenJDK 11 is widely used and is a long-term support (LTS) version, making it a solid choice for application development. Follow these detailed steps to install OpenJDK 11 on your AlmaLinux system:

  1. Open Terminal: Start by opening your terminal if you haven’t already.
  2. Install OpenJDK 11: Use the following command to install OpenJDK 11 along with the development kit which includes the necessary tools:
    sudo dnf install java-11-openjdk java-11-openjdk-devel
    This command instructs DNF to download and install OpenJDK 11 from the AlmaLinux repositories.
  3. Verify the Installation: Once the installation completes, verify it by checking the installed version with this command:
    java --version
    You should see output detailing the version of OpenJDK 11 that has been installed, confirming a successful installation.
  4. Check Java Compiler Version: In addition, confirm that you can use the Java compiler (javac) by executing:
    javac --version
    This ensures that the development tools are also correctly installed.

If you encounter any errors during installation, ensure that your system repository settings are correctly configured and that you have internet access for downloading the packages. If a specific package cannot be found, you may need to enable additional repositories or check for repository updates.

Step 3: Install OpenJDK 17

Transitioning to OpenJDK 17 signifies adopting the latest features and enhancements available to Java developers. OpenJDK 17, officially released as a long-term support (LTS) version, brings performance improvements and new language features. Here’s how to install it on AlmaLinux 9:

  1. Open Terminal: Launch your terminal window to begin the installation.
  2. Install OpenJDK 17: Execute the following command to install both OpenJDK 17 and the development tools necessary for compiling and running Java applications:
    sudo dnf install java-17-openjdk java-17-openjdk-devel
    This command fetches the necessary packages from the AlmaLinux repositories and installs them on your system.
  3. Verify Successful Installation: After the installation concludes, check the installed version to confirm it was successful:
    java --version
    The output should reflect OpenJDK 17, indicating that it’s installed correctly.
  4. Check the Java Compiler Version: Ensure that the Java compiler is also installed by running:
    javac --version
    Seeing the version number of javac will confirm that you’ve everything set up correctly.

If any errors occur during this process, check your package manager settings and ensure you have a stable internet connection. Included relevant repository settings can be verified by referencing documentation specific to AlmaLinux.

Step 4: Set Default Java Version

If you have installed multiple versions of Java, such as OpenJDK 11 and OpenJDK 17, managing your Java environment is essential. The alternatives command in AlmaLinux provides a user-friendly way to set the default Java version that the system uses. Follow these steps to set your preferred Java version:

  1. Open Terminal: Access your terminal to start configuration.
  2. View Installed Java Versions: First, check which Java versions are currently installed by running the command:
    sudo alternatives --config java
    This will display a list of installed Java versions, along with their corresponding selection numbers.
  3. Select Default Version: Type the number of the Java version you want to use as the default and press Enter. For example, if you want to set OpenJDK 17 as your default version, input its corresponding number.
  4. Verify Default Version: To confirm the default version has been set, use the command:
    java --version
    The output should reflect the version you just set as default.
  5. Repeat for javac: If you need to set the default compiler version as well, run the same command for javac:
    sudo alternatives --config javac
    Again, select the desired version and confirm by running javac --version.

Setting the appropriate Java version is crucial for the successful execution of Java applications, avoiding compatibility issues and ensuring that the environment is correctly configured for development.

Step 5: Configure JAVA_HOME Environment Variable

Configuring the JAVA_HOME environment variable is a crucial step in ensuring that Java applications run correctly on AlmaLinux. This variable points to the location of your Java installation and is used by various applications and frameworks to locate Java libraries and executables. Follow these steps to set the JAVA_HOME variable on AlmaLinux 9:

  1. Open Terminal: Access your terminal to begin this configuration.
  2. Determine Java Installation Path: First, find the installation path of your Java version. This can typically be found at: “/usr/lib/jvm/java-11-openjdk” for OpenJDK 11 or “/usr/lib/jvm/java-17-openjdk” for OpenJDK 17. To verify, run:
    readlink -f $(which java)
    This command will show you the full path of the Java executable, and from there, you can deduce your installation path.
  3. Create a Configuration File: Next, create a new file for the JAVA_HOME variable, which will be loaded at startup. Use the following command:
    sudo nano /etc/profile.d/java.sh
    This opens the Nano text editor.
  4. Set JAVA_HOME: In the editor, add the following line (adjusting the path if you are using a different version):
    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
    Save your changes by pressing CTRL + O and then exit with CTRL + X.
  5. Make the Script Executable: Ensure the script is executable by running:
    sudo chmod +x /etc/profile.d/java.sh
  6. Load the New Configuration: To apply the changes you made, load the new profile by running:
    source /etc/profile.d/java.sh
  7. Verify JAVA_HOME: Finally, verify that the JAVA_HOME variable has been set correctly by using:
    echo $JAVA_HOME
    You should see the path that you defined, confirming that the configuration was successful.

Setting the JAVA_HOME variable ensures that Java applications run properly, allowing easy integration with build tools like Maven and Gradle, and improving overall environment configuration for development.

Troubleshooting Common Issues

While installing OpenJDK on AlmaLinux 9 is generally straightforward, you may encounter some issues. Here are some common problems and their solutions:

  • Package Not Found: If you receive a “package not found” error, ensure that your system’s repositories are up to date. Run sudo dnf update before attempting the installation again.
  • Dependency Conflicts: In case of dependency conflicts, try installing OpenJDK with the --allowerasing option: sudo dnf install java-17-openjdk java-17-openjdk-devel --allowerasing. This allows DNF to remove conflicting packages.
  • Java Command Not Found: If the java command is not recognized after installation, verify that the PATH environment variable includes the Java bin directory. You can add it temporarily with: export PATH=$PATH:$JAVA_HOME/bin
  • Incorrect Java Version: If the wrong version of Java is being used, double-check your alternatives configuration and JAVA_HOME setting.

Congratulations! You have successfully installed OpenJDK. Thanks for using this tutorial for installing OpenJDK on your AlmaLinux system. For additional or useful information, we recommend you check the official OpenJDK website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button