Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to install Java on Ubuntu

This guide shows you how to install Java on Ubuntu with a few commands. It works on a VPS or dedicated server where you have root or sudo access.

How-To Guides2 min read13 steps3 screenshots

What is Java?

Java is a programming language. Many tools, such as Tomcat and Apache Spark, need Java to run. Think of Java as the engine those tools use.

There are two parts. The JRE (Java Runtime Environment) only runs Java programs. The JDK (Java Development Kit) also lets you build them. If you are unsure, install the JDK.

Steps

  1. Log in to your server over SSH.
  2. Check if Java is already there.
java -version

If you see "command not found", Java is not installed.

  1. Update the package list.
sudo apt update

This refreshes the list of software you can install.

  1. Install the default JDK.
    oracle-java11-installer page
sudo apt install -y default-jdk

This picks the Java version that suits your Ubuntu release.

JavaScript version 11.0.13 script
  1. Check the install worked.
java -version
javac -version

The first command shows the runtime. The second shows the compiler, which turns your code into a program.

Install a specific version

Some apps need an exact Java version. Search for the ones you can install.

apt search openjdk

Then install one by name. Here is an example for version 17.

sudo apt install -y openjdk-17-jdk

Switch between versions

  1. Run this command.
sudo update-alternatives --config java
  1. You will see a numbered list of installed versions.
    Linux x64 compressed archive .tar.gz package
  2. Type the number you want and press Enter.

Set JAVA_HOME

Some programs look for a setting called JAVA_HOME. It tells them where Java lives.

  1. Find the Java path with readlink -f $(which java).
  2. Open the file /etc/environment with a text editor such as nano.
  3. Add a line like JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64". Remove the /bin/java end from the path you found.
  4. Save the file and log in again.
  5. Check it with echo $JAVA_HOME.
Tip: Take a backup before you edit system files like /etc/environment. A small mistake there can affect every user.

Quick recap

  • Use sudo apt install default-jdk for the easiest install.
  • Check with java -version.
  • Use update-alternatives to switch versions.
  • Set JAVA_HOME if your app asks for it.