How to install Java on Ubuntu Server


Mule and Java have a very close relationship. Mule is a Java-based runtime, which means that Mule runs on the Java Virtual Machine (JVM), and Mule applications are executed in the JVM.


That’s why the first requirement to work with the mule runtime in your own server is to have Java installed. For us, Mulesoft Architects and developers, this is an important concept - we need to know well the environment in which our Mule runtime will be installed, which will help us understand much better how the Mule runtime works.

Mulesoft recommends to install the Java JDK (not the JRE) for the Mule runtime. In this post, we will see how to install the Java JDK on a Linux server (Ubuntu).

If you need to install Java on Windows server for the Mule runtime have a look at these two other posts:


Prerequisites

  • In this tutorial, we will be installing the Java JDK on an Ubuntu Server 24.04 LTS
  • We will be using the APT package manager. Before you install anything make sure you update the repositories on your Ubuntu by running
sudo apt-get update

  • Make sure you understand the difference between JVM, JRE and JDK in Java and the different JDKs (OracleJDK, OpenJDK, AdoptOpenJDK). Check out this post - JVM, JRE and JDK - Which one do I need for Mule?
  • We will be installing OpenJDK versions


Check if Java is already installed

Before we install anything, let’s double check that there’s no previous installation of Java in the system. For that, SSH into your server and run the command
java --version


Install the latest version of the OpenJDK

To install the latest version of the OpenJDK you just need to run the command:
sudo apt-get install default-jdk

At the time we’re writing this post, the latest version is OpenJDK 21. Depending on when you’re reading this post the latest version of the OpenJDK might be different. If you don’t have any particular requirement for your JAVA applications, this option would be the recommend one. 

Install a specific version of OpenJDK

However, as it is our case, make sure you understand what’s the version you need for your mule apps and the Mule runtime version you’d be using, it’s very likely that the latest one won’t be compatible with all your apps and runtimes. 

From Ubuntu, you can run the following command to list the different OpenJDK versions available:
apt search openjdk



In general, you can install a specific version of the OpenJDK following this syntax:
sudo apt-get install openjdk-#-jdk

Where you’d be replacing ‘#’ with your preferred version number.


This way, we can install the OpenJDK 11 with the command:

sudo apt-get install openjdk-11-jdk


Verify the Installation

Once your installation is complete, let’s run again the java version command to verify that now the system recognizes the command;
java --version


Set the JAVA_HOME environment variable

The JAVA_HOME environment variable determines the location of your Java installation. It’s not mandatory to set this up, but some Java applications might require it.

To set up the JAVA_HOME env variable, first we need to locate where the OpenJDK is installed. For that, we can run the command:
sudo update-alternatives —config java



Next, open the .profile file from the home directory
vi ~/.profile

Add the following lines, using the path to the root folder of your OpenJDK installation (don’t include the /bin). In our example:

JAVA_HOME='/usr/lib/jvm/java-11-openjdk-amd64'
PATH="$PATH:$JAVA_HOME/bin"
export JAVA_HOME
export PATH

With these lines, we’re doing three things:

  • Define the JAVA_HOME env variable
  • Add the Java OpenJDK bin folder to the PATH env variable so that we can run Java anywhere from the system
  • The ~/.profile file will make these env variable values available to any shell session
Save the file and relaunch the terminal or execute the command below to apply the configuration changes
source ~/.profile 

Verify that now you've got the JAVA_HOME in the list of env variables and that it has been added to the PATH. Run the command:

$ env


Previous Post Next Post