How to Install Java on Ubuntu: A Step-by-Step Guide for Beginners
Java is a versatile and widely-used programming language that powers applications from web servers to Android apps. To develop or run Java programs on Ubuntu, you need to install the Java Development Kit (JDK), which includes the tools to write, compile, and execute Java code. For beginners, setting up Java on Ubuntu is a crucial first step to start coding. This blog provides a comprehensive, step-by-step guide to installing Java on Ubuntu, covering the JDK, environment configuration, and verification. With detailed explanations and practical tips, you’ll be ready to run your first Java program, like the Hello World program, in no time. Let’s dive into installing Java on Ubuntu and get your development environment up and running!
Why Install Java on Ubuntu?
Ubuntu is a popular Linux distribution favored by developers for its stability, open-source nature, and robust package management. Installing Java on Ubuntu enables you to:
- Develop Java Applications: Write, compile, and debug Java programs using the JDK.
- Run Java Programs: Execute Java-based software, such as IDEs or servers, using the Java Runtime Environment (JRE).
- Explore Frameworks: Use Java frameworks like Spring or Hadoop, common in enterprise and big data environments.
- Learn Programming: Practice Java fundamentals, from data types to object-oriented programming.
This guide focuses on installing the OpenJDK, a free and open-source implementation of the JDK, ideal for most development needs. For a broader overview of Java installation across platforms, see the Java Installation Guide.
Understanding Java Components
Before installing, let’s clarify the key components involved:
- Java Development Kit (JDK): Includes the Java compiler (javac), Java Virtual Machine (JVM), JRE, and development tools for building Java applications.
- Java Runtime Environment (JRE): Contains the JVM and libraries to run Java programs but lacks development tools. The JDK includes the JRE, so installing the JDK is sufficient.
- JVM: Executes Java bytecode, ensuring platform independence.
For development, you need the JDK. For a detailed comparison, see JVM vs. JRE vs. JDK.
Choosing a JDK Version
Java releases new versions every six months, with Long-Term Support (LTS) versions receiving updates for years. As of June 8, 2025, Java 21 is the latest LTS version, offering stability and modern features like virtual threads. For beginners, an LTS version like Java 21 is recommended.
You can choose between:
- OpenJDK: Free, open-source, and maintained by the community. Distributions like Adoptium (Temurin) are popular.
- Oracle JDK: Official version with commercial licensing for production use. OpenJDK is sufficient for most cases.
This guide uses OpenJDK 21 from Ubuntu’s package repository for simplicity, but we’ll also cover installing Adoptium’s Temurin for the latest LTS.
Prerequisites
Ensure your Ubuntu system is ready:
- Ubuntu Version: This guide targets Ubuntu 20.04, 22.04, or 24.04 (LTS releases). Commands may vary slightly for other versions.
- Internet Connection: Required to download packages.
- Terminal Access: You’ll use the terminal to run commands.
- Sudo Privileges: Administrative rights are needed to install software.
Open a terminal with Ctrl + Alt + T to begin.
Installing OpenJDK on Ubuntu
Ubuntu’s package manager (apt) makes installing OpenJDK straightforward. Follow these steps to install OpenJDK 21.
Step 1: Update Package Lists
Ensure your system’s package index is up-to-date to avoid installation issues:
sudo apt update
sudo apt upgrade
- sudo: Runs the command with administrative privileges.
- apt update: Refreshes the package list.
- apt upgrade: Updates installed packages (optional but recommended).
Step 2: Install OpenJDK 21
Install the OpenJDK 21 JDK, which includes the JRE and JVM:
sudo apt install openjdk-21-jdk
- openjdk-21-jdk: The package for Java 21 development tools.
- The installer downloads and sets up the JDK, typically in /usr/lib/jvm/java-21-openjdk-amd64.
If you prefer a different version (e.g., Java 17, another LTS), use:
sudo apt install openjdk-17-jdk
To check available versions:
apt search openjdk
Step 3: Verify the Installation
Confirm Java is installed by checking the versions of java and javac:
java -version
javac -version
Example output:
openjdk 21.0.1 2023-10-17
OpenJDK Runtime Environment (build 21.0.1+12-Ubuntu-1)
OpenJDK 64-Bit Server VM (build 21.0.1+12-Ubuntu-1, mixed mode, sharing)
javac 21.0.1
- java -version: Verifies the JRE and JVM.
- javac -version: Confirms the compiler is installed.
- If both commands return version information, the installation is successful.
Step 4: Troubleshooting Installation
If the commands fail:
- “Command not found”: Ensure the package installed correctly. Re-run sudo apt install openjdk-21-jdk.
- Permission Issues: Use sudo for installation commands.
- Outdated Repositories: Run sudo apt update again or check your internet connection.
Configuring Environment Variables
To use Java tools from any directory, configure environment variables like JAVA_HOME and update the PATH. This is optional but recommended for development.
Step 1: Find the JDK Path
Locate the JDK installation directory:
update-java-alternatives -l
Example output:
java-1.21.0-openjdk-amd64 2111 /usr/lib/jvm/java-21-openjdk-amd64
The path /usr/lib/jvm/java-21-openjdk-amd64 is your JDK directory. Alternatively, check:
ls /usr/lib/jvm
Step 2: Set JAVA_HOME
Add JAVA_HOME to your shell profile (e.g., ~/.bashrc for Bash, ~/.zshrc for Zsh): 1. Open the profile file in a text editor:
nano ~/.bashrc
- Append the following lines at the end:
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
- JAVA_HOME: Points to the JDK directory.
- PATH: Adds the JDK’s bin folder (containing java, javac) to the system path.
3. Save and exit (Ctrl+O, Enter, Ctrl+X in nano). 4. Apply the changes:
source ~/.bashrc
Step 3: Verify JAVA_HOME
Check the JAVA_HOME setting:
echo $JAVA_HOME
Output:
/usr/lib/jvm/java-21-openjdk-amd64
Verify java and javac are accessible:
which java
which javac
Output:
/usr/lib/jvm/java-21-openjdk-amd64/bin/java
/usr/lib/jvm/java-21-openjdk-amd64/bin/javac
Step 4: Troubleshooting Environment Variables
- Empty JAVA_HOME: Ensure the export lines were added correctly and source ~/.bashrc was run.
- “java not found”: Check that $JAVA_HOME/bin is in PATH. Re-edit ~/.bashrc if needed.
- Wrong JDK Path: Confirm the path with update-java-alternatives -l or ls /usr/lib/jvm.
Alternative: Installing Adoptium’s Temurin JDK
If you need the latest OpenJDK version or prefer Adoptium’s Temurin (a reliable OpenJDK distribution), follow these steps to install Java 21.
Step 1: Add Adoptium Repository
- Install required packages:
sudo apt install -y wget apt-transport-https
- Download and add Adoptium’s GPG key:
wget -O - https://packages.adoptium.net/artifactory/deb/pool/main/a/adoptium/adoptium-keyring.gpg | sudo tee /usr/share/keyrings/adoptium.asc
- Add the Adoptium repository:
echo "deb [signed-by=/usr/share/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/adoptium.list
- Update package lists:
sudo apt update
Step 2: Install Temurin JDK
Install the Temurin JDK 21:
sudo apt install -y temurin-21-jdk
Step 3: Verify Installation
Check the version:
java -version
javac -version
Example output:
openjdk 21.0.1 2023-10-17
OpenJDK Runtime Environment Temurin-21.0.1+12 (build 21.0.1+12)
OpenJDK 64-Bit Server VM Temurin-21.0.1+12 (build 21.0.1+12, mixed mode)
javac 21.0.1
Step 4: Set JAVA_HOME for Temurin
Find the Temurin JDK path (e.g., /usr/lib/jvm/temurin-21-jdk-amd64) and update ~/.bashrc:
nano ~/.bashrc
Add:
export JAVA_HOME=/usr/lib/jvm/temurin-21-jdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
Apply changes:
source ~/.bashrc
Verify:
echo $JAVA_HOME
Managing Multiple Java Versions
If you install multiple JDK versions (e.g., OpenJDK 17 and 21), use update-java-alternatives to switch between them.
Step 1: List Available Versions
update-java-alternatives -l
Example output:
java-1.17.0-openjdk-amd64 1711 /usr/lib/jvm/java-17-openjdk-amd64
java-1.21.0-openjdk-amd64 2111 /usr/lib/jvm/java-21-openjdk-amd64
Step 2: Switch Versions
Set a specific version:
sudo update-java-alternatives -s java-1.21.0-openjdk-amd64
Verify:
java -version
Step 3: Update JAVA_HOME
Manually update JAVA_HOME in ~/.bashrc to match the selected version, or use a tool like SDKMAN! for easier management:
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java 21.0.1-tem
Testing Your Installation with a Hello World Program
To confirm your setup, write and run a simple Java program.
Step 1: Create the Program
- Create a directory for your projects:
mkdir ~/JavaProjects
cd ~/JavaProjects
- Create a file named HelloWorld.java:
nano HelloWorld.java
- Add the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java on Ubuntu!");
}
}
- Save and exit (Ctrl+O, Enter, Ctrl+X).
For a detailed explanation, see Hello World Program.
Step 2: Compile and Run
Compile the program:
javac HelloWorld.java
This creates HelloWorld.class.
Run the program:
java HelloWorld
Output:
Hello, Java on Ubuntu!
Step 3: Troubleshooting
- “Class file not found”: Ensure you’re in the correct directory and HelloWorld.java exists.
- “Main class not found”: Verify the class name matches (java HelloWorld, not java helloworld).
- Compilation Errors: Check for typos in the code (e.g., missing semicolon).
Setting Up a Development Environment
With Java installed, choose a tool to write code:
- Text Editors: VS Code with the Java Extension Pack or Vim.
- IDEs: IntelliJ IDEA Community Edition, Eclipse, or NetBeans offer features like code completion and debugging.
To install VS Code:
sudo snap install --classic code
Open VS Code, install the “Extension Pack for Java,” and open your JavaProjects folder to start coding.
Uninstalling Java (Optional)
If you need to remove Java:
sudo apt remove openjdk-21-jdk
sudo apt autoremove
For Temurin:
sudo apt remove temurin-21-jdk
Remove environment variables from ~/.bashrc if set.
FAQ
Do I need both JDK and JRE on Ubuntu?
No, the JDK includes the JRE. Installing openjdk-21-jdk provides both development and runtime capabilities.
Which Java version should I install on Ubuntu?
Choose an LTS version like Java 21 for stability. Java 17 is also a good option if you need compatibility with older tools.
How do I switch between multiple Java versions?
Use sudo update-java-alternatives -s <version></version> to switch, and update JAVA_HOME in ~/.bashrc. Tools like SDKMAN! simplify this process.
Why use OpenJDK instead of Oracle JDK?
OpenJDK is free, open-source, and sufficient for most development. Oracle JDK requires a commercial license for production use but offers additional enterprise features.
What if javac is not found after installation?
Ensure you installed the JDK (openjdk-21-jdk), not just the JRE. Check PATH includes $JAVA_HOME/bin and re-install if needed.
Conclusion
Installing Java on Ubuntu is a straightforward process that sets you up for developing powerful Java applications. By installing OpenJDK or Adoptium’s Temurin, configuring environment variables, and testing with a Hello World program, you’ve established a solid development environment. Continue your journey by exploring Java fundamentals like variables or control flow statements. With Java installed, you’re ready to code, experiment, and build exciting projects on Ubuntu!