A2oz

How to Install Java in a Linux Server Step-by-Step

Published in Software Installation 3 mins read

Installing Java on a Linux server is a straightforward process. Here's a step-by-step guide:

1. Determine the Java Version You Need

  • OpenJDK: This is the open-source implementation of Java, widely available and often the default choice for Linux servers.
  • Oracle JDK: The proprietary version of Java, offering features like commercial support and advanced tools.

2. Choose Your Installation Method

You can install Java using a package manager or by downloading and installing the binaries directly.

A. Using a Package Manager (Recommended)

  1. Update Your System: Run sudo apt update (Debian/Ubuntu) or sudo yum update (Red Hat/CentOS) to ensure you have the latest package lists.
  2. Install Java:
    • OpenJDK:
      • Debian/Ubuntu: sudo apt install openjdk-11-jdk (replace 11 with the desired Java version)
      • Red Hat/CentOS: sudo yum install java-11-openjdk-devel (replace 11 with the desired Java version)
    • Oracle JDK: You'll need to download the Oracle JDK from https://www.oracle.com/java/technologies/downloads/ and follow the instructions in the downloaded file.
  3. Verify Installation: Run java -version to check the installed Java version.

B. Downloading and Installing Binaries

  1. Download the binaries: Go to https://www.oracle.com/java/technologies/downloads/ and download the appropriate Java version for your Linux distribution.
  2. Extract the binaries: Use the tar command to extract the downloaded archive. For example, tar -xvf jdk-11.0.14_linux-x64_bin.tar.gz.
  3. Set Environment Variables: Add the following lines to your shell configuration file (e.g., .bashrc, .zshrc):
    export JAVA_HOME=/path/to/jdk-11.0.14
    export PATH=$JAVA_HOME/bin:$PATH

    Replace /path/to/jdk-11.0.14 with the actual path to your Java installation directory.

  4. Apply changes: Run source ~/.bashrc or source ~/.zshrc to apply the changes.
  5. Verify Installation: Run java -version to check the installed Java version.

3. Configure Your Java Environment

  • Set Default Java Version: If you have multiple Java versions installed, you can use the update-alternatives command to set the default version. For example, to set OpenJDK 11 as the default:
    sudo update-alternatives --config java

    Select the desired Java version from the list.

4. Test Your Java Installation

  • Compile and Run a Java Program: Create a simple Java program (e.g., HelloWorld.java) and compile and run it using the javac and java commands.
  • Use a Java Development Environment (IDE): Install a Java IDE like Eclipse or IntelliJ IDEA to develop and test your Java applications.

Conclusion

By following these steps, you can successfully install Java on your Linux server and begin developing or running Java applications.

Related Articles