Of course! This is a very common and essential task for any Java developer on Ubuntu. Here’s a comprehensive guide covering everything you need to know about installing and managing Java (JDK) on Ubuntu.
We'll cover three main methods, from the simplest and most recommended to the most manual.
Quick Answer: The Best Way (Recommended)
For most users, the best way is to use a Version Manager like SDKMAN!. It's the industry standard for managing multiple Java versions on Unix-like systems.
Install SDKMAN!
curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh"
(Open a new terminal or run source ~/.bashrc to complete the installation).
Install a JDK Let's install the latest LTS (Long-Term Support) version of OpenJDK, which is Java 21.
sdk install java 21.0.2-tem
0.2is the version number.-temstands for Temurin, which is a high-quality, open-source build of the OpenJDK.
Set it as Default
sdk default java 21.0.2-tem
Verify
java -version # Expected output: # openjdk version "21.0.2" 2025-01-16 # OpenJDK Runtime Environment Temurin-21.0.2+7 (build 21.0.2+7) # OpenJDK 64-Bit Server VM Temurin-21.0.2+7 (build 21.0.2+7, mixed mode, sharing) javac -version # Expected output: # javac 21.0.2
That's it! You are ready to go. To switch to a different Java version (e.g., Java 17 LTS), you would just run:
sdk install java 17.0.10-tem and sdk default java 17.0.10-tem.
Method 1: Using apt (The System Package Manager)
This is the traditional way to install software on Ubuntu. It's simple but less flexible. It's good if you only need one version of Java for system-wide use.
Step 1: Update Package Lists
Always start by updating your package repository's index.
sudo apt update
Step 2: Install the OpenJDK
You can install various versions. The most common are LTS (Long-Term Support) versions like 17 and 21.
To install OpenJDK 17 (Recommended for most projects as of 2025):
sudo apt install openjdk-17-jdk
To install OpenJDK 21 (The latest LTS):
sudo apt install openjdk-21-jdk
openjdk: The open-source implementation of Java.17or21: The major version number.jdk: The Java Development Kit, which includes the compiler (javac) and tools needed for development. Usejre(Java Runtime Environment) if you only need to run Java applications.
Step 3: Verify the Installation
Check the Java version to confirm it's installed correctly.
java -version
Step 4: Set the JAVA_HOME Environment Variable (Important!)
Many tools (like Maven, Gradle, Tomcat) require the JAVA_HOME environment variable to be set.
-
Find the installation path: The
update-alternativestool manages this. You can see the available Java installations with:sudo update-alternatives --config java
Note the path shown, for example,
/usr/lib/jvm/java-17-openjdk-amd64/bin/java. -
Find the JDK home directory: The JDK home is usually one level up from the
bindirectory. Based on the example above, it would be/usr/lib/jvm/java-17-openjdk-amd64. -
Set
JAVA_HOMEpermanently: Open your shell's profile file. For Bash (the default on Ubuntu), it's~/.bashrc.nano ~/.bashrc
Add the following line to the end of the file. Replace the path with your own.
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export PATH="$JAVA_HOME/bin:$PATH"
Save the file and exit (
Ctrl+X, thenY, thenEnterin nano). -
Apply the changes: Either open a new terminal or run the following command to load the changes into your current session:
source ~/.bashrc
-
Verify
JAVA_HOME:echo $JAVA_HOME # Should output: /usr/lib/jvm/java-17-openjdk-amd64
Method 2: Manual Installation (Advanced)
This method gives you full control but is more complex and requires manual management. Use this if you need a specific version not available in the repositories or if you want to install it in a custom location (e.g., /opt).
Step 1: Download the JDK
Go to the official Oracle Java SE Development Kit Downloads page or the Eclipse Temurin (Adoptium) downloads page. For this example, we'll use Temurin.
Download the Linux x64 archive file (.tar.gz).
# Example: Downloading Temurin JDK 21 wget -O openjdk-21_linux-x64_bin.tar.gz "https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.2%2B7/OpenJDK21U-jdk_x64_linux_hotspot_21.0.2_7.tar.gz"
Step 2: Extract the Archive
Create a directory for your installations (e.g., /opt/java) and extract the file there.
sudo mkdir -p /opt/java sudo tar -xvf openjdk-21_linux-x64_bin.tar.gz -C /opt/java/
This will create a directory like /opt/java/jdk-21.0.2+7.
Step 3: Set Environment Variables
This is the most critical step. You must set JAVA_HOME and PATH so your system knows where to find Java.
-
Edit the profile file:
sudo nano /etc/profile.d/java.sh
-
Add the following lines: IMPORTANT: Replace the path with the actual directory name you extracted.
#!/bin/sh export JAVA_HOME=/opt/java/jdk-21.0.2+7 export PATH="$JAVA_HOME/bin:$PATH"
-
Save and exit. (
Ctrl+X,Y,Enterin nano). -
Make the script executable:
sudo chmod +x /etc/profile.d/java.sh
-
Apply the changes:
source /etc/profile.d/java.sh
Step 4: Verify
java -version javac -version echo $JAVA_HOME
All commands should now point to your manually installed JDK.
Summary: Which Method to Choose?
| Method | Pros | Cons | Best For |
|---|---|---|---|
| SDKMAN! | Easy to install/unswitch versions, manages JAVA_HOME automatically, huge repository of tools. |
Requires a one-time installation script. | Almost everyone, especially developers who work with multiple Java versions or projects. |
apt |
Simple, integrated with system package management, security updates via apt. |
Hard to switch versions, JAVA_HOME must be set manually, versions may be outdated. |
Beginners, server administration where only one system-wide Java version is needed. |
| Manual | Full control, install any version, custom locations. | Complex, manual JAVA_HOME setup, must manage updates yourself. |
Advanced users, specific requirements not met by other methods. |
