How to Install Java on Linux (Ubuntu, Debian & Fedora)

Installing Java on Linux is straightforward when you use your distribution’s package manager. For most users, OpenJDK is the simplest choice: it provides the Java runtime and development tools without requiring you to download an old archive and configure system paths by hand.
This guide covers Ubuntu and Debian-based distributions, Fedora, installation checks, switching between installed Java versions, and a small test program. Package versions vary by Linux release, so use the version required by your application or project rather than assuming every distribution ships the same release.
1. Understand the difference between JDK and JRE
- JRE (Java Runtime Environment): the components needed to run Java applications.
- JDK (Java Development Kit): the runtime plus development tools such as the Java compiler (
javac). Install the JDK if you want to write, compile, or build Java programs.
If you are unsure, install the JDK. It is the right choice for most learners and developers. A server that only runs an already-built Java application may need only a runtime package.
2. Check which Linux distribution you are using
Open Terminal and run:
cat /etc/os-release
Look for the distribution name. The commands below cover Ubuntu/Debian and Fedora. Linux Mint and other Ubuntu-based distributions generally use APT, but package availability still depends on the release and its configured repositories.
3. Install Java on Ubuntu or Debian
For a normal development setup, install the default JDK provided for your distribution:
sudo apt update
sudo apt install default-jdk
The default-jdk package follows the Java development kit selected by that Debian-based release. This avoids hard-coding an old Java version into your installation steps.
Install only the runtime
If you only need to run Java applications and will not compile Java code, install the default runtime instead:
sudo apt update
sudo apt install default-jre
Install a specific Java version
Some projects, application servers, build tools, or workplace environments require a particular Java major version. Ubuntu provides versioned OpenJDK packages for supported releases. If the required package is available in your repositories, the command follows this pattern:
sudo apt install openjdk-<version>-jdk
For example, a system whose repositories provide OpenJDK 21 can use:
sudo apt install openjdk-21-jdk
Check package availability for your exact Ubuntu or Debian release before running a version-specific command. Do not upgrade a production application to a different Java major version without checking the application’s compatibility requirements.
4. Install Java on Fedora
Fedora provides OpenJDK packages through DNF. To install the JDK and its development tools, run:
sudo dnf install java-devel
If the machine only needs to run Java applications, Fedora also provides runtime-only packages. A headless runtime is suitable for many command-line servers:
sudo dnf install java-openjdk-headless
For applications that need desktop graphics or audio support, use the full runtime package documented by Fedora:
sudo dnf install java-openjdk
5. Verify that Java is installed correctly
After installation, check the Java runtime version:
java -version
If you installed the JDK, check that the compiler is available too:
javac -version
Both commands should print a version. If java works but javac is not found, you may have installed only a runtime package rather than the JDK.
6. Compile and run a simple Java program
You can confirm that the compiler and runtime work together by creating a small program. Save the following code in a file named Hello.java:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Linux!");
}
}
In Terminal, move to the directory containing the file and compile it:
javac Hello.java
Then run the compiled class:
java Hello
You should see:
Hello, Linux!
If the compiler reports an error, check that the file is named Hello.java and that the public class name is exactly Hello.
7. Switch between multiple Java versions
You may need more than one JDK when different projects require different Java versions. On Ubuntu and Debian, the alternatives system can help you choose which installed executable is used by default.
To select the default Java runtime:
sudo update-alternatives --config java
To select the compiler separately:
sudo update-alternatives --config javac
Choose the entry that matches your project’s requirement. After switching, run java -version and javac -version again to confirm the selected versions. Other distributions may use a different alternatives command or configuration method.
8. Set JAVA_HOME when a tool requires it
Many Java tools detect Java automatically. Set JAVA_HOME only when an IDE, build tool, application server, or deployment guide specifically asks for it.
First, locate the compiler:
readlink -f "$(command -v javac)"
The output normally ends in /bin/javac. The JDK home directory is the parent directory above bin. Set the variable to that JDK directory, not to the java executable itself. For the current Terminal session, the pattern is:
export JAVA_HOME="/usr/lib/jvm/<your-jdk-directory>"
export PATH="$JAVA_HOME/bin:$PATH"
Replace the placeholder with the actual directory on your machine. To make the setting persistent, add the correct values to the startup file used by your shell (for example, ~/.bashrc for Bash), then open a new Terminal window.
9. Common Java installation problems
java: command not found: check whether the runtime package installed successfully. Open a new Terminal session and runcommand -v java.javac: command not found: install a JDK development package, not just the runtime.- The wrong Java version is selected: on Ubuntu or Debian, review the choices with
sudo update-alternatives --config javaandsudo update-alternatives --config javac. UnsupportedClassVersionError: the application was compiled for a newer Java class-file version than the runtime can handle. Use a compatible runtime or rebuild the application for the required target version.- A tool cannot find Java: check whether it expects
JAVA_HOME, and make sure that variable points to the JDK directory.
10. Do you need Oracle JDK instead of OpenJDK?
Most Linux users can start with the OpenJDK packages supplied by their distribution. Use Oracle JDK or another vendor build only when a project, vendor support policy, or compatibility requirement specifically calls for it. Download it from the vendor’s official website, choose the correct CPU architecture and Linux package format, read the applicable licence terms, and follow that vendor’s installation instructions. Avoid copying commands from tutorials written for obsolete Java releases.
Important: Java browser plugins are obsolete
The older version of this tutorial included instructions for adding a Java plugin to Mozilla browsers. Those steps should not be followed on modern systems. Oracle removed the Java deployment stack used by browser applets and Java Web Start from JDK 11. Installing a current JDK does not add a Java browser plugin. Run Java applications as desktop or command-line applications, or use the application’s supported modern deployment method.
Conclusion
For most Ubuntu and Debian users, sudo apt install default-jdk is the simplest way to install Java for development. Fedora users can use sudo dnf install java-devel. Verify both the runtime and compiler, test them with a small program, and select a specific Java version only when your project requires it. Using your distribution’s package manager also makes routine security and maintenance updates easier to manage.
Official documentation and references
- Ubuntu: Set up a Java development environment
- Debian: default-jdk package details
- Fedora: Java installation
- Oracle: Installing the JDK on Linux
- Oracle: JDK 11 release notes and removed deployment features






Thanks for the post…….
Could someone tell me how to use multithreading in applet…