How to Install Java on Ubuntu: A Comprehensive Guide

How to Install Java on Ubuntu: A Comprehensive Guide

Java is a versatile and widely used programming language and software platform. It’s essential for running many applications, from desktop programs to web servers. If you’re using Ubuntu, this guide will walk you through the process of installing Java, covering different Java Development Kit (JDK) versions and methods to ensure a smooth setup.

## Prerequisites

Before you begin, ensure you have:

* An Ubuntu system (desktop or server).
* A user account with `sudo` privileges.
* A stable internet connection.

## Checking if Java is Already Installed

First, let’s check if Java is already installed on your system. Open a terminal and run the following command:

bash
java -version

If Java is installed, you’ll see version information. If not, you’ll receive a message indicating that Java is not found. If you have an older version, it’s still a good idea to follow these instructions to install the latest version.

## Installing Java using APT (OpenJDK)

OpenJDK is an open-source implementation of the Java Platform, Standard Edition (Java SE). It’s the default Java development and runtime environment on Ubuntu, and it’s easily installable using the `apt` package manager.

### Step 1: Update the Package Index

Before installing any new software, update your system’s package index:

bash
sudo apt update

This command retrieves the latest package information from the configured repositories.

### Step 2: Install the Default JRE/JDK or a Specific Version

#### Installing the Default Java Runtime Environment (JRE)

The JRE allows you to run Java applications. To install the default JRE, use the following command:

bash
sudo apt install default-jre

#### Installing the Default Java Development Kit (JDK)

The JDK is required if you want to develop Java applications. It includes the JRE and tools like the Java compiler (`javac`). To install the default JDK, use this command:

bash
sudo apt install default-jdk

#### Installing a Specific Version of OpenJDK (e.g., Java 11)

If you need a specific Java version, you can install it directly. For example, to install OpenJDK 11:

bash
sudo apt install openjdk-11-jdk

Similarly, to install OpenJDK 17:

bash
sudo apt install openjdk-17-jdk

And to install OpenJDK 8 (although generally older versions are discouraged due to security concerns unless required by a specific application):

bash
sudo apt install openjdk-8-jdk

Replace `11`, `17`, or `8` with the desired Java version. Newer versions like OpenJDK 21 are also typically available.

### Step 3: Verify the Installation

After installation, verify that Java is correctly installed by checking the version:

bash
java -version
javac -version

The first command checks the JRE version, and the second checks the JDK version (if you installed the JDK). The output should display the installed Java version.

## Installing Java using SDKMAN!

SDKMAN! (Software Development Kit Manager) is a tool for managing multiple versions of software development kits, including Java. It simplifies installing, switching between, and managing different JDKs. This is beneficial for developers working on different projects that require distinct Java versions.

### Step 1: Install SDKMAN!

Before installing Java with SDKMAN!, you need to install SDKMAN! itself. Open your terminal and run the following commands:

bash
curl -s “https://get.sdkman.io” | bash

This command downloads and executes the SDKMAN! installation script. Follow the on-screen instructions. It usually involves opening a new terminal or sourcing the SDKMAN! configuration script:

bash
source “$HOME/.sdkman/bin/sdkman-init.sh”

Alternatively, you can simply close and reopen your terminal.

### Step 2: List Available Java Versions

To see the available Java versions that SDKMAN! can install, use the following command:

bash
sdk list java

This will display a list of available JDK distributions, including those from Oracle, OpenJDK, GraalVM, and others, along with their identifiers and versions.

### Step 3: Install a Java Version using SDKMAN!

Choose a Java version from the list and install it using the `sdk install` command. For example, to install version `21.0.2.r17-grl` of the GraalVM JDK, use:

bash
sdk install java 21.0.2.r17-grl

Replace `21.0.2.r17-grl` with the identifier of the Java version you want to install. SDKMAN! will download and install the specified JDK.

### Step 4: Set a Default Java Version (Optional)

If you have multiple Java versions installed, you can set a default version to be used when you open a new terminal. To set a default version, use the `sdk default` command:

bash
sdk default java 21.0.2.r17-grl

Replace `21.0.2.r17-grl` with the identifier of the Java version you want to set as the default.

### Step 5: Verify the Installation

Verify that Java is correctly installed and that the correct version is being used:

bash
java -version
javac -version

The output should display the version of Java that you installed using SDKMAN!.

## Installing Oracle Java (Deprecated – Use OpenJDK Instead)

While historically Oracle Java was a common choice, it’s now generally recommended to use OpenJDK due to licensing changes and ease of installation. However, if you specifically require Oracle Java for compatibility reasons, the following steps provide guidance. Note that these steps may become outdated as Oracle’s distribution methods evolve.

**Important:** Installing Oracle Java can be more complex and may involve manually downloading the JDK from Oracle’s website and configuring it. Oracle’s licensing requires you to have an Oracle account and accept their license agreement before downloading.

### Step 1: Download the Oracle JDK

1. Visit the Oracle Java download page: [https://www.oracle.com/java/technologies/javase-downloads.html](https://www.oracle.com/java/technologies/javase-downloads.html)
2. Choose the appropriate JDK version for your system (e.g., Linux x64). You will likely need to create an Oracle account and accept the license agreement.
3. Download the `.tar.gz` archive.

### Step 2: Extract the Archive

Extract the downloaded archive to a suitable directory, such as `/opt/java`:

bash
sudo mkdir -p /opt/java
sudo tar -xzf jdk-*.tar.gz -C /opt/java

Replace `jdk-*.tar.gz` with the actual name of the downloaded file.

### Step 3: Configure Environment Variables

To make Java accessible system-wide, you need to set the `JAVA_HOME` environment variable and add the Java binary directory to your `PATH`. Create a new file in `/etc/profile.d/` to set these variables:

bash
sudo nano /etc/profile.d/java.sh

Add the following lines to the file, replacing `/opt/java/jdk-*` with the actual path to your extracted JDK directory:

bash
export JAVA_HOME=/opt/java/jdk-21.0.2
export PATH=$PATH:$JAVA_HOME/bin

Make the script executable:

bash
sudo chmod +x /etc/profile.d/java.sh

Load the environment variables:

bash
source /etc/profile.d/java.sh

### Step 4: Update Alternatives (Optional but Recommended)

If you have multiple Java versions installed, use the `update-alternatives` command to manage which version is used by default. This is important for preventing conflicts.

bash
sudo update-alternatives –install “/usr/bin/java” “java” “$JAVA_HOME/bin/java” 1
sudo update-alternatives –install “/usr/bin/javac” “javac” “$JAVA_HOME/bin/javac” 1
sudo update-alternatives –install “/usr/bin/javadoc” “javadoc” “$JAVA_HOME/bin/javadoc” 1

After running these commands, you can choose the default Java version using:

bash
sudo update-alternatives –config java
sudo update-alternatives –config javac
sudo update-alternatives –config javadoc

### Step 5: Verify the Installation

Verify that Java is correctly installed:

bash
java -version
javac -version

The output should display the version of Oracle Java that you installed.

## Setting the JAVA_HOME Environment Variable

The `JAVA_HOME` environment variable is crucial for many Java-based applications and build tools (like Maven, Gradle, and IntelliJ IDEA) to locate the Java installation directory. If you followed the OpenJDK installation, the variable might already be set by the system. If you installed Oracle Java, you’ve already configured it in `/etc/profile.d/java.sh`. To verify or set `JAVA_HOME`, follow these steps:

### Step 1: Check if JAVA_HOME is Already Set

bash
echo $JAVA_HOME

If this command returns a path to your Java installation directory, `JAVA_HOME` is already set. If it returns nothing, proceed to the next step.

### Step 2: Set JAVA_HOME (if not already set)

There are several ways to set `JAVA_HOME`:

* **System-wide (for all users):** As demonstrated earlier, create or modify `/etc/profile.d/java.sh` with the `export JAVA_HOME` line.
* **User-specific (for the current user only):** Add the `export JAVA_HOME` line to your `~/.bashrc` or `~/.zshrc` file (depending on your shell).

For example, to set `JAVA_HOME` for your user account, open your `~/.bashrc` file:

bash
nano ~/.bashrc

Add the following line at the end of the file, replacing `/path/to/your/java` with the actual path to your Java installation directory (e.g., `/usr/lib/jvm/java-11-openjdk-amd64` for OpenJDK or `/opt/java/jdk-21.0.2` for Oracle Java):

bash
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Save the file and apply the changes by sourcing it:

bash
source ~/.bashrc

### Step 3: Verify JAVA_HOME

Verify that `JAVA_HOME` is correctly set by running:

bash
echo $JAVA_HOME

The output should now display the path to your Java installation directory.

## Switching Between Java Versions

If you have multiple Java versions installed (e.g., using SDKMAN! or by manually installing different JDKs), you may need to switch between them for different projects. Here’s how to switch between Java versions using `update-alternatives` (for OpenJDK installed via `apt`) and SDKMAN!.

### Switching with `update-alternatives`

If you installed Java using `apt`, you can use the `update-alternatives` command to manage the default Java version. First, check the available Java alternatives:

bash
sudo update-alternatives –config java
sudo update-alternatives –config javac
sudo update-alternatives –config javadoc

These commands will display a list of installed Java versions. Select the number corresponding to the Java version you want to use as the default. `update-alternatives` will then update the system’s symbolic links to point to the selected version.

### Switching with SDKMAN!

If you installed Java using SDKMAN!, switching between versions is straightforward:

bash
sdk use java

Replace `` with the identifier of the Java version you want to use. For example:

bash
sdk use java 21.0.2.r17-grl

This command sets the specified Java version as the current version for your terminal session. To make it the default version for all future sessions, use the `sdk default` command as described earlier.

## Troubleshooting Common Issues

* **”java command not found”**: This usually means that the Java binary directory is not in your `PATH`. Double-check that you’ve correctly set the `JAVA_HOME` environment variable and added `$JAVA_HOME/bin` to your `PATH`.
* **Incorrect Java version**: Verify that the correct Java version is being used by checking the output of `java -version`. If it’s incorrect, double-check your `JAVA_HOME` setting and ensure that you’ve selected the correct version using `update-alternatives` or SDKMAN!.
* **Permission issues**: If you encounter permission errors during installation or configuration, make sure you’re using `sudo` when required and that the Java installation directory has appropriate permissions.
* **Package installation errors**: If you encounter errors while installing packages using `apt`, make sure your package index is up-to-date (`sudo apt update`) and that you don’t have any broken dependencies (`sudo apt –fix-broken install`).

## Uninstalling Java

If you need to uninstall Java, follow these steps:

### Uninstalling OpenJDK (installed via `apt`):

To remove the default JRE:

bash
sudo apt remove default-jre

To remove the default JDK:

bash
sudo apt remove default-jdk

To remove a specific version of OpenJDK (e.g., OpenJDK 11):

bash
sudo apt remove openjdk-11-jdk

To completely remove the packages and their configuration files:

bash
sudo apt purge default-jre
sudo apt purge default-jdk
sudo apt purge openjdk-11-jdk

Additionally, you can remove unused dependencies:

bash
sudo apt autoremove

### Uninstalling Oracle Java (manually installed):

1. Remove the Java installation directory (e.g., `/opt/java/jdk-*`):

bash
sudo rm -rf /opt/java/jdk-*

2. Remove the `java.sh` script from `/etc/profile.d/`:

bash
sudo rm /etc/profile.d/java.sh

3. Remove the entries from `update-alternatives`:

bash
sudo update-alternatives –remove “java” “/opt/java/jdk-*/bin/java”
sudo update-alternatives –remove “javac” “/opt/java/jdk-*/bin/javac”
sudo update-alternatives –remove “javadoc” “/opt/java/jdk-*/bin/javadoc”

Replace `/opt/java/jdk-*` with the actual path to your JDK directory.

### Uninstalling Java installed by SDKMAN!

To uninstall a Java version installed by SDKMAN! use the command:

bash
sdk uninstall java

Replace `` with the id of the sdk you want to uninstall. For example, if you wanted to uninstall GraalVM java with the identifier `21.0.2.r17-grl` you would execute:

bash
sdk uninstall java 21.0.2.r17-grl

## Conclusion

This guide has covered several methods for installing Java on Ubuntu, including using `apt` to install OpenJDK, SDKMAN! to manage multiple JDK versions, and manually installing Oracle Java (although OpenJDK is the recommended approach). Choose the method that best suits your needs and follow the steps carefully. Remember to verify your installation and configure environment variables correctly. With Java properly installed, you’ll be ready to run Java applications and develop your own Java projects on your Ubuntu system.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments