How to Install and Set Up Java on Ubuntu: Complete Beginner’s Guide (2025)

目次

1. What is Java? When is Java Needed on Ubuntu?

What Kind of Programming Language is Java?

Java is an object-oriented programming language widely used around the world. Its core philosophy is “Write Once, Run Anywhere,” meaning Java applications can run across different operating systems and devices without modification—this is one of its biggest strengths.

Java is used in many development areas including desktop applications, web applications, and especially Android mobile apps. It’s also a major player in building large-scale enterprise and financial systems, making it one of the most actively used programming languages today.

Why is Java Important on Ubuntu?

Java plays an essential role on Linux-based operating systems like Ubuntu. Here are some typical cases where Java is needed:

1. Running Applications Built with Java

Many software tools are built with Java, and they require the Java Runtime Environment (JRE) to run. Examples include development tools like Eclipse and IntelliJ IDEA, or database managers like DBeaver—all of which won’t launch without Java installed.

2. Setting Up a Java Programming Environment

If you’re a developer or student learning Java or building applications on Ubuntu, you’ll need to install the Java Development Kit (JDK). Ubuntu is a popular platform for programming, and it works well with Java, making this combo a favorite among developers.

3. Running Java-Based Server Applications

Java powers many server frameworks like Tomcat and Spring Boot. If you’re running a web application built with Java on an Ubuntu server, a proper Java environment is essential.

Summary: Ubuntu and Java are a Great Match

Thanks to Ubuntu’s open-source nature and lightweight design, it’s an excellent match for Java’s cross-platform capabilities. By installing Java on Ubuntu, you can handle everything from using Java-based apps to developing and running server environments.

年収訴求

2. Java Options on Ubuntu (OpenJDK vs Oracle JDK)

What Java Versions Are Available on Ubuntu?

On Ubuntu, there are mainly two Java environments available: OpenJDK and Oracle JDK.

Both can be used for running and developing Java applications, but they differ in terms of licensing, vendor, and distribution methods. It’s important to choose the right JDK based on your specific use case.

What is OpenJDK? (Default JDK for Ubuntu)

OpenJDK (Open Java Development Kit) is the open-source implementation of Java, originally led by Sun Microsystems (now Oracle). It continues to evolve under an open development model.

Ubuntu uses OpenJDK as its default Java environment, and it can be easily installed using the apt command.

Key Features of OpenJDK

  • Completely free, including for commercial use
  • Easy to install from the official Ubuntu repository
  • Regular security updates are provided
  • Highly stable as a development environment; widely used in enterprises

For general development purposes or if you’re unsure, OpenJDK is usually the recommended choice.

What is Oracle JDK? (Official JDK with Support)

Oracle JDK is the official implementation of Java provided by Oracle Corporation. It’s based on OpenJDK but may include performance enhancements and integrations with certain tools.

However, Oracle JDK comes with a different license, and commercial use may require a paid license (note: personal learning or evaluation is usually fine).

Key Features of Oracle JDK

  • Official support and maintenance from Oracle
  • Commercial use may require license verification
  • Fast delivery of security patches
  • Includes some additional features not found in OpenJDK

While installing it on Ubuntu requires manual download and setup, it’s still a viable option for commercial projects or enterprise environments.

Comparison: OpenJDK vs Oracle JDK

AspectOpenJDKOracle JDK
VendorOpen-source communityOracle Corporation
LicenseGPL + Classpath ExceptionOracle Binary Code License (check for commercial use)
CostFreeMay require a paid license for commercial use
Use CaseGeneral development, learning, internal toolsCommercial services, enterprise-scale deployment
InstallationEasy via aptManual download and setup

Conclusion: OpenJDK is Recommended for Most Ubuntu Users

If you’re setting up a Java environment on Ubuntu, OpenJDK is more than sufficient in most cases. It’s reliable and works seamlessly with the majority of frameworks and libraries.

That said, if you need official support or are building a commercial service, Oracle JDK might be the better choice. Choose the JDK that best fits your needs.

3. Preparation Before Installing Java on Ubuntu

What to Check Before Installing Java

Installing Java on Ubuntu is not too difficult, but a few preparation steps can help ensure a smooth setup. This is especially important for users new to Ubuntu or those who have already installed multiple software packages. Review the following steps before proceeding.

1. Update the Package List

Ubuntu uses APT (Advanced Package Tool) to manage software installations. If your package list is outdated, you might encounter errors such as missing Java packages or old versions being installed.

Start by updating your system’s package list with the following command:

sudo apt update

This command retrieves the latest package information, ensuring that your system installs the correct and most up-to-date Java version.

2. Check for Conflicting Java Installations

In some cases, old Java packages (such as outdated JDKs or JREs) may already be installed, leading to potential conflicts during the installation of a new Java version.

You can check if Java is already installed with this command:

java -version

If a version is displayed, it means Java is already installed. In such cases, it’s often better to uninstall the existing version first, then proceed with a fresh installation to avoid configuration issues.

3. Make Sure You Have Sudo (Administrator) Access

Installing Java requires changes to system directories, so sudo privileges are necessary. Typically, the user created during Ubuntu installation has sudo access by default. If not, you might see an error like this:

user 'yourname' is not in the sudoers file. This incident will be reported.

If you encounter this, ask your system administrator for access or log in as a user with root privileges to perform the installation.

Summary: Preparation Helps Avoid Installation Issues

While installing Java is relatively straightforward, checking for package updates and verifying your environment can prevent unnecessary errors and confusion. If you’re new to Ubuntu, taking the time to prepare will make the rest of the process much smoother.

4. How to Install Java on Ubuntu [Basic Guide]

4.1 How to Install OpenJDK Using APT

Ubuntu uses a package management system called APT (Advanced Package Tool). Since OpenJDK is available in the official Ubuntu repositories, it can be installed easily using the command line.

Install OpenJDK 11 (Stable LTS Version)

sudo apt install openjdk-11-jdk

Install OpenJDK 17 (Newer LTS Version)

sudo apt install openjdk-17-jdk

Replace openjdk-XX-jdk with the version you want. As of April 2025, the recommended Long-Term Support (LTS) versions are 11 and 17.

APT automatically handles dependencies, so even beginners can install Java with confidence.

4.2 Check the Installed Java Version

Once the installation is complete, verify that Java is working correctly by entering the following command in the terminal:

java -version

Example output (OpenJDK 17):

openjdk version "17.0.9" 2024-10-17
OpenJDK Runtime Environment (build 17.0.9+8-Ubuntu)
OpenJDK 64-Bit Server VM (build 17.0.9+8-Ubuntu, mixed mode, sharing)

If you see a similar version output, Java has been successfully installed.

4.3 Managing Multiple Java Versions

Ubuntu allows multiple Java versions to coexist, but only one version is used by default.

If you have multiple Java versions installed, you can select which one to use with the following command:

sudo update-alternatives --config java

This will show a list of available Java versions. Enter the number of the version you want to use as default.

Summary: Start with OpenJDK Installation

If you’re just getting started with Java on Ubuntu, we recommend installing the stable and widely-supported OpenJDK 11 or 17. With the APT command, you can set up your Java environment in just a few minutes—even if you’re new to Linux.

5. How to Set JAVA_HOME (Environment Variable)

What is JAVA_HOME?

JAVA_HOME is an environment variable that tells applications and tools where Java is installed on your system.
Many development and build tools (such as Maven and Gradle) rely on this variable to locate Java’s runtime path.

Installing Java alone doesn’t automatically set this variable, so you’ll need to configure it manually.

How to Find the JAVA_HOME Path

First, check where Java is currently installed using the following command:

readlink -f $(which java)

Example output:

/usr/lib/jvm/java-17-openjdk-amd64/bin/java

In this case, the JAVA_HOME path is everything before bin/java, like this:

/usr/lib/jvm/java-17-openjdk-amd64

Steps to Set JAVA_HOME

To set the environment variable, you need to add it to your shell configuration file. On Ubuntu, this is typically ~/.bashrc or ~/.bash_profile (or ~/.zshrc for Zsh users).

Step 1: Edit Your ~/.bashrc File

nano ~/.bashrc

Scroll to the bottom and add the following lines (replace the path with your actual Java path):

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

Step 2: Apply the Changes

After saving the file, run the following command to apply the changes:

source ~/.bashrc

Verify That JAVA_HOME is Set

Check that JAVA_HOME is correctly set by running:

echo $JAVA_HOME

If the path displayed matches the one you set, the configuration was successful.

Note: Switching Between Multiple JDK Versions

If you switch Java versions later, you’ll also need to update the JAVA_HOME path accordingly.
Use the update-alternatives command to switch versions and make sure JAVA_HOME matches the selected one.

Summary: JAVA_HOME is Essential for Development Tools

Setting JAVA_HOME is important for many Java-based applications and tools. While it takes a bit of manual setup, it helps prevent issues down the road—especially in development environments.

6. How to Manage and Switch Between Multiple Java Versions

Why Use Multiple Java Versions?

Some development environments or applications require specific versions of Java to function properly.
For example, one tool might only work with Java 8, while another project may require Java 17.

Ubuntu allows you to install multiple Java versions and easily switch between them as needed.

How to Switch Java Versions

Use the update-alternatives command in Ubuntu to manage and switch Java versions.

Step 1: Install Multiple Java Versions

First, install all the versions of Java you want to use. For example, to install Java 8 and Java 17:

sudo apt install openjdk-8-jdk
sudo apt install openjdk-17-jdk

Step 2: Use update-alternatives to View and Select Versions

Run this command to display a list of installed Java versions and choose the one you want to use:

sudo update-alternatives --config java

Example output:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java     1111      auto mode
  1            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java  1081      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java     1111      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Select the number corresponding to the version you want to use.

Also Switch javac (Java Compiler)

In addition to switching java, you should also switch javac, which is used to compile Java code. Use the following command:

sudo update-alternatives --config javac

As with java, select the version you want from the list.

Update JAVA_HOME if Needed

After switching Java versions, remember to update the JAVA_HOME environment variable as well. Make sure the path matches the selected version from update-alternatives.

Common Mistakes and Fixes

  • Switched Java but IDE doesn’t reflect the change:
    → Be sure to update the Java path inside your IDE (like Eclipse or IntelliJ IDEA).
  • java works but javac doesn’t:
    → You need to run update-alternatives for javac separately.

Summary: Efficiently Manage Multiple Projects

Ubuntu makes it easy to install and switch between different versions of Java. This flexibility is especially useful for developers working on multiple projects or frameworks that require specific Java versions.

7. Common Errors and How to Fix Them

Error 1: “Unable to locate package openjdk-xx-jdk”

Symptoms

You may see this error when trying to install Java via the APT command:

E: Unable to locate package openjdk-17-jdk

Causes

  • Package list is outdated
  • Spelling mistake (e.g., openjdk-17-jdk)
  • The Java package version does not exist for your Ubuntu release

Solutions

First, update your package list with:

sudo apt update

If the issue persists, the version you’re trying to install might not be available for your current Ubuntu version. Use this command to search for available OpenJDK packages:

apt search openjdk

Error 2: “java: command not found”

Symptoms

Even after installation, you might encounter this message:

Command 'java' not found, but can be installed with:
sudo apt install default-jre

Causes

  • Java was not installed correctly
  • The Java path is not included in your PATH environment variable

Solutions

Install Java with one of the following commands:

sudo apt install default-jre

Or install a specific version:

sudo apt install openjdk-17-jdk

Then run java -version to verify that it works.

Error 3: JAVA_HOME Not Reflected After Setting

Symptoms

You’ve set the JAVA_HOME variable, but running echo $JAVA_HOME returns nothing or an incorrect path.

Causes

  • You’ve edited .bashrc but haven’t run source ~/.bashrc
  • There’s a typo or incorrect path in the setting

Solutions

  1. Open your config file to review settings:
nano ~/.bashrc
  1. Apply the changes:
source ~/.bashrc
  1. Verify the value:
echo $JAVA_HOME

Error 4: Java Works but javac Does Not

Symptoms

When trying to compile Java code, you see:

Command 'javac' not found

Cause

  • Only the JRE (Java Runtime Environment) is installed; the JDK (Java Development Kit) is missing

Solution

Install the JDK with this command:

sudo apt install openjdk-17-jdk

Then verify it:

javac -version

Error 5: Java Version Doesn’t Switch Even After Selection

Symptoms

You used update-alternatives to switch versions, but the version shown hasn’t changed.

Causes

  • You didn’t switch both java and javac
  • JAVA_HOME is still pointing to the old version

Solutions

sudo update-alternatives --config java
sudo update-alternatives --config javac

Also update your JAVA_HOME variable to match the selected version.

Summary: Stay Calm and Troubleshoot

Most Java-related issues on Ubuntu are caused by configuration problems or typos. Read the error messages carefully and follow the steps above to resolve them.
Bookmark this guide as a reference for future troubleshooting.

8. How to Install Java Using the GUI (Beginner-Friendly)

Don’t Worry If You’re Not Comfortable With the Terminal

Ubuntu is known as one of the most user-friendly Linux distributions, but many people find the command line a bit intimidating at first.
Fortunately, Ubuntu provides a graphical interface (GUI) that allows you to install software like Java with just a few clicks.

This section shows you how to install Java (OpenJDK) using the built-in “Ubuntu Software” tool, also known as the Software Center.

Steps to Install Java Using Ubuntu Software

Step 1: Open Ubuntu Software

  1. Click on “Ubuntu Software” from the Activities menu or the launcher on the left side.
  2. Once open, click the search icon (magnifying glass) at the top right.

Step 2: Search for “OpenJDK”

In the search bar, type “openjdk”.
After a moment, you should see results such as “OpenJDK Java Runtime” or “OpenJDK Java Development Kit.”

Step 3: Choose a Version and Install

  • Click on a version, such as “OpenJDK 11 Java Development Kit.”
  • Click the “Install” button on the details screen.
  • When prompted, enter your Ubuntu password to authorize the installation.

The installation will begin and should be completed in less than a minute.

How to Confirm the Installation (Without Terminal)

If you’d like to confirm that Java is installed but prefer not to use the terminal, here are two options:

  1. Open the terminal and type java -version (it’s a simple command just for checking).
  2. Or, try launching a Java-based application like DBeaver or Eclipse and confirm it starts properly.

Note: GUI Installs May Have Limited Version Options

The versions of Java available through Ubuntu Software are not always the latest. Also, managing multiple versions or fine-tuning configurations is harder through the GUI.

For more advanced development work, it’s worth learning basic terminal commands over time.

Summary: GUI Installation is Perfect for Beginners

Installing Java on Ubuntu is easy even without using the terminal. The GUI method is perfect for beginners or those who just want to quickly get started. Once you’re comfortable, you can gradually explore the terminal to unlock more control and flexibility.

9. Setting Up a Java Development Environment [+Extras]

What Do You Need for Java Development?

To build Java applications, you’ll typically need the following tools:

ToolPurpose
JDK (Java Development Kit)Required to compile and run Java programs
Source Code Editor / IDEUsed for writing, debugging, and managing code
Build Tool (Maven or Gradle)Handles project dependencies and automates builds

Top 3 Java IDEs for Ubuntu

1. Eclipse

  • A classic, full-featured IDE for Java development
  • Stable and widely used for years
  • Supports web, Android, and Spring development with plugins

Installation (via Snap):

sudo snap install eclipse --classic

2. IntelliJ IDEA

  • Modern, feature-rich IDE with smart code completion
  • Free Community Edition and paid Ultimate Edition available
  • Excellent UI and productivity features

Installation (via Snap):

sudo snap install intellij-idea-community --classic

3. Visual Studio Code (VS Code)

  • Lightweight and fast source code editor
  • Not a full IDE, but powerful with Java extensions
  • Great for multi-language development (Python, C, etc.)

Installation:

sudo snap install code --classic

After installing, add the “Java Extension Pack” for full Java development support.

Installing a Build Tool (Maven / Gradle)

To manage dependencies and automate builds, most Java projects use a build tool. The two most common options are:

Install Apache Maven:

sudo apt install maven

Install Gradle (some versions may require extra setup):

sudo apt install gradle

These tools help streamline your development workflow and make project builds more efficient.

Summary: You Can Build Powerful Java Apps on Ubuntu

Ubuntu is lightweight, stable, and works very well with Java. Whether you’re just starting out or working on professional projects, combining Ubuntu with tools like Eclipse, IntelliJ IDEA, Maven, or Gradle provides a solid foundation for Java development.

If you want to move from simply “using” Java to actually “creating” with it, building your development environment is the perfect first step.

10. Frequently Asked Questions (FAQ)

Q1. Which should I choose: OpenJDK or Oracle JDK?

In most cases, OpenJDK is perfectly fine. It’s officially supported by Ubuntu, open-source, and free to use—even commercially.
On the other hand, Oracle JDK is optimized for enterprise use and includes official support and additional features. But keep in mind: commercial use of Oracle JDK may require a paid license.

Q2. How do I install the latest version of Java on Ubuntu?

Ubuntu’s official repositories may not always include the latest Java version. If you need a newer version, try one of these methods:

  • Add a PPA (e.g., sudo add-apt-repository ppa:openjdk-r/ppa)
  • Manually install from Oracle’s official site

However, for most users, it’s recommended to use an LTS (Long-Term Support) version like OpenJDK 11 or 17 for stability and security.

Q3. Do I really need to set JAVA_HOME?

If you’re just running Java applications, setting JAVA_HOME is usually not required.
However, it’s essential if you’re using:

  • Development tools (like Eclipse or IntelliJ IDEA)
  • Build tools (like Maven or Gradle)
  • Apps or frameworks that rely on the JAVA_HOME path

If you plan on doing any Java development, it’s a good idea to set it early on.

Q4. How can I uninstall Java?

If you installed Java via APT, you can remove it using this command:

sudo apt remove openjdk-17-jdk

If you have multiple versions installed, just repeat the process for each one. After uninstalling, run java -version to confirm it’s no longer active.

Q5. I switched Java versions, but it’s not taking effect—what should I do?

If the version doesn’t change after running update-alternatives, try the following:

sudo update-alternatives --config java
sudo update-alternatives --config javac

Also double-check that JAVA_HOME is updated to reflect the new version.

Q6. Should I use the GUI or Terminal to install Java?

For basic Java installation, the GUI method is totally fine—especially if you’re new to Ubuntu. The Ubuntu Software app makes it easy to get started.
However, if you need to manage multiple versions or configure your environment more deeply, the terminal offers much more control.

Final Thoughts: Come Back Here Anytime You Get Stuck

Java has been around for a long time and has a massive support ecosystem. With the tips in this guide, you can confidently install, configure, and manage Java on Ubuntu.
Bookmark this FAQ for future reference—it’ll save you time when troubleshooting or revisiting your setup.