Installing Java on Linux: The Two Main Routes

Java runs on Linux through two different packages: OpenJDK (free, open-source, and what most Linux distributions recommend) and Oracle Java (the official version from Oracle, which requires you to accept their license terms). Most people on Linux use OpenJDK because it's already in your system's package manager and installs in seconds. If you need Oracle Java specifically — usually because a particular program requires it — that takes a few extra steps but is still straightforward.

The method you use depends on which Linux distribution you're running. Ubuntu, Debian, Fedora, and CentOS all have Java in their built-in package managers, so you won't need to download anything manually. You'll use either apt (Ubuntu and Debian) or dnf (Fedora and newer CentOS) to pull Java from the repository and install it automatically.

Key Takeaways

  • OpenJDK installs in one command on Ubuntu and Debian using sudo apt install default-jdk, and on Fedora using sudo dnf install java-latest-openjdk.
  • After installation, verify Java is working by opening a terminal and typing java -version — you should see the version number appear.
  • If you need Oracle Java instead of OpenJDK, you must download it from Oracle's website and follow their installation steps, because Oracle's license terms prevent it from being in most package managers.
  • Java sets its own PATH automatically on Linux, so you do not need to manually edit environment variables in most cases.
  • If a program cannot find Java after you install it, the program may be looking in a specific folder — check the program's documentation for where it expects Java to be located.

Installing OpenJDK on Ubuntu or Debian

Open a terminal window (press Ctrl+Alt+T on most Ubuntu systems, or search "Terminal" in your applications menu). Type the following command and press Enter:

sudo apt update

This refreshes your package list so you get the newest version of Java available. You'll be asked for your password — type it in and press Enter. Nothing will appear on screen as you type, which is normal.

Once that finishes, type:

sudo apt install default-jdk

Press Enter. The system will show you how much space Java will take (usually around 200 to 300 megabytes) and ask if you want to continue. Type y and press Enter. Java will download and install automatically. This usually takes one to three minutes depending on your internet speed.

Installing OpenJDK on Fedora or CentOS

Open a terminal and type:

sudo dnf install java-latest-openjdk

Press Enter and type your password when asked. The system will show you the download size and ask for confirmation — type y and press Enter. The installation will complete in a minute or two.

If you're on an older CentOS system (version 7 or earlier), use yum instead of dnf:

sudo yum install java-openjdk

The process is identical — you'll see the same confirmation prompt and the same installation time.

Verifying That Java Installed Correctly

After the installation finishes, open a new terminal window (or type in the same one) and type:

java -version

Press Enter. You should see output that looks like this:

openjdk version "17.0.1" 2021-10-19OpenJDK Runtime Environment (build 17.0.1+12-Ubuntu-0ubuntu120.04)OpenJDK 64-Bit Server VM (build 17.0.1+12-Ubuntu-0ubuntu120.04, mixed mode, sharing)

The exact version number will be different depending on when you installed it, but as long as you see a version number and no error message, Java is working. If you see "command not found", Java did not install properly — go back and check that the installation command completed without errors.

Installing Oracle Java If You Need It

Most programs work fine with OpenJDK, but some require Oracle Java specifically. If that's your situation, you'll need to download it directly from Oracle's website because their license terms prevent it from being in most Linux package managers.

Go to oracle.com/java/technologies/downloads in your web browser. Click the download button for Linux, then choose either x64 or ARM64 depending on your system (most desktop and laptop Linux systems use x64). You'll need to accept Oracle's license agreement before the download starts.

Once the file downloads (it will be named something like jdk-21_linux-x64_bin.tar.gz), open a terminal in the folder where it downloaded. Type:

sudo tar -xzf jdk-21_linux-x64_bin.tar.gz -C /opt

Replace jdk-21_linux-x64_bin.tar.gz with whatever your actual filename is. This extracts Oracle Java into the /opt folder, which is the standard location for manually installed programs on Linux. You'll be asked for your password.

After that completes, you need to tell your system where to find Oracle Java. Type:

sudo update-alternatives --install /usr/bin/java java /opt/jdk-21/bin/java 1

Again, replace jdk-21 with whatever folder name was created when you extracted the file. This creates a link so that when you type java, the system knows to use Oracle's version. Verify it worked by typing java -version — you should see Oracle Java's version information instead of OpenJDK's.

Troubleshooting When Java Won't Run

If you installed Java but a program still can't find it, the program may be looking in a specific folder. Check the program's documentation or help file for a setting called "Java home" or "JDK location". You can find where Java is installed by typing:

which java

This shows the path to Java. For OpenJDK on Ubuntu, it's usually something like /usr/lib/jvm/default-java. For Oracle Java installed manually, it's /opt/jdk-21 (or whatever version you downloaded). Copy that path into the program's settings.

If a program says it needs a specific Java version (like Java 11 or Java 17) and you have a different one installed, you can install multiple versions side by side. Just run the installation command again with the version number specified — for example, on Ubuntu you could type sudo apt install openjdk-11-jdk to install Java 11 alongside whatever version you already have.

Frequently Asked Questions

Do I need to install both OpenJDK and Oracle Java?

No. Most programs work with OpenJDK alone. Install Oracle Java only if a specific program requires it or if you have a reason to prefer it. You can have both installed at the same time, but you only need one.

What's the difference between JDK and JRE?

JDK (Java Development Kit) is for people who write Java programs — it includes tools to compile and debug code. JRE (Java Runtime Environment) is just for running Java programs that someone else wrote. The default-jdk package installs the full JDK. If you only need to run programs, you can use default-jre instead, which is smaller.

Will Java slow down my Linux system?

Java itself takes up about 200 to 300 megabytes of disk space and uses very little memory when it's not running. When a Java program is actually running, it uses whatever memory that program needs — the same as any other application. Installing Java does not slow down your system.

Can I uninstall Java if I don't need it anymore?

Yes. On Ubuntu or Debian, type sudo apt remove default-jdk. On Fedora, type sudo dnf remove java-latest-openjdk. If you installed Oracle Java manually, delete the folder with sudo rm -r /opt/jdk-21 (replacing the version number with yours) and then remove the link with sudo update-alternatives --remove java /opt/jdk-21/bin/java.

Why does my terminal say "command not found" after I install Java?

This usually means the installation didn't complete successfully, or you're using a shell that hasn't reloaded. Try closing your terminal completely and opening a new one. If you still see the error, run the installation command again and watch for any error messages during the process.