How to Install OpenJDK 8 on Ubuntu

OpenJDK 8 is a free, open-source implementation of the Java Platform used primarily to run and develop legacy Java applications. Despite being an older Long-Term Support release, OpenJDK 8 remains essential for enterprise software, build tools, and applications that require Java 8 compatibility. By the end of this guide, you will have OpenJDK 8 installed on Ubuntu using the default APT repository, with the ability to manage multiple Java versions and verify your installation.

Compare OpenJDK LTS Releases for Ubuntu

Before installing, confirm that OpenJDK 8 matches your project requirements. The table below compares LTS Java versions available in Ubuntu’s default repositories.

Java VersionLTS Support UntilChoose It WhenTrade-offs
OpenJDK 8December 2030 (community)Legacy applications, older frameworks like Spring 4.x, or vendor-certified deployments requiring Java 8Missing modern language features (no var, no records, no virtual threads); some libraries dropping Java 8 support
OpenJDK 11September 2027 (community)Enterprise applications, build servers, projects needing stable LTS with module system support and modern TLSLacks Java 21 features; some newer frameworks require 17+ as minimum
OpenJDK 17September 2029 (community)Spring Boot 3.x, Jakarta EE 10+, or workloads benefiting from records and sealed classesMissing virtual threads and newer pattern matching features from Java 21
OpenJDK 21September 2031 (community)New projects, high-concurrency applications using virtual threads, or development requiring the latest stable LTS featuresNewest LTS release; verify all dependencies support Java 21 before migrating production workloads

Choose OpenJDK 8 when your application explicitly requires Java 8 compatibility, your CI/CD pipeline targets this version, or you need to run legacy systems that cannot migrate to newer Java releases. For new projects without legacy constraints, consider starting with OpenJDK 17 instead.

Update Ubuntu Before OpenJDK 8 Installation

First, update your Ubuntu system to ensure all packages are current and avoid potential conflicts during installation:

sudo apt update
sudo apt upgrade

The first command refreshes your package index, while the second upgrades installed packages to their latest versions. Depending on how many packages require updates, this process may take a few minutes.

Install OpenJDK 8 on Ubuntu

Ubuntu’s default repositories include OpenJDK 8 packages, so installation is straightforward without adding external sources. Before installing, you can view the available OpenJDK 8 packages:

apt-cache search openjdk-8

This command displays all packages related to OpenJDK 8. You should see output similar to the following:

openjdk-8-jdk - OpenJDK Development Kit (JDK)
openjdk-8-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-8-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-8-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-8-source - OpenJDK Development Kit (JDK) source files

Install JRE or JDK

Next, choose the package that matches your needs. If you only need to run Java applications, install the Java Runtime Environment (JRE):

sudo apt install openjdk-8-jre

Alternatively, if you plan to develop Java applications or use build tools like Apache Maven, install the full Java Development Kit (JDK). This includes the JRE plus development tools such as the Java compiler:

sudo apt install openjdk-8-jdk

Verify the Installation

After installation completes, verify that Java is accessible by checking the installed version. Note that OpenJDK 8 uses the single-hyphen -version flag, not the double-hyphen --version used by Java 9 and later:

java -version

You should see output confirming OpenJDK 8 is installed:

openjdk version "1.8.0_xxx"
OpenJDK Runtime Environment (build 1.8.0_xxx-8uxxx-bxx)
OpenJDK 64-Bit Server VM (build 25.xxx-bxx, mixed mode)

If you installed the JDK, also verify the Java compiler is available:

javac -version
javac 1.8.0_xxx

Manage Multiple Java Versions

If you have multiple Java versions installed on your system, Ubuntu provides the update-alternatives command to switch between them. This feature is particularly useful when different projects require different Java versions.

Switch Between Java Versions

To view all installed Java versions and select one as the system default, run the following command:

sudo update-alternatives --config java

This command displays an interactive menu showing all available Java installations with their paths and priorities:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      auto mode
  1            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Enter the selection number corresponding to OpenJDK 8 (in the example above, type 1) and press Enter. After making your selection, verify the change by running java -version again.

Set JAVA_HOME Environment Variable

Many Java applications and build tools require the JAVA_HOME environment variable to locate your Java installation. For detailed instructions on configuring this variable persistently, see our guide on setting the Java environment path in Ubuntu.

As a quick reference, you can find the path to your active Java installation with:

dirname $(dirname $(readlink -f $(which java)))

On 64-bit Ubuntu systems with OpenJDK 8 as the active version, this command returns:

/usr/lib/jvm/java-8-openjdk-amd64/jre

Test the Installation

To confirm that Java compiles and runs correctly, create a simple test program. This step verifies that both the runtime and the compiler are functioning as expected.

Create a Test Program

First, create a new Java source file using your preferred text editor:

nano Hello.java

Next, add the following Java code to the file:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello from LinuxCapable!");
    }
}

Save the file by pressing Ctrl+O, then exit with Ctrl+X.

Compile and Run

Now, compile the Java source code into bytecode:

javac Hello.java

If compilation succeeds without errors, a Hello.class file appears in the current directory. Run the compiled program with the following command:

java Hello

Expected output:

Hello from LinuxCapable!

If you see this output, your OpenJDK 8 installation is working correctly.

If you have multiple Java versions installed and compilation fails with version errors, ensure OpenJDK 8 is set as the active version using sudo update-alternatives --config java and sudo update-alternatives --config javac.

Remove OpenJDK 8

If you no longer need OpenJDK 8, remove it along with any automatically installed dependencies:

sudo apt remove openjdk-8-jdk openjdk-8-jre
sudo apt autoremove

The first command removes the main OpenJDK 8 packages, while apt autoremove cleans up any orphaned dependencies that were installed alongside Java but are no longer needed.

After removal, verify that Java is no longer available or has switched to another installed version:

java -version

If no other Java versions are installed, this command returns an error confirming removal. If another version remains, the output shows which Java is now active.

Conclusion

You now have OpenJDK 8 installed on Ubuntu and configured as your Java runtime. With the update-alternatives system, you can easily switch between multiple Java versions as needed for different projects. For Java development workflows, consider installing Apache Maven as a build tool, or explore OpenJDK 11 or OpenJDK 17 if your projects can migrate to a more recent LTS release.

Leave a Comment