Explain Codes LogoExplain Codes Logo

How to install the JDK on Ubuntu Linux

java
jdk
ubuntu
environment-setup
Alex KataevbyAlex Kataev·Aug 11, 2024
TLDR

First, let's quench your thirst for a quick installation of OpenJDK 11:

# Remember folks! Penguins love fish but your system loves updates more! sudo apt-get update # Give it what it loves! sudo apt install openjdk-11-jdk

Ensure JDK is enjoying its new cozy corner:

# Did the magic happen? Let's summon the confirmation oracle! javac -version

Understanding JDK: OpenJDK vs Oracle JDK

JDK installation involves two popular choices: OpenJDK and Oracle JDK. OpenJDK is freely accessible, while Oracle JDK requires a license beyond development and testing purposes since early 2019. OpenJDK is generally preferred due to its broader platform support, licensing freedom, and active development.

Environmental setup: Bathing your system with JAVA_HOME and PATH

Post installation involves setting up JAVA_HOME and PATH. Our loyal servant, the system, then knows where to find java and other executables.

Setting up JAVA_HOME and PATH

Let's perform this ritual together:

# "Oh, mighty Shell, remember JAVA_HOME." echo "export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))" >> ~/.profile # "Now share this wisdom with all who dwell in PATH." echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> ~/.profile # Implement the new decrees of the land. source ~/.profile

Considering alternatives and advanced steps

Oracle JDK installation

If your heart longs for Oracle JDK, directly download it from Oracle and manually extract it. Beware, this approach requires manual updates and consideration of licensing terms.

# Unleash the Oracle from its tar-zipped crystal ball tar -xvf jdk-8u201-linux-x64.tar.gz # Make Oracle feel at home sudo mv jdk1.8.0_201 /usr/local/

Other Quests: OpenJDK alternatives

For OpenJDK 8, PPAs (Personal Package Archives) offer an alternative automatic updates route.

# Dear PPA guide, lead us to the promised land of Java 8 sudo add-apt-repository ppa:openjdk-r/ppa # Let's tidy up and enlist what's new sudo apt-get update # And finally, claim the treasure sudo apt-get install openjdk-8-jdk