Of course! Here is a comprehensive guide on how to install Java (JDK) on Ubuntu, covering the most common methods and best practices.
Quick Answer: The Easiest & Recommended Way
For most users, the easiest and most recommended method is to use apt to install an OpenJDK package provided by Ubuntu's repositories. This ensures good integration with the system.
To install the latest LTS (Long-Term Support) version of OpenJDK (currently JDK 17):
# Update your package list sudo apt update # Install OpenJDK 17 sudo apt install openjdk-17-jdk
That's it! You can skip to the "Verifying the Installation" section below.
Detailed Guide: Different Methods Explained
There are several ways to install Java on Ubuntu. Here’s a breakdown of the most common methods, from easiest to most flexible.
Method 1: Using apt (Recommended for Beginners)
This method installs Java from Ubuntu's official software repositories. It's simple, reliable, and handles dependencies automatically.
Update Package Lists First, always update your local package index to see the latest available versions.
sudo apt update
Find Available Java Versions You can see which JDK versions are available in the repositories.
apt search openjdk | grep 'jdk-[0-9]'
This will list packages like openjdk-11-jdk, openjdk-17-jdk, openjdk-21-jdk, etc.
Install a Specific JDK Version
It's highly recommended to install a LTS (Long-Term Support) version like JDK 17 or JDK 21, as they receive security updates for a longer period.
-
Install OpenJDK 17 (Recommended LTS):
sudo apt install openjdk-17-jdk
-
Install OpenJDK 21 (Latest LTS):
sudo apt install openjdk-21-jdk
-
Install OpenJDK 11 (Previous LTS):
sudo apt install openjdk-11-jdk
(Optional) Set the Default Java Version
If you have multiple Java versions installed, you might want to set a default one for your system. The update-java-alternatives tool helps with this.
-
List installed Java versions:
update-java-alternatives --list
You'll see output like:
java-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64 java-1.17.0-openjdk-amd64 1711 /usr/lib/jvm/java-1.17.0-openjdk-amd64 -
Set a default version (e.g., JDK 17):
sudo update-java-alternatives --set /usr/lib/jvm/java-1.17.0-openjdk-amd64
Note: Use the path from the
--listcommand output.
Pros:
- Very easy to install.
- Managed by the system's package manager (
apt). - Automatic security updates.
Cons:
- Might not be the absolute latest version available.
- Can be slightly older than the release from the official vendor.
Method 2: Using SDKMAN! (Recommended for Developers)
SDKMAN! is a popular SDK Manager for Unix-based systems. It's the best choice for developers who need to switch between multiple Java versions frequently.
Install SDKMAN! Open a new terminal and run this command:
curl -s "https://get.sdkman.io" | bash
Then, open a new terminal for the changes to take effect.
Install a Java Version SDKMAN! makes it incredibly easy to install and switch versions.
-
List available Java versions:
sdk list java
-
Install a specific version (e.g., OpenJDK 17.0.9):
sdk install java 17.0.9-tem
(The
-temsuffix means it's from Temurin, a high-quality build of OpenJDK.) -
Set the default Java version for your current shell:
sdk default java 17.0.9-tem
Pros:
- Best for managing multiple versions.
- Easy to switch between versions.
- Provides a wide range of Java distributions (Oracle, OpenJDK, Amazon Corretto, etc.).
- Doesn't require
sudo.
Cons:
- Adds another tool to your system.
- Only affects the user who installed it (unless you set it globally).
Method 3: Manual Installation from Official Source (Advanced)
This method gives you the most control and allows you to install the very latest version directly from Oracle or Eclipse Temurin. You should only use this if you have a specific reason.
Download the JDK Go to the official download page. For example, for Eclipse Temurin:
- Eclipse Temurin: https://adoptium.net/
- Oracle JDK: https://www.oracle.com/java/technologies/downloads/
Choose your desired version, OS (Linux x64), and package type (the .tar.gz archive is common).
Extract the Archive
Let's say you downloaded the file to your Downloads folder and it's named OpenJDK17U-jdk_x64_linux_hotspot_17.0.9_9.tar.gz.
# Navigate to your Downloads folder cd ~/Downloads # Create a directory for Java if it doesn't exist sudo mkdir -p /usr/lib/jvm # Extract the archive to the Java directory sudo tar -xzf OpenJDK17U-jdk_x64_linux_hotspot_17.0.9_9.tar.gz -C /usr/lib/jvm
Set Up Environment Variables You need to tell your system where to find the Java binaries.
-
Find the exact path of the extracted JDK:
ls /usr/lib/jvm/
You'll see a folder like
jdk-17.0.9+9. -
Edit the
environmentfile:sudo nano /etc/environment
Add the following lines at the end of the file. Remember to replace
jdk-17.0.9+9with your actual folder name.JAVA_HOME="/usr/lib/jvm/jdk-17.0.9+9" PATH="$PATH:$JAVA_HOME/bin"
Save and exit (
Ctrl+X, thenY, thenEnterin nano). -
Reload the environment:
source /etc/environment
Create Alternatives (Optional but Recommended)
To make it easy to switch with update-java-alternatives, create symlinks:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17.0.9+9/bin/java 1 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-17.0.9+9/bin/javac 1
Pros:
- You get the exact version you want.
- Full control over the installation location.
Cons:
- Complex and manual process.
- You are responsible for security updates.
- Can break if not done carefully.
Verifying the Installation
No matter which method you used, you can verify your Java installation with these commands.
Check the Java Version This command will show the installed version and vendor.
java -version
Expected Output:
openjdk version "17.0.9" 2025-10-17
OpenJDK Runtime Environment Temurin-17.0.9+9 (build 17.0.9+9)
OpenJDK 64-Bit Server VM Temurin-17.0.9+9 (build 17.0.9+9, mixed mode, sharing)
Check the Java Compiler Version
javac -version
Expected Output:
javac 17.0.9
Check the JAVA_HOME Environment Variable
This variable is crucial for many tools like Maven, Gradle, and Spring Boot.
echo $JAVA_HOME
Expected Output:
/usr/lib/jvm/java-17-openjdk-amd64 (if installed via apt)
/usr/lib/jvm/jdk-17.0.9+9 (if installed manually)
If you see a path, it's set correctly. If not, you may need to log out and log back in, or source your shell profile (source ~/.bashrc or source ~/.zshrc).
