Of course! Installing Java on Ubuntu is a common task. There are a few ways to do it, each suited for different needs.

Here’s a complete guide covering the most popular methods, from the easiest to the most flexible.
Quick Answer: The Easiest Way (Recommended for Most Users)
This method uses apt to install a standard OpenJDK package. It's simple, managed by the system, and integrates well with other Ubuntu tools.
-
Update your package list:
sudo apt update
-
Install the default JDK: The
default-jdkpackage is a "meta-package" that will automatically install the latest Long-Term Support (LTS) version of OpenJDK available in your Ubuntu repository. This is the best choice for general development.
(图片来源网络,侵删)sudo apt install default-jdk
-
Verify the installation:
java -version javac -version
You should see output similar to this (the version number may vary):
openjdk version "17.0.8" 2025-07-18 OpenJDK Runtime Environment (build 17.0.8+7-Ubuntu-0ubuntu122.04) OpenJDK 64-Bit Server VM (build 17.0.8+7-Ubuntu-0ubuntu122.04, mixed mode, sharing)
Detailed Guide: Different Methods & Versions
The "Quick Answer" is great, but you might need a specific version or want to use a different distribution. Here are the details.
Method 1: Using apt (The System Package Manager)
This is the method used in the quick answer. It's reliable and handles dependencies for you.

Step 1: Update Package Lists Always start by updating your local package index.
sudo apt update
Step 2: Choose a Java Version to Install
You have two main options:
Option A: Install the Default (Recommended) This installs the latest LTS version (e.g., OpenJDK 17 or 11) that the Ubuntu maintainers have tested and approved.
sudo apt install default-jdk
Option B: Install a Specific Version If you need a specific version for compatibility (e.g., for an older project), you can install it directly.
-
To see available versions:
apt search openjdk | grep 'openjdk-[0-9]' | head -20
This will show you available packages like
openjdk-8-jdk,openjdk-11-jdk,openjdk-17-jdk, etc. -
To install a specific version (e.g., OpenJDK 17):
sudo apt install openjdk-17-jdk
Step 3: Set the JAVA_HOME Environment Variable
Many Java-based tools (like Maven, Tomcat, Spring Boot) require the JAVA_HOME environment variable to be set.
-
Find the Java installation path: The path is usually
/usr/lib/jvm/java-VERSION-openjdk-amd64. You can find it with this command:sudo update-java-alternatives --list
The output will look something like this:
java-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64 java-1.17.0-openjdk-amd64 1171 /usr/lib/jvm/java-1.17.0-openjdk-amd64 java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64Note the path for your desired version (e.g.,
/usr/lib/jvm/java-1.17.0-openjdk-amd64). -
Set
JAVA_HOMEfor your current session:export JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64
Note: This is temporary and will be reset after you reboot.
-
Make
JAVA_HOMEpermanent: Add the export line to your shell's profile file (e.g.,~/.bashrcfor Bash).echo 'export JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64' >> ~/.bashrc source ~/.bashrc # Apply the changes to your current terminal
-
Verify
JAVA_HOME:echo $JAVA_HOME
Method 2: Using SDKMAN! (The Developer's Choice)
SDKMAN! is a popular command-line tool for managing multiple versions of SDKs on Unix-based systems. It's the best choice if you frequently switch between Java versions.
Step 1: Install SDKMAN! Open a new terminal and run this command:
curl -s "https://get.sdkman.io" | bash
Follow the on-screen instructions. It will ask you to open a new terminal window to complete the installation.
Step 2: Install Java with SDKMAN! Now you can easily install any version of Java (including Oracle's JDK and OpenJDK).
-
List available Java versions:
sdk list java
-
Install a specific version (e.g., OpenJDK 17.0.8):
sdk install java 17.0.8-tem
The
-temsuffix stands for Temurin, which is a build of OpenJDK. -
Switch between installed versions:
# Set a version as the default sdk default java 17.0.8-tem # Or just use it for the current session sdk use java 11.0.20-tem
-
Verify the installation:
java -version sdk current java
Method 3: Using the Official Oracle Website
This method is useful if you need the proprietary Oracle JDK, which has different licensing terms and might offer slightly different performance or features.
Step 1: Download the JDK
- Go to the Oracle Java SE Development Kit Downloads page: https://www.oracle.com/java/technologies/downloads/
- Accept the license agreement.
- Select the
.tar.gzarchive for Linux x64.
Step 2: Install the JDK
-
Move the downloaded file to a convenient location, like
/opt.# Replace 'jdk-17.0.8_linux-x64_bin.tar.gz' with your actual filename sudo mv ~/Downloads/jdk-17.0.8_linux-x64_bin.tar.gz /opt/
-
Navigate to the
/optdirectory and extract the archive.cd /opt sudo tar -xzf jdk-17.0.8_linux-x64_bin.tar.gz
-
Create a symbolic link for easier version management.
sudo ln -sfn jdk-17.0.8 jdk
Step 3: Set Environment Variables
This is similar to the apt method but you must do it manually.
-
Create a file for the environment variable:
sudo nano /etc/profile.d/java.sh
-
Add the following lines to the file:
export JAVA_HOME=/opt/jdk export PATH="$PATH:$JAVA_HOME/bin"
Save the file (
Ctrl+O, Enter) and exit (Ctrl+X). -
Apply the changes:
source /etc/profile.d/java.sh
-
Verify the installation:
java -version echo $JAVA_HOME
Summary: Which Method Should You Use?
| Method | Best For | Pros | Cons |
|---|---|---|---|
apt (Default) |
Beginners, general purpose, server deployments. | Simple, managed by OS, handles dependencies. | Limited to versions in Ubuntu's repos. |
| SDKMAN! | Developers who need to switch Java versions often. | Extremely easy version switching, wide selection of JVMs. | Requires a separate tool to be installed. |
| Official Oracle | Projects requiring the proprietary Oracle JDK. | Latest Oracle features, direct from the source. | Manual installation, more complex setup, licensing terms. |
