Of course! Here is a comprehensive guide on how to install and manage Java 1.8 (also known as Java 8) on Ubuntu.
Java 1.8 is an older, Long-Term Support (LTS) version. While many applications still require it, it's important to know that it's no longer receiving public security updates from Oracle. For this reason, using an OpenJDK build from the Ubuntu repositories is often the recommended approach for security.
We'll cover two main methods:
- Recommended: Using the APT Repository (Easy & Automatic Updates)
- Alternative: Manual Installation from Oracle (For specific Oracle licensing needs)
Method 1: Recommended - Using the APT Repository (OpenJDK 8)
This is the simplest and most common method. It uses Ubuntu's built-in package manager (apt) to install a free, open-source version of Java 8 called OpenJDK. The main advantage is that apt will handle security updates for you.
Step 1: Update Your Package List
First, update your local package index to make sure you have the latest information about available packages.
sudo apt update
Step 2: Install OpenJDK 8
You can install the Java 8 Development Kit (JDK), which includes both the runtime environment (JRE) and development tools. This is what most developers need.
sudo apt install openjdk-8-jdk
If you only need the Java Runtime Environment (JRE) and not the compiler (javac) and other development tools, you can install the jre package instead:
sudo apt install openjdk-8-jre
During the installation, you will be asked to confirm the disk space usage and the installation of additional packages. Press Y and Enter to proceed.
Step 3: Verify the Installation
After the installation is complete, you can verify that Java 8 is installed and check its version.
java -version
You should see output similar to this, confirming the version is 1.8.x:
openjdk version "1.8.0_362"
OpenJDK Runtime Environment (build 1.8.0_362-8u362-ga-0ubuntu1~22.04-b09)
OpenJDK 64-Bit Server VM (build 25.362-b09, mixed mode)
You can also check that the Java compiler is installed if you chose the jdk package:
javac -version
Output:
javac 1.8.0_362
Step 4: Setting JAVA_HOME (Optional but Recommended)
Many applications and build tools (like Maven, Gradle, Tomcat) require the JAVA_HOME environment variable to be set. This variable points to the directory where Java is installed.
-
Find the Installation Path: The path is typically
/usr/lib/jvm/java-8-openjdk-amd64. You can find it by listing the contents of the/usr/lib/jvm/directory.ls /usr/lib/jvm/
Look for a directory named
java-1.8.0-openjdk-amd64or similar. -
Set the Environment Variable: You can set
JAVA_HOMEfor your current session by running:export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
However, this will be lost when you close your terminal. To make it permanent, add it to your shell's profile file (e.g.,
~/.bashrcor~/.profile).echo 'export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64' >> ~/.bashrc source ~/.bashrc
-
Verify
JAVA_HOME:echo $JAVA_HOME
This should now print the path you configured.
Method 2: Alternative - Manual Installation from Oracle
Use this method only if you have a specific requirement for the Oracle JDK (e.g., for licensing or compatibility with a proprietary product). This method is more manual and you will not get automatic security updates from apt.
Step 1: Download the Oracle JDK 8
- Go to the Oracle Java SE Development Kit 8 Downloads page: https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html
- Accept the license agreement.
- Under "Java SE Development Kit 8uXXX", download the
.tar.gzfile for Linux x64.
Step 2: Extract the Archive
Move the downloaded file to your home directory or a temporary location and extract it.
# Move the file (replace 'jdk-8uXXX-linux-x64.tar.gz' with your actual filename) mv ~/Downloads/jdk-8uXXX-linux-x64.tar.gz ~/ # Navigate to your home directory cd ~/ # Create a directory for Java if it doesn't exist sudo mkdir -p /usr/lib/jvm # Extract the archive to the system Java directory sudo tar -xzvf jdk-8uXXX-linux-x64.tar.gz -C /usr/lib/jvm/
Step 3: Set Up Alternatives
The update-alternatives tool helps you manage different versions of software. We'll use it to make your new Oracle JDK the default.
-
Find the path to the
bindirectory of your new JDK. It will be something like/usr/lib/jvm/jdk1.8.0_XXX/bin. -
Register the JDK with
update-alternatives:sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0_XXX/bin/java 1 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.8.0_XXX/bin/javac 1
-
Configure the Default: Now, run the following command to choose which version of Java to use as the default.
sudo update-alternatives --config java
A list will appear. If your Oracle JDK is listed, select its number to set it as the default. If it's the only one, it will be set automatically.
Do the same for
javac:sudo update-alternatives --config javac
Step 4: Set JAVA_HOME (Required)
This step is mandatory for the manual installation. The path is now different.
-
Find the Installation Path: It will be
/usr/lib/jvm/jdk1.8.0_XXX. -
Set the Environment Variable Permanently:
echo 'export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_XXX' >> ~/.bashrc source ~/.bashrc
Step 5: Verify the Installation
Check the version to confirm it's the Oracle JDK.
java -version
You should see output similar to this, indicating it's the Oracle build:
java version "1.8.0_XXX"
Java(TM) SE Runtime Environment (build 1.8.0_XXX-bXX)
Java HotSpot(TM) 64-Bit Server VM (build 25_XXX-bXX, mixed mode)
How to Switch Between Java Versions
If you have multiple Java versions installed (e.g., OpenJDK 11 and OpenJDK 8), you can easily switch between them using update-alternatives.
-
List all installed Java versions:
sudo update-alternatives --config java
This will show you all the versions
aptor you manually installed. -
Select a new version: Simply enter the number corresponding to the Java version you want to use and press
Enter. It will immediately become the default for your terminal session and for any system service that relies on it.
Uninstalling Java 8
If you installed via APT (Recommended Method):
This is very straightforward.
# To remove the JDK sudo apt remove openjdk-8-jdk # To remove the JRE sudo apt remove openjdk-8-jre # To clean up configuration files sudo apt autoremove
If you installed Manually (Oracle Method):
You need to manually remove the files and unregister the alternatives.
-
Unregister the alternatives:
sudo update-alternatives --remove java /usr/lib/jvm/jdk1.8.0_XXX/bin/java sudo update-alternatives --remove javac /usr/lib/jvm/jdk1.8.0_XXX/bin/javac
-
Delete the installation directory:
sudo rm -rf /usr/lib/jvm/jdk1.8.0_XXX
