How to Install Java on Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04

Java is one of the most popular programming languages and the JVM (Java’s virtual machine) is the run-time environment to run Java applications. These two platforms are required for many popular software that includes Tomcat, Jetty, Cassandra, Glassfish, and Jenkins.

This tutorial will guide you through the installation of Java on Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04, covering both the installation from the Ubuntu repository and the official Oracle package.

Installing Java from the Ubuntu Repository

To begin, search for all available Java versions in the Ubuntu package repository.

apt search openjdk | grep -E 'openjdk-.*-jdk/'
Search Java Versions in Ubuntu
Search Java Versions in Ubuntu

Next, choose the Java version that suits your needs. Here we are installing openjdk-21-jdk (Java Development Kit), which includes the compiler, libraries, and tools needed for developing Java applications.

sudo apt install openjdk-21-jdk
Install Java in Ubuntu
Install Java in Ubuntu

Verify that Java has been installed correctly by checking the version of both the Java runtime and the compiler.

java --version
javac --version
Check Java in Ubuntu
Check Java in Ubuntu

If the installation was successful, you should see the version of Java you installed displayed in the terminal.

Installing Java from the Official Oracle Package

Download the Oracle Java package from the official Oracle website or use the following wget command to download directly in the terminal.

wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.deb

Once downloaded, use the following dpkg command to install the Oracle Java installer file that includes the necessary binaries and files to run the Java Development Kit on your system.

sudo dpkg -i jdk-22_linux-x64_bin.deb

Verify that Java has been installed correctly by checking the version of both the Java runtime and the compiler.

java --version
javac --version
Verify Java in Ubuntu
Verify Java in Ubuntu

If the installation was successful, you should see the version of Java you installed displayed in the terminal.

Managing Multiple Java Versions in Ubuntu

Having multiple versions of Java installed allows you to test and develop applications using different versions of the Java platform.

You can install multiple versions of Java from the Ubuntu repository and switch between different Java versions using the update-alternatives command as shown.

List all available Java versions installed on your system.

update-alternatives --list java

To switch between different Java versions, use the following command.

sudo update-alternatives --config java
Multiple Java Versions in Ubuntu
Multiple Java Versions in Ubuntu
Conclusion

Installing Java on Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04 is a straightforward process that can be accomplished through various methods. This tutorial has covered both the installation from the Ubuntu repository and the official Oracle package.

Additionally, it has discussed how to switch between different Java versions. By following these steps, you can successfully install and manage Java on your Ubuntu system.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

1 Comment

Leave a Reply
  1. Hello,

    I am having issue. After adding the line JAVA_Home.

    $ source /etc/environment
    

    bash: /etc/environment: line 2: unexpected EOF while looking for matching `”‘

    bash: /etc/environment: line 3: syntax error: unexpected end of file

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.