Start by downloading the Oracle JDK 8 archive (jdk-8u301-linux-x64.tar.gz) from the official website. Since Oracle requires authantication, the file is typically fetched on a local machine and transferred to the server via SCP or SFTP.
Log into the Ubuntu 18.04 server and place the archive in a temporary location, for example ~/downloads:
mkdir -p ~/downloads
# Transfer jdk-8u301-linux-x64.tar.gz to ~/downloads (use scp or your preferred tool)
Create the installation directory and unpack the bundle:
sudo mkdir -p /opt/java
sudo tar -xzf ~/downloads/jdk-8u301-linux-x64.tar.gz -C /opt/java
This extracts the JDK into /opt/java/jdk1.8.0_301. To simplify future upgrades, create a symbolic link named current:
sudo ln -s /opt/java/jdk1.8.0_301 /opt/java/current
Define environment variables system-wide by creating a new script under /etc/profile.d:
sudo tee /etc/profile.d/jdk.sh > /dev/null <<'EOF'
export JAVA_HOME=/opt/java/current
export PATH=$JAVA_HOME/bin:$PATH
EOF
Load the new settings into the current session:
source /etc/profile.d/jdk.sh
Verify the installation by querying the Java version:
java -version
Expected output resembles:
java version "1.8.0_301"
Java(TM) SE Runtime Environment (build 1.8.0_301-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.301-b09, mixed mode)
If the java command is not found, ensure the script was sourced and that JAVA_HOME/bin is included in the PATH. For persistent availability, log out and back in, or reboot the machine.
To manage multiple Java versions, consider using update-alternatives:
sudo update-alternatives --install /usr/bin/java java /opt/java/current/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /opt/java/current/bin/javac 1
This approach keeps the system clean and makes switching implementations straightforward.