Setting Up Java 21 and Maven on Linux

A Spring Boot project has been waiting in the wings for months. Day to day work is Java, but this Linux machine had never been set up properly for it. The plan was simple. Install Oracle JDK 21 to match the work environment and install a current Apache Maven without depending on the older versions in the default package repositories. Here is the exact sequence that worked, with the commands and the checks that confirm the setup.

Install Oracle JDK 21

Oracle provides a Debian package that installs cleanly. Download the latest JDK 21 .deb and install it directly.

1
2
3
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb
sudo apt install ./jdk-21_linux-x64_bin.deb
java --version

On success the version output looks like this:

java 21.0.4 2024-07-16 LTS
Java(TM) SE Runtime Environment (build 21.0.4+8-LTS-274)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.4+8-LTS-274, mixed mode, sharing)

Nothing else is required for Java at this point. The package places binaries in the expected locations, so shells and tools find java and javac without manual path surgery.

Install Apache Maven 3.9.8

The version available from default repositories was older than needed, so Maven was installed from the official binary distribution. First, remove any previous Maven that might be on the system. Then download, extract, and place it under /opt, keeping a stable symlink for easier upgrades.

1
2
3
4
5
sudo apt-get remove maven
wget https://downloads.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gz
tar -xvzf apache-maven-3.9.8-bin.tar.gz
sudo mv apache-maven-3.9.8 /opt/
sudo ln -sfn /opt/apache-maven-3.9.8 /opt/maven

With Maven in /opt/maven, add it to the shell PATH by updating .bashrc:

1
2
3
echo 'export M2_HOME=/opt/maven' >> ~/.bashrc
echo 'export PATH=$M2_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

The M2_HOME variable points tools at the installation, and the PATH addition exposes mvn for the current user. Sourcing the profile applies the change immediately.

Verify the setup

Run a quick version check to confirm that Maven is available and that it is using the newly installed JDK 21.

1
mvn -v

The output should report Maven 3.9.8 and Java 21, similar to:

Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256)
Maven home: /opt/maven
Java version: 21.0.4, vendor: Oracle Corporation, runtime: /usr/lib/jvm/jdk-21.0.4-oracle-x64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.15.153.1-microsoft-standard-wsl2", arch: "amd64", family: "unix"

With both tools in place, the machine is ready for a modern Spring project without relying on stale packages.

Notes that kept things simple

  • Match work and home. Using Oracle JDK 21 here mirrors the environment used at work, which reduces surprises when switching contexts.
  • Keep Maven upgrades easy. The /opt/maven symlink points to the real versioned folder. When a new Maven release arrives, unpack it next to the old one and update the symlink. Shell configuration stays the same.
  • Avoid global side effects. Installing Maven this way does not replace system packages or depend on distro decisions. It is contained and reversible.
  • Confirm before coding. The java --version and mvn -v checks catch path issues early. Seeing the right versions in the output is the green light to start writing code.

With Java 21 and Maven 3.9.8 confirmed, the Spring Boot work can finally begin. The environment is current, predictable, and not tied to older repository snapshots. Now to find that tutorial and start building.

Thanks for reading.


↤ Previous Post
Next Post ↦