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
sudoaccess - A downloaded Java
tar.gzarchive (Oracle JDK or OpenJDK)
Step-by-Step Installation
1. Download the Java Tarball
Download the Java tar.gz archive from an official source:
- OpenJDK (recommended): https://adoptium.net/
- Oracle JDK: https://www.oracle.com/java/technologies/downloads/
Example (OpenJDK):
wget https://example.com/OpenJDK-XX_linux-x64_bin.tar.gz
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
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/
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
Apply the changes:
source ~/.bashrc # or ~/.zshrc
Best Practice:
Avoid using wildcards (*) inJAVA_HOME. Always specify the exact directory name.
5. Verify the Installation
Open a new terminal session and verify the Java installation:
java -version
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
Select the Default Version
sudo update-alternatives --config java
Choose the desired Java version from the list.
Best Practices and Recommendations
- Prefer OpenJDK (Adoptium builds) for production and enterprise use.
- Install Java under
/optor/usr/lib/jvmfor 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)