Install Default Java with APT on Ubuntu 22.04

In this guide, we want to teach you to Install Default Java (JRE and JDK) on Ubuntu 22.04 with APT package manager. Also, you will learn to manage multiple Java installations and set up the Java_Home environment path on Ubuntu 22.04. Java is a popular programming language that is used for many apps and software. The default Java version on Ubuntu 22.04 is Java 11.

How To Install Default Java with APT on Ubuntu 22.04?

To install default JDK and JRE on Ubuntu 22.04, you must have access to your server as a non-root user with sudo privileges. To do this, you can check this guide on Initial Server Setup with Ubuntu 22.04.

Now follow the steps below to complete this guide.

Step 1 – How To Run System Update on Ubuntu 22.04?

First, you must update your local package index with the command below:

sudo apt update

Then, check that you have Java installed on your server by checking its version:

java -version

If Java is not currently installed, you’ll receive the following output:

Output
Command 'java' not found

Step 2 – Install Default JRE and JDK on Ubuntu 22.04

At this point, you can use APT to install the default JRE from OpenJDK 11:

sudo apt install default-jre -y

When your installation is completed, you can verify it by checking its version:

java -version

In your output, you should see:

Output
openjdk version "11.0.19" 2023-04-18
OpenJDK Runtime Environment (build 11.0.19+7-post-Ubuntu-0ubuntu122.04.1)
OpenJDK 64-Bit Server VM (build 11.0.19+7-post-Ubuntu-0ubuntu122.04.1, mixed mode, sharing)

To compile and run some Java-based software, you may need JDK. To install the JDK, run the following command on Ubuntu 22.04, which will also install the JRE:

sudo apt install default-jdk -y

Verify that the JDK is installed by checking the version of Javac which is the Java compiler:

javac -version

In your output, you should see:

Output
javac 11.0.19

Step 3 – How To Manage Multiple Java Installations on Ubuntu 22.04?

At this point, you can configure which Java version is the default for use if you have multiple Java installations on your server. To do this, you can use the following command in your command line:

sudo update-alternatives --config java

In my case, I get the following output:

Output
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Here I have 2 choices for Java. You can type your selection number or press enter to keep the current choice. I keep the current choice with Java 17.

Also, you can do this for your Java compiler, to do this, you can run the command below:

sudo update-alternatives --config javac
Example Output
There are 2 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                          Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/javac   1711      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/javac   1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/javac   1711      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Step 4 – Set up Java Home Environment Variable on Ubuntu 22.04

As you know, programs that are written in Java will use the Java_Home environment variable to find which Java installation location. To do this, you need to know where your Java is installed. You can check it by using the command below:

sudo update-alternatives --config java
Output
  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode

As you can see from the output, OpenJDK 17 is installed under the following path:

/usr/lib/jvm/java-17-openjdk-amd64/bin/java

Next, open the /etc/environment file with your favorite text editor, here we use the vi editor:

sudo vi /etc/environment

Note: This file may be blank. At the end of the file, add the following line, making sure to replace the path with your own, and not include the bin/ portion of the path:

JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"

When you are done, save and close the file.

Reload the file to apply the changes with the following command:

source /etc/environment

Finally, verify your Java Home environment variable path on Ubuntu 22.04 with the command below:

echo $JAVA_HOME
Output
/usr/lib/jvm/java-17-openjdk-amd64

Note: Other users will need to run the source /etc/environment command or log out and log back in to apply this setting.

That’s it. You are done.

Conclusion

At this point, you have learned to Install Default Java which includes JRE and JDK, manage multiple Java installations, and set up Java Home environment variables on Ubuntu 22.04.

Hope you enjoy it. You may be like these articles too:

Fix Error User Not in Sudoer File on Ubuntu

How To Install Remmina on Ubuntu 22.04

How To Install build-essential on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!