DEV Community

Cover image for How to Install Java on Linux Using a Tar.gz Archive
johanputra
johanputra

Posted on • Edited on

How to Install Java on Linux Using a Tar.gz Archive

Installing Java from a tar.gz archive requires manually extracting the package, configuring environment variables, and optionally setting the default Java version. This method is distribution-agnostic and is commonly used in server environments where package managers are restricted or when a specific Java version is required.

Prerequisites

  • Root or sudo access
  • A downloaded Java tar.gz archive (Oracle JDK or OpenJDK)

Step-by-Step Installation

1. Download the Java Tarball

Download the Java tar.gz archive from an official source:

Example (OpenJDK):

wget https://example.com/OpenJDK-XX_linux-x64_bin.tar.gz
Enter fullscreen mode Exit fullscreen mode

Note:
Always verify the checksum provided by the vendor to ensure file integrity.

2. Extract the Archive

Navigate to the directory containing the downloaded file and extract it:

tar -xzvf jdk-*.tar.gz
Enter fullscreen mode Exit fullscreen mode

Replace jdk-*.tar.gz with the actual filename if necessary.

3. Move the Extracted Directory

Move the extracted JDK directory to a standard installation path, such as /opt:

sudo mv jdk-* /opt/
Enter fullscreen mode Exit fullscreen mode

This keeps manually installed software isolated from system-managed packages.

4. Configure Environment Variables

Set the JAVA_HOME and update the PATH variable.

Edit the appropriate shell configuration file:

  • Bash: ~/.bashrc
  • Zsh: ~/.zshrc
  • System-wide (recommended for servers): /etc/profile.d/java.sh

Example (user-level configuration):

export JAVA_HOME=/opt/jdk-XX
export PATH=$PATH:$JAVA_HOME/bin
Enter fullscreen mode Exit fullscreen mode

Apply the changes:

source ~/.bashrc   # or ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Best Practice:
Avoid using wildcards (*) in JAVA_HOME. Always specify the exact directory name.

5. Verify the Installation

Open a new terminal session and verify the Java installation:

java -version
Enter fullscreen mode Exit fullscreen mode

You should see the installed Java version displayed.

Optional: Configure the Default Java Version (Linux Only)

If multiple Java versions are installed, use update-alternatives to manage the default version.

Register the Java Binary

sudo update-alternatives --install /usr/bin/java java /opt/jdk-XX/bin/java 1
Enter fullscreen mode Exit fullscreen mode

Select the Default Version

sudo update-alternatives --config java
Enter fullscreen mode Exit fullscreen mode

Choose the desired Java version from the list.

Best Practices and Recommendations

  • Prefer OpenJDK (Adoptium builds) for production and enterprise use.
  • Install Java under /opt or /usr/lib/jvm for consistency.
  • Use system-wide environment variables for shared servers.
  • Regularly update Java to address security vulnerabilities.
  • Avoid mixing package-manager Java installations with manual tarball installs unless strictly required.

You have now successfully installed Java on Linux using a tar.gz archive. This method provides full control over Java versions and is ideal for environments requiring strict version management.

Top comments (0)