How To Install Java On Ubuntu: A Complete Step-By-Step Guide

Why Java Installation on Ubuntu Can Be Confusing

You’ve just set up your new Ubuntu server or desktop, ready to build that application, run a Minecraft server, or deploy a Spring Boot service. You type java -version, only to be met with “Command ‘java’ not found.” It’s a common first hurdle for developers and system administrators working in the Linux environment.

Unlike Windows or macOS, Ubuntu doesn’t come with Java pre-installed. Furthermore, the open-source nature of Linux means you have multiple versions and distributions of Java to choose from, each serving different needs. Should you install the official Oracle JDK, the open-source OpenJDK, or a specific version like Java 11 or Java 17? The official repositories, third-party PPAs, and manual tarball installations add to the initial confusion.

This guide cuts through that noise. We’ll walk you through the most reliable methods to get a fully functional Java Runtime Environment (JRE) or Java Development Kit (JDK) running on your Ubuntu system, whether you’re on the latest LTS release or an older version. By the end, you’ll not only have Java installed but also understand how to manage multiple versions side-by-side.

Understanding Your Java Options on Ubuntu

Before running any installation commands, it’s crucial to know what you’re installing. The two primary flavors you’ll encounter are OpenJDK and Oracle JDK. For the vast majority of users, OpenJDK is the recommended choice. It’s the official open-source implementation of the Java Platform, Standard Edition, and is fully compatible, secure, and freely available. Oracle JDK is Oracle’s commercial build, which historically had minor differences but now is very similar under a different license.

You also need to decide between the JRE and the JDK. The Java Runtime Environment (JRE) is what you need if you only plan to run Java applications. It includes the Java Virtual Machine (JVM), core libraries, and other components to execute compiled Java code. The Java Development Kit (JDK) includes the JRE plus all the tools required to develop Java software, such as the compiler (javac), debugger, and documentation tools. If you’re doing any development, install the JDK.

Finally, version matters. Long-Term Support (LTS) versions like Java 8, 11, 17, and 21 are typically preferred for production stability, while newer feature releases (like Java 22) offer the latest language improvements. Your project requirements will dictate the version.

Method 1: Installing OpenJDK from Ubuntu’s Official Repository

This is the simplest and most straightforward method, perfect for getting started quickly. Ubuntu’s APT package manager maintains curated packages for various OpenJDK versions. The packages are well-integrated, receive security updates through the standard system update process, and require no extra configuration.

First, update your local package index to ensure you get information on the latest available versions. Open a terminal and run:

sudo apt update

Now, you can search for available OpenJDK packages. A useful command is:

apt search openjdk | grep jdk

This will list packages like openjdk-11-jdk, openjdk-17-jdk, and openjdk-21-jdk. To install a specific version, for example, OpenJDK 17 JDK, use the following command:

sudo apt install openjdk-17-jdk

The installation process will download the package and all its dependencies. It will also set up the necessary alternatives on your system, which we’ll discuss later. To install just the runtime environment (JRE) for version 17, you would install openjdk-17-jre instead.

Once the installation completes, verify it by checking the Java version:

java -version

You should see output detailing the OpenJDK version, runtime environment, and HotSpot VM. Also, verify the compiler if you installed the JDK:

javac -version

This method’s main advantage is simplicity and maintenance. The downside is that the repositories might not always have the very latest minor or patch release immediately available.

Setting the JAVA_HOME Environment Variable

Many Java applications and build tools like Maven, Gradle, and Tomcat rely on the JAVA_HOME environment variable to locate the Java installation. After installing via APT, you need to set this manually.

First, find the installation path. A reliable way is to use the update-alternatives command:

sudo update-alternatives –config java

This command will list all installed Java versions and show the path of the currently selected one. The path will look something like /usr/lib/jvm/java-17-openjdk-amd64/bin/java. The JAVA_HOME is this path without the /bin/java suffix.

You can set JAVA_HOME for your current session in the terminal:

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64

To make this permanent for your user, add the export line to your shell profile file. For Bash, which is Ubuntu’s default, edit ~/.bashrc:

nano ~/.bashrc

Add the export line at the end of the file, save, and exit. Then, reload the profile:

source ~/.bashrc

Confirm the variable is set:

how to install java on ubuntu

echo $JAVA_HOME

This should now correctly point to your Java installation directory.

Method 2: Installing Specific Versions via a PPA

What if you need a version not available in Ubuntu’s main repository, or you want faster access to new updates? This is where Personal Package Archives (PPAs) come in. The most trusted PPA for Java on Ubuntu is maintained by the “Linux Uprising” team, which provides packages for recent OpenJDK versions.

Using a third-party repository always carries a small trust consideration, so ensure you’re comfortable with the source. The installation process involves adding the repository, updating your package list, and then installing as usual.

First, install the software-properties-common package if you don’t have it, which provides the add-apt-repository command:

sudo apt install software-properties-common -y

Next, import the GPG key for the PPA and add the repository. For example, to add the PPA for OpenJDK 21:

sudo add-apt-repository ppa:linuxuprising/java -y

Now, update your package list again to pull in information from the new repository:

sudo apt update

You can now install the JDK package. The package names often follow a pattern like openjdk-21-jdk. Confirm the exact name with apt search openjdk. Then install it:

sudo apt install openjdk-21-jdk

Verify the installation with java -version. The PPA method gives you more version flexibility while still using the convenient APT package management system for updates.

Method 3: Manual Installation from a Tarball

For maximum control—such as installing a specific build from Oracle or Adoptium, or needing a version not packaged anywhere—the manual tarball method is the way to go. You download a compressed archive (.tar.gz), extract it to a directory of your choice, and configure your environment manually.

This method is more involved but is often used in containerized environments like Docker, where you want a minimal, reproducible setup without extra package manager layers.

First, navigate to a directory where you download files, like ~/Downloads:

cd ~/Downloads

Visit the official OpenJDK website or a distribution site like Adoptium (adoptium.net) to get the download link for the Linux x64 tarball of your desired version. Use wget or curl to download it. For example, to download OpenJDK 17 from Adoptium:

wget https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10+7/OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz

Create a directory to host your Java installations. A common convention is /usr/lib/jvm:

sudo mkdir -p /usr/lib/jvm

Extract the downloaded tarball into this directory:

sudo tar -xzf OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz -C /usr/lib/jvm

This will create a folder like /usr/lib/jvm/jdk-17.0.10+7. Now, you must configure your system to use this Java installation. You can manually set JAVA_HOME by adding the export line to your ~/.bashrc, pointing to the extracted directory:

export JAVA_HOME=/usr/lib/jvm/jdk-17.0.10+7

export PATH=$JAVA_HOME/bin:$PATH

Notice we also prepend the bin directory to the PATH. This ensures that when you type java or javac, the system finds the executables from your manual installation. Reload your profile with source ~/.bashrc and verify.

The manual method gives you a clean, isolated installation but requires you to manage updates and security patches yourself by repeating the download and extraction process.

Managing Multiple Java Versions

It’s common for developers to work on projects that require different Java versions. Ubuntu provides a powerful tool called update-alternatives to manage this seamlessly. It creates symbolic links for common commands like java, javac, and javadoc, allowing you to switch the system-wide default version with a single command.

how to install java on ubuntu

If you installed multiple versions via APT, they are usually registered with update-alternatives automatically. For manual installations, you need to register them yourself. To register a manually installed Java version, use commands like:

sudo update-alternatives –install /usr/bin/java java /usr/lib/jvm/jdk-17.0.10+7/bin/java 1

sudo update-alternatives –install /usr/bin/javac javac /usr/lib/jvm/jdk-17.0.10+7/bin/javac 1

The number at the end (1) is a priority; higher numbers give higher priority.

To see all registered Java versions and interactively select the default, run:

sudo update-alternatives –config java

You’ll see a numbered list. Enter the selection number for the version you want as the default. Do the same for javac if needed. This switch is system-wide and affects all users and terminal sessions.

Troubleshooting Common Installation Issues

Even with a clear guide, you might hit a snag. Let’s address some frequent problems.

If java -version still shows “command not found” after installation, the most likely cause is that the Java binaries are not in your PATH. For APT installations, try logging out and back in, or open a new terminal window. For manual installations, double-check that you correctly added the bin directory to your PATH in .bashrc and sourced the file.

If you get a “Permission denied” error when running java, check the execute permissions on the binary file. They should be set correctly, but you can verify with ls -l /path/to/java. In extremely rare cases with manual tarballs, you might need to run sudo chmod +x on the bin/ directory files.

Version mismatch is another common headache. You think you installed Java 17, but java -version reports Java 11. This means an older version is still set as the default in update-alternatives. Use the sudo update-alternatives –config java command to switch to the newer version.

For applications that complain about JAVA_HOME not being set, ensure the variable is correctly defined and exported. Remember that setting it in ~/.bashrc only affects interactive shell sessions for that user. System services started at boot may require the variable to be set in a global file like /etc/environment. To set it globally, you can add the line JAVA_HOME=”/usr/lib/jvm/java-17-openjdk-amd64″ (without the “export” keyword) to /etc/environment and reboot.

Verifying Your Setup with a Simple Program

The ultimate test is to compile and run a Java program. Create a simple file to verify everything works end-to-end.

Create a new file called HelloWorld.java:

nano HelloWorld.java

Add the following classic code:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println(“Java is successfully installed on Ubuntu!”);
    }
}

Save and exit. Now, compile it using javac:

javac HelloWorld.java

If successful, this will create a HelloWorld.class file. Now, run it:

java HelloWorld

You should see the triumphant message: “Java is successfully installed on Ubuntu!” If you do, congratulations—your Java development environment is fully operational.

Choosing the Right Method for Your Needs

With multiple installation paths available, which one should you choose? Your specific use case dictates the best approach.

For most desktop users and developers starting a new project, the official APT repository method (Method 1) is ideal. It’s secure, stable, and easy to maintain. If your project requires a very recent version not yet in the main repos, the trusted PPA method (Method 2) offers a good balance of convenience and recency.

For system administrators setting up production servers where consistency across different Linux distributions is key, or for building Docker images, the manual tarball installation (Method 3) provides the most control and reproducibility. You can script the exact version and configuration.

Remember that regardless of the method, the core principles remain: verify the installation, set JAVA_HOME, and manage versions with update-alternatives if needed. Keeping Java updated is also critical for security. For APT installations, regular system updates (sudo apt update && sudo apt upgrade) will handle this. For manual installations, you must establish your own update cycle.

You now have the knowledge to install Java on Ubuntu with confidence. The initial “command not found” error is a thing of the past. Your system is ready to run everything from enterprise microservices to simple utility scripts, unlocking the full potential of the Java ecosystem on one of the world’s most popular development platforms.

Leave a Comment

close