Installing Java on Ubuntu is a fundamental step for developers and users who need to run Java-based applications or develop their own. Ubuntu, a popular Linux distribution, offers several ways to get Java up and running, ranging from the default OpenJDK packages to the official Oracle JDK. This guide will walk you through the most common and recommended methods, ensuring you can choose the version that best suits your needs. We will cover installation via the Ubuntu repositories using apt, as well as the manual installation of Oracle JDK.
Installing OpenJDK via Ubuntu Repositories
OpenJDK (Open Java Development Kit) is a free and open-source implementation of the Java Platform, Standard Edition. It’s the default Java Development Kit on most Linux distributions, including Ubuntu. Installing it via apt is the simplest and most common method, as it leverages Ubuntu’s package management system for easy installation and updates.

Understanding Java Versions
Before you begin, it’s helpful to understand the different versions of Java you might encounter. As of this writing, the most relevant versions for most users are Java 8 (LTS), Java 11 (LTS), Java 17 (LTS), and more recent releases like Java 21. Long-Term Support (LTS) versions are recommended for production environments due to their extended support periods.
- Java SE 8 (LTS): Still widely used, especially for legacy applications.
- Java SE 11 (LTS): A significant release with many improvements and a foundational LTS version.
- Java SE 17 (LTS): The latest LTS version, offering enhanced performance, security, and new features.
- Java SE 21 (LTS): The newest LTS release, bringing the latest advancements.
The Ubuntu repositories typically offer recent LTS versions and sometimes the latest non-LTS releases.
Installing the Default JDK (OpenJDK)
Ubuntu usually defaults to installing a recent version of OpenJDK when you request a Java Development Kit. To install the default version available in your Ubuntu repositories, open your terminal and run the following commands:
Updating Package Lists
First, ensure your package list is up-to-date. This is crucial to fetch the latest available package information.
sudo apt update
Installing the Default JDK
Once your package list is updated, you can install the default Java Development Kit. This command will install the default-jdk package, which typically points to the latest stable version of OpenJDK available in your Ubuntu version’s repositories.
sudo apt install default-jdk
This command will download and install the OpenJDK JRE (Java Runtime Environment) along with the development tools (compiler, debugger, etc.).
Installing a Specific OpenJDK Version
If you need a specific version of OpenJDK, such as Java 11 or Java 17, you can install it directly by specifying the package name.
Installing OpenJDK 11
To install OpenJDK 11, use the following command. Note that the package name might vary slightly depending on your Ubuntu version. Common names include openjdk-11-jdk.
sudo apt install openjdk-11-jdk
Installing OpenJDK 17
For OpenJDK 17, the command is:
sudo apt install openjdk-17-jdk
You can search for available OpenJDK packages using apt search openjdk-.
Verifying the Installation
After installation, you can verify that Java has been installed correctly and check its version.
java -version
This command should output information about the installed Java version. If you have multiple Java versions installed, this will show the currently active one.
Managing Multiple Java Versions
It’s common to have multiple Java versions installed on a system, especially if you work on projects that require different Java environments. Ubuntu provides a tool called update-alternatives to manage these versions and switch between them easily.
Listing Available Java Versions
The update-alternatives system manages symbolic links that determine which version of a command (like java, javac, etc.) is executed. You can see the currently configured Java alternatives with:
sudo update-alternatives --config java
This command will list all installed Java executables and prompt you to choose which one should be the default. You will see output similar to this:
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 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:
Simply enter the number corresponding to the Java version you wish to set as default and press Enter.
Setting the Default Java Version
To set a specific Java version as the default, you can use the --config command as shown above, or directly with:
sudo update-alternatives --set java /path/to/your/java/executable

Replace /path/to/your/java/executable with the actual path to the java executable of the version you want to set as default. You can find these paths in the output of sudo update-alternatives --config java.
Similarly, you can manage the javac (Java compiler) and other Java-related commands:
sudo update-alternatives --config javac
Installing Oracle JDK
While OpenJDK is excellent and widely used, some commercial applications or specific development needs might require the official Oracle JDK. Installing Oracle JDK typically involves downloading the binaries from Oracle’s website and then manually configuring the system.
Note: Oracle JDK requires accepting their license agreement. Always check the licensing terms before installation.
Downloading Oracle JDK
- Visit the Oracle Java Downloads Page: Go to the official Oracle Java SE Downloads website (e.g.,
oracle.com/java/technologies/downloads/). - Select the Version: Choose the desired Java SE version (e.g., JDK 17, JDK 21).
- Choose the Operating System: Select “Linux” as the operating system.
- Download the Archive: Download the
.tar.gzarchive for the x64 architecture.
Installing Oracle JDK Manually
Once you have downloaded the Oracle JDK .tar.gz file, follow these steps:
Extracting the Archive
Create a directory to store your Java installations. A common location is /usr/lib/jvm/.
sudo mkdir -p /usr/lib/jvm
Move the downloaded .tar.gz file to this directory and extract it:
sudo mv ~/Downloads/jdk-XX_linux-x64_bin.tar.gz /usr/lib/jvm/
cd /usr/lib/jvm/
sudo tar -xvzf jdk-XX_linux-x64_bin.tar.gz
Replace jdk-XX_linux-x64_bin.tar.gz with the actual name of the downloaded file. This will create a directory named after the JDK version (e.g., jdk-17.0.8).
Configuring with update-alternatives
Now, you need to register this manually installed Oracle JDK with the update-alternatives system.
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-XX/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk-XX/bin/javac" 1
Replace /usr/lib/jvm/jdk-XX/ with the actual path to your extracted Oracle JDK directory. The 1 at the end is a priority value; you can assign higher priorities to versions you prefer.
After running these commands, you can use sudo update-alternatives --config java and sudo update-alternatives --config javac to select the Oracle JDK as your default.
Setting JAVA_HOME Environment Variable
Many Java applications and build tools rely on the JAVA_HOME environment variable to locate the Java installation directory. It’s good practice to set this, especially when using manual installations.
Edit your shell’s profile file. For most users, this is ~/.bashrc or ~/.profile.
nano ~/.bashrc
Add the following lines at the end of the file, replacing the path with your Oracle JDK installation path:
export JAVA_HOME="/usr/lib/jvm/jdk-XX"
export PATH=$JAVA_HOME/bin:$PATH
Save and close the file (Ctrl+X, Y, Enter). Then, apply the changes to your current terminal session:
source ~/.bashrc
You can verify if JAVA_HOME is set correctly:
echo $JAVA_HOME
Troubleshooting Common Issues
While installing Java on Ubuntu is generally straightforward, you might encounter a few common issues.
“command not found” After Installation
If you run java -version and get a “command not found” error after installing via apt, it usually means the PATH environment variable hasn’t been updated for your current session.
- Solution: Log out and log back in, or run
source ~/.bashrc(if using Bash) in your terminal. If you installed manually and usedupdate-alternatives, ensure the symbolic links are correctly set and the directory is in yourPATH.
Incorrect Java Version Being Used
If java -version shows an older version than expected, it means another Java installation is taking precedence.
- Solution: Use
sudo update-alternatives --config javato explicitly select the desired version. Ensure that your~/.bashrcor~/.profilefile correctly setsJAVA_HOMEand adds the desired Javabindirectory to thePATH.

Issues with Specific Applications
Some applications might have specific requirements regarding the Java version or distribution.
- Solution: Carefully read the application’s documentation. If an application strictly requires Oracle JDK, ensure you have installed it correctly and that the
JAVA_HOMEenvironment variable is pointing to it. If it’s a matter of Java version compatibility (e.g., needing Java 8 for an older app), ensure that version is installed and set as default.
By following these steps, you should be able to successfully install and manage Java on your Ubuntu system, whether you opt for the convenient OpenJDK packages or the official Oracle JDK. This flexibility ensures you have the right Java environment for all your development and application needs.
