What you need to do first

Installing Java on Linux means downloading a package from Oracle or your Linux distribution's repository, then running a few commands in the terminal. Most Linux systems come with a package manager — a tool that finds, downloads, and installs software for you — so you won't need to hunt for files manually. The simplest path is to use your distribution's built-in package manager, which handles all the setup steps at once.

Before you start, open a terminal window. On Ubuntu or Debian, press Ctrl+Alt+T. On other distributions, look for "Terminal" in your applications menu. You'll type commands here to check what you have and install what you need.

Key Takeaways

  • Ubuntu and Debian users can install Java in one command using apt, while Fedora and CentOS users use dnf or yum instead.
  • You have two choices: OpenJDK (free, maintained by the community) or Oracle JDK (Oracle's official version), and most people use OpenJDK.
  • After installation, verify Java is working by typing java -version in the terminal to see which version you have.
  • If you need multiple Java versions or want to switch between them, use the update-alternatives command on Debian-based systems.

Installing Java on Ubuntu or Debian

Ubuntu and Debian use a package manager called apt. Open your terminal and type this command:

sudo apt update

This refreshes your package list so apt knows what versions are available. You'll be asked for your password — type it and press Enter. Then install Java with:

sudo apt install default-jdk

This installs OpenJDK, which is the free version of Java. The word "default" means apt will pick the most recent stable version for your system. If you want Oracle's official JDK instead, type sudo apt install oracle-java21-installer (replace 21 with whichever version number you need). Oracle's version requires you to accept their license terms during installation — just read the prompt and press Tab, then Y to agree.

The installation takes a few minutes. When it finishes, move to the verification step below.

Installing Java on Fedora or CentOS

Fedora and CentOS use dnf (or yum on older CentOS versions). Open your terminal and type:

sudo dnf install java-latest-openjdk

This installs the most recent OpenJDK version. If you want a specific version — say, Java 17 instead of the latest — type sudo dnf install java-17-openjdk. For Oracle's JDK, you'll need to read it manually from Oracle's website and follow their installation guide, because Fedora and CentOS don't host Oracle's version in their repositories.

The installation takes a few minutes. When it finishes, move to the verification step below.

Verifying Java is installed and working

After installation completes, check that Java is working by typing this command in your terminal:

java -version

You should see output that looks like this:

openjdk version "21.0.1" 2023-10-17 OpenJDK Runtime Environment (build 21.0.1+12-Ubuntu-0ubuntu0.23.10.1) OpenJDK 64-Bit Server VM (build 21.0.1+12-Ubuntu-0ubuntu0.23.10.1, mixed mode, sharing)

The exact numbers will differ depending on your version, but if you see version information, Java is installed. If you see "command not found", the installation didn't complete — go back and check that the apt or dnf command finished without errors.

You should also check the Java compiler, which you'll need if you're writing Java code. Type:

javac -version

This should show the same version number. If it says "command not found", you installed the Java Runtime Environment (JRE) instead of the full Development Kit (JDK). Go back and reinstall using the commands above — they should give you both.

Switching between multiple Java versions

If you have more than one Java version installed and need to switch between them, use the update-alternatives command on Ubuntu or Debian. First, see what versions you have:

sudo update-alternatives --list java

This shows the full path to each Java installation. Then set which one you want to use by default:

sudo update-alternatives --config java

You'll see a numbered list. Type the number of the version you want and press Enter. From now on, typing java will use that version. Do the same for the compiler:

sudo update-alternatives --config javac

On Fedora and CentOS, use alternatives instead of update-alternatives — the command works the same way.

Setting the JAVA_HOME environment variable

Some programs need to know where Java is installed on your system. They look for a variable called JAVA_HOME. To set it, first find where Java is installed by typing:

which java

This shows you the path, but it's usually a link to the real location. To find the real location, type:

readlink -f $(which java)

Copy the path it shows you, but remove the /bin/java part at the end. For example, if the output is /usr/lib/jvm/java-21-openjdk-amd64/bin/java, your JAVA_HOME is /usr/lib/jvm/java-21-openjdk-amd64.

Now open your shell configuration file. If you use bash, type:

nano ~/.bashrc

If you use zsh, type nano ~/.zshrc instead. At the end of the file, add this line (replace the path with your actual path):

export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64

Press Ctrl+O, then Enter to save, then Ctrl+X to close. Close your terminal and open a new one, then type echo $JAVA_HOME to confirm it's set.

Uninstalling Java if you need to

If you installed Java and want to remove it, use your package manager. On Ubuntu or Debian, type:

sudo apt remove default-jdk

Or replace default-jdk with oracle-java21-installer if you installed Oracle's version. On Fedora or CentOS, type:

sudo dnf remove java-latest-openjdk

This removes Java but leaves your configuration files in place. If you want to remove everything including configuration, add --purge to the apt command (dnf doesn't have this option). You can reinstall Java later without any conflicts.

Frequently Asked Questions

What's the difference between OpenJDK and Oracle JDK?

OpenJDK is free and maintained by the community. Oracle JDK is Oracle's official version and requires a paid license for commercial use. For learning, testing, or open-source projects, OpenJDK works identically and is the standard choice on Linux.

Do I need both Java and the Java compiler?

If you're only running Java programs that someone else wrote, you need the Java Runtime Environment (JRE). If you're writing Java code, you need the full Development Kit (JDK), which includes the compiler. The commands above install the JDK, which includes the JRE.

Why does my terminal say "command not found" after installation?

Your terminal hasn't reloaded the list of available commands. Close the terminal window completely and open a new one. If it still doesn't work, the installation didn't finish — check for error messages in the terminal output and try the install command again.

Can I have Java installed in two places at once?

Yes. You can install multiple versions using your package manager, and they won't interfere with each other. Use update-alternatives to switch between them, or run them from their full paths directly.

What if I want a specific Java version, not the latest?

On Ubuntu or Debian, type apt search openjdk to see all available versions, then install the one you want — for example, sudo apt install openjdk-17-jdk. On Fedora or CentOS, use dnf search java-openjdk the same way.