Step by Step Guide to Install Java on Ubuntu Linux

Java is one of the most popular programming platforms used by developers around the world. Whether you want to run Java applications or develop them yourself, having Java installed on your  Ubuntu system is essential.

In this beginner-friendly guide, I’ll walk you through the steps to install different versions of Java on your Ubuntu 22.04 LTS system. We will cover installing both the Java Runtime Environment (JRE) to run Java programs and the full Java Development Kit (JDK) to compile Java code.

Prerequisites

Before installing Java, make sure your Ubuntu system is fully updated by running:

sudo apt update && sudo apt upgrade

This will fetch the latest updates from the Ubuntu repositories and prep your system.

A screenshot of a terminal window displaying the output of package management commands in Ubuntu. The commands 'sudo apt update' and 'sudo apt upgrade' have been executed, showing package updates and messages regarding repository management and available upgrades.

OpenJDK vs Oracle JDK Key Differences

When installing Java on Ubuntu, you have the option of using the free and open-source OpenJDK or the official Oracle Java Development Kit. Here are some key differences:

Licensing:

  • OpenJDK is licensed under GPL and can be freely used in commercial applications
  • Oracle JDK is proprietary and requires a commercial license for business use

Performance:

  • OpenJDK and Oracle JDK generally have similar runtime performance
  • Some benchmarks show Oracle JDK slightly faster in certain workloads

Features:

  • OpenJDK provides all core Java features and keeps up with Oracle’s releases
  • Some advanced management and monitoring tools only available with Oracle JDK

Support:

  • OpenJDK is community-supported with Ubuntu forums/chat available
  • Oracle provides official support, updates and patches for Oracle JDK

So in summary, OpenJDK works great for most developer needs and Ubuntu systems. But certain commercial software products may mandate using Oracle Java. For proprietary applications, the paid Oracle JDK support may also be preferred.

See also  Biden’s Executive Order on AI: What Businesses Need to Know

Hope this gives you the context to decide which Java distribution better meets your needs!

Install Default Java OpenJDK JRE/JDK

The easiest way to install Java is via Ubuntu’s default repositories which contain OpenJDK – the open-source version of Java.

To install OpenJDK 11, run:

sudo apt install default-jre default-jdk
A screenshot of a terminal window on Ubuntu with the output of an 'apt install' command that has been run to install the default Java Development Kit (JDK) and related packages. The text on the screen includes a prompt asking for confirmation to continue with the installation, indicating the amount of disk space that will be used.

This will install both the JRE and JDK. To confirm, check their versions:

java -version
javac -version
A screenshot of a terminal window on Ubuntu showing the process of updating Java alternatives after installing Java packages. The screen displays a list of update-alternatives commands setting the newly installed Java version as default. At the bottom, the output of the 'java -version' command is visible, confirming the installed version of OpenJDK.

Install Oracle Java JDK

If you require the official Oracle Java JDK, you’ll need to manually install it.

First, add Oracle’s PPA to apt:

sudo add-apt-repository ppa:linuxuprising/java

Then update apt and install the latest Java JDK 11:

sudo apt update 
sudo apt install oracle-java11-installer

Once done, you can check the installed version:

java -version

And to set it as the default Java:

sudo update-alternatives --config java

Install Specific Java JDK Version

If you need an older Java version like Java 8 or 17, run:

sudo apt install oracle-java8-installer
sudo apt install oracle-java17-installer

Again check the version with java -version and update alternatives if needed.

Set JAVA_HOME Environment Variable

No matter whetever the Java version you install. You may need to set the env variable. Many Java apps and servers require the JAVA_HOME env variable set to function properly.

To set this permanently on Ubuntu, edit /etc/environment and add:

nano /etc/environment

OpenJDK

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Oracle JDK

JAVA_HOME="/usr/lib/jvm/java-11-oracle"
A screenshot of the GNU nano 6.2 text editor open in a terminal window on Ubuntu, editing the file /etc/environment. The file contains environment variable settings, including the PATH and JAVA_HOME variables, which are configured to include the paths for user binaries and the Java 11 OpenJDK installation directory, respectively.

 

Just remember to replace the path with where your preferred Java version is installed.

After saving the changes, run source /etc/environment to reload the file. Java should now be fully up and running!

A screenshot of a terminal window on Ubuntu, where the user has executed the 'source /etc/environment' command to apply changes made to the environment variables. The command has completed, and the prompt is awaiting new input.

Leave a Reply

Your email address will not be published. Required fields are marked *