How to install Java on Ubuntu with apt |Video upload date:  · Duration: PT4M40S  · Language: EN

Step by step guide to install Java on Ubuntu using apt with commands to verify and configure default Java runtime and JDK

Why this matters and why you should care

Java is the language that refuses to die and your machine needs a runtime and a compiler if you plan to write or run Java programs. This guide shows how to install OpenJDK on Ubuntu with the apt package manager, verify the runtime and compiler, and manage multiple JDK versions without turning your system into a confusion shrine.

Quick checklist before you begin

  • Have sudo access on the Ubuntu machine
  • Know whether you need a specific OpenJDK version or the default JDK
  • Be comfortable running a couple of terminal commands and pretending you meant to

Update apt and get the package list

First make sure apt knows what is new and exciting. Run the update command so the package index is up to date. You can also upgrade everything if you want to live dangerously.

sudo apt update
sudo apt upgrade -y

Choose a JDK and install it

For most people the default JDK is the easiest option. If you need a specific LTS release pick openjdk-11-jdk or openjdk-17-jdk depending on your project. The package manager will fetch the runtime and the compiler so you do not have to be a package whisperer.

sudo apt install default-jdk
# or for a specific version
sudo apt install openjdk-11-jdk

Make sure Java and javac match

After installation confirm both the runtime and the compiler are present. Mismatched versions are the leading cause of build confusion and dramatic error messages.

java -version
javac -version

If the versions in the output look sane then congrats. If not you may have multiple JDKs installed which leads us to the next delightful topic.

Switch between installed Java versions

Ubuntu provides the update alternatives tool to pick which java and javac are the system defaults. It is a menu driven selection so you do not need to edit symlinks while holding your breath.

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

Choose the number that corresponds to the version you want. Repeat for both java and javac when necessary to avoid build time surprises.

Removing a JDK you no longer want

If a JDK is causing trouble or you simply want less clutter remove it with apt and then clean up leftover packages.

sudo apt remove openjdk-11-jdk
sudo apt autoremove

Handy extras and troubleshooting tips

  • Look for available packages with apt search openjdk to see what versions your Ubuntu release offers
  • If you switch JDKs often consider SDKMAN for per user JVM management and less sudo drama
  • If you plan to use Oracle builds check the license before adding any third party repositories

Wrap up and final thought

Following these steps will give you a working Java runtime and compiler on Ubuntu ready for development or production deployment. It is not rocket science, but it is precise science. Keep your package list fresh, pick the JDK that matches your project, and use update alternatives when the universe offers choices. Now go write something that will print Hello World and then promptly crash in a useful way.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.