Install Oracle Java JDK 21 / 22 in Ubuntu 24.04 | 22.04 | 20.04

Last updated: May 6, 2024 — 11 Comments

This simple tutorial shows how to install the latest Oracle Java (JDK 21 or JDK 22) in Ubuntu Linux via the official binary package.

There used to be unofficial Ubuntu PPA to automate the process of installing Java JDK and setup the environment, however, discontinued. So, here’s the tutorial to manually install the Java package and set as default.

Install JDK 21/22 in Ubuntu:

Press Ctrl+Alt+T on keyboard to open a terminal window and follow the steps below one by one.

NOTE: In all commands below, you need to replace jdk-21 with jdk-22 for installing or uninstall Java JDK 22.

1. Download Java package

Firstly, go to oracle website and select download the .deb package for JDK 21 or 22 depends on which version you want to install:

It’s the 64-bit .deb package for modern Intel/AMD PC and laptops, or tarball for ARM cpu architecture, e.g., Raspberry Pi.

2. Install the .deb package

Once you downloaded the package, open terminal (Ctrl+Alt+T) and run the command below to install the deb package:

sudo apt install ~/Downloads/jdk-21_linux-x64_bin.deb

For choice, you may drag’n’drop the .deb package into terminal, instead of typing ~/Downloads/filename.deb.

For the ARM package (for Raspberry Pi, Apple M1/2, etc), run command to create ‘/usr/lib/jvm’ directory in case it does not exist:

sudo mkdir -p /usr/lib/jvm

Then, extract the tarball to that directory via (For JDK 22, replace the number in command.):

sudo tar -zxf ~/Downloads/jdk-21_linux-aarch64_bin.tar.gz -C /usr/lib/jvm/

3. Set JDK 21/22 as default (for ARM only):

NOTE 1: The .deb package now automatically set Java 21/22 as default! So, you ONLY need to follow the steps below for the ARM package.

NOTE 2: The JDK root folder-name varies depends on which package you installed. It may be ‘jdk-21’, ‘jdk-21.0.1’, ‘jdk-22’, ‘jdk-22.0.1’, and so forth. Run ls /usr/lib/jvm to tell and replace jdk-21 in commands below accordingly.

a.) Create symbolic links for the executable files:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-21/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-21/bin/javac 1
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk-21/bin/jar 1

Similarly, add links for other executable files (e.g., jarsigner, jlink, javadoc) as you need.

b.) Next, run the commands below one by one, and type number to select Java JDK 21 as default.

sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config jar

When done, verify by running command in terminal:

java -version
javac -version

4. Set JAVA_HOME (for ARM only):

As well, you only need to do this for ARM package. If installed the .deb version, just verify JAVA HOME via the bottom command.

Also, replace ‘jdk-21’ below according to ls /usr/lib/jvm command output.

Option 1.) Set JAVA_HOME for current command console, that will work until you close it:

export JAVA_HOME=/usr/lib/jvm/jdk-21
setenv JAVA_HOME=/usr/lib/jvm/jdk-21

Option 2.) To make it permanent, create and edit config file via command:

sudo gedit /etc/profile.d/

Depends on your desktop environment, you may replace gedit with mousepad for XFCE, xed for Linux Mint, pluma for MATE, kate for KDE Plasma, gnome-text-editor for Ubuntu 23.04+.

then add following lines:

export J2SDKDIR=/usr/lib/jvm/jdk-21
export J2REDIR=/usr/lib/jvm/jdk-21
export PATH=$PATH:/usr/lib/jvm/jdk-21/bin:/usr/lib/jvm/jdk-21/db/bin
export JAVA_HOME=/usr/lib/jvm/jdk-21
export DERBY_HOME=/usr/lib/jvm/jdk-21/db

And create anther one for C shell:

sudo gedit /etc/profile.d/jdk.csh

add following lines and save it:

setenv J2SDKDIR /usr/lib/jvm/jdk-21
setenv J2REDIR /usr/lib/jvm/jdk-21
setenv PATH ${PATH}:/usr/lib/jvm/jdk-21/bin:/usr/lib/jvm/jdk-21/db/bin
setenv JAVA_HOME /usr/lib/jvm/jdk-21
setenv DERBY_HOME /usr/lib/jvm/jdk-21/db

Finally, change the permissions via command, and it should take place next time you log in.

sudo chmod +x /etc/profile.d/jdk.csh /etc/profile.d/

To verify JAVA HOME, run the command below in terminal at next login:

java -XshowSettings:properties -version

Uninstall Java JDK 21

To uninstall the Java package, open terminal (Ctrl+Alt+T) and run command:

sudo apt remove --autoremove jdk-21

For the ARM version, simply remove all the installed files, by running the commands below one by one:

  • Remove the alternative links:
    sudo update-alternatives --remove java /usr/lib/jvm/jdk-21/bin/java
    sudo update-alternatives --remove javac /usr/lib/jvm/jdk-21/bin/javac
    sudo update-alternatives --remove jar /usr/lib/jvm/jdk-21/bin/jar
  • Remove JDK installation files:
    sudo rm -R /usr/lib/jvm/jdk-21
  • And remove JAVA HOME config files:
    sudo rm /etc/profile.d/
    sudo rm /etc/profile.d/jdk.csh


I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via [email protected] Buy me a coffee:

11 responses to Install Oracle Java JDK 21 / 22 in Ubuntu 24.04 | 22.04 | 20.04

  1. Always good instructions for java installation.
    Thx Very much and CU next

  2. gracias!
    realmente util

  3. immeasurable thanks!!

  4. Thank you very much!!

  5. How to upgrade java 19.0.1 to 19.0.2?

  6. This is absolute garbage. This “tutorial” is not that well made, and it ruined my java installation.

  7. well, easly the best java installation tutorial over the web. kind of rare these days. Congrats and thank you !

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>