Java is a high-level, object-oriented programming language initially developed by Sun Microsystems and now owned by Oracle. It can run on any platform that supports Java without recompilation, a feature known as “write once, run anywhere” (WORA). That’s why Java is used to develop web apps, desktop apps, games, and mobile apps. It can also run on the Tomcat application server, which we covered in this article. LTS stands for Long-Term Support. This post will teach you how to install Java LTS on Ubuntu 24.04.
At the time of writing, Java 21 is the latest LTS version. You can always check the latest Oracle LTS versions of Java.
Prerequisites
• A server running Ubuntu 24.04 or any Linux OS
• User privileges: root or non-root user with sudo privileges
1. Update the system
Before we start to install anything on the server, we need to update the system packages to the latest versions available:
sudo apt update -y && sudo apt upgrade -y
2. Install Java
You can use apt to install Java 21 from the default repository on Ubuntu 24.04 using the following command:
# apt install openjdk-21-jdk -y
Once installed, you can now check the Java version installed with:
# java -version
openjdk version "21.0.4" 2024-07-16
OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu224.04)
OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)
3. Set up the Default Java version
If you already have two or more Java versions installed, you can configure the Default Java version you want to use using the command.
# update-alternatives --config java
There are two choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
- 0 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 auto mode
1 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press to keep the current choice[*], or type the selection number.
4. The difference between JRE and JDK
You may notice that some packages are JDK or OpenJDK, and some are JRE. Let’s find out which package you need.
JDK – Developers use the Java Development Kit, which includes a compiler, debugger, archiver, and other tools for Java Development.
JRE – Java Runtime Environment is used to run Java programs without compiling or debugging the code.
JDK and JRE include JVM – Java Virtual Machine, a runtime component or interpreter. Java code is first compiled into bytecode by the Java compiler (Javac) rather than into platform-specific machine code. The JVM then interprets or compiles this bytecode into machine code that the host machine can understand. Each platform (Windows, Mac, Linux, etc.) has its own JVM implementation that converts bytecode into native instructions for that particular OS.
5. Installing Oracle JDK 17 on Ubuntu 24.04
Open the browser and search for Download JDK 17 or download it from the Oracle website.
You can download it using the following command:
# wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
You have successfully downloaded the Java JDK 21 file, and now you will extract it to the /opt directory using the tar command:
# tar xvzf jdk-17_linux-x64_bin.tar.gz -C /opt
Now, you need to set the environment variables as below:
export JAVA_HOME=/opt/jdk-21.0.5
export PATH=$PATH:$JAVA_HOME/bin
Verify the JAVA_HOME environment variable with the following command:
# echo $JAVA_HOME
You should get the following output:
# /opt/jdk-17.0.2
Java 17 is now installed. To confirm, use the following commands:
java -version
If installed correctly, you should see the following output:
# java -version
openjdk version "21.0.4" 2024-07-16
OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu224.04)
OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)
Conclusion
You have now learned how to install Java LTS on your Ubuntu 24.04 server.
If you liked this post, please share it with your friends or leave a comment below.