How to Install Apache Maven 3.9.x on Ubuntu 24.04

How to Install Apache Maven 3.9.x on Ubuntu 24.04

Ubuntu 24.04's own package repository doesn't ship the latest Apache Maven release — and projects like DSpace explicitly require Maven 3.9.x or above to build correctly. This guide walks through installing a current Maven version manually, straight from the Apache Software Foundation, alongside OpenJDK 21.

Step 1: Update the System

i. Confirm your Ubuntu version

lsb_release -a
Purpose: Confirm OS version
Explanation: Displays your Ubuntu release details. Worth checking before you start, especially on a server that's changed hands a few times.

ii. Update and Upgrade System Packages

sudo apt update && sudo apt upgrade -y
Purpose: Update and upgrade system packages
Explanation: This command updates the package lists and upgrades all installed packages to their latest versions. The -y flag automatically confirms the upgrade process.

Step 2: Install OpenJDK 21

Maven runs on Java, and modern build toolchains for projects like DSpace expect JDK 21 specifically.

sudo apt install openjdk-21-jdk -y
Purpose: Install Java Development Kit
Explanation: Installs OpenJDK 21, the JDK version Maven and DSpace's build process require.
# Should show OpenJDK 21
java -version
Purpose: Verify Java installation
Explanation: Displays the installed Java version, confirming OpenJDK 21 installed correctly.

Step 3: Remove the Distro's Maven Package

It's tempting to just run apt install maven and move on — don't. Ubuntu 24.04 ships an older Maven build, and installing a newer version manually alongside it creates two competing mvn binaries on your PATH. Clear it out first.

sudo apt remove maven -y
Purpose: Remove the outdated package-manager Maven
Explanation: Removes any Maven version installed via apt, so the manually-installed 3.9.x release is the only one on your system.
Note: This only removes Maven. Apache Ant (also required for DSpace) can stay installed via apt — Ubuntu 24.04's packaged Ant version comfortably satisfies DSpace's 1.10.x+ requirement, so there's no need to install it manually.

Step 4: Download and Extract Maven

/opt is the conventional home for manually-installed software on Linux — it keeps things separate from whatever your package manager controls.

## Move into /opt
cd /opt
Purpose: Navigate to /opt
Explanation: /opt is the standard Linux location for manually-installed, self-contained software packages.
## Download the official Maven 3.9.16 binary from the offical site 
sudo wget https://dlcdn.apache.org/maven/maven-3/3.9.16/binaries/apache-maven-3.9.16-bin.tar.gz
Purpose: Download Apache Maven
Explanation: Downloads the official Maven 3.9.16 release archive directly from the Apache Software Foundation.
## Extract the archive
sudo tar xzf apache-maven-3.9.16-bin.tar.gz
Purpose: Extract Maven package
Explanation: Unpacks the downloaded archive into a folder named apache-maven-3.9.16 inside /opt.

Step 5: Create a Version-Agnostic Symlink

The goal is a stable path — /opt/maven — that always points at whichever version is installed, so future upgrades don't mean rewriting every config file that references it.

sudo ln -s /opt/apache-maven-3.9.16 /opt/maven
Purpose: Create a stable symlink
Explanation: Points /opt/maven at the actual versioned install folder, giving you one consistent path to reference in configs and scripts.
ls -l /opt/maven
Purpose: Verify the symlink
Explanation: Confirms /opt/maven correctly points to the apache-maven-3.9.16 directory.
Common mistake: It's easy to type the wrong version number here out of habit — pointing the symlink at a folder that doesn't actually exist yet. The ln -s command will "succeed" even though the target is missing, and you won't find out until mvn -version fails later. If that happens, just remove and recreate the symlink:
sudo rm /opt/maven
sudo ln -s /opt/apache-maven-3.9.16 /opt/maven
Purpose: Fix an incorrect symlink
Explanation: Removes the broken symlink and recreates it pointing at the correct, existing version folder.

Step 6: Set Environment Variables System-Wide

Rather than exporting PATH changes in a personal ~/.bashrc (which only applies to your own login shell), drop a script into /etc/profile.d/. It applies to every user and every future shell session on the machine.

## Create the profile script
sudo nano /etc/profile.d/maven.sh
Purpose: Create a system-wide environment script
Explanation: Opens a new file that will set Maven's environment variables for every user on the system.
Paste the following three lines, then save and exit (Ctrl+O, Enter, Ctrl+X in nano):
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
Purpose: Define Maven environment variables
Explanation: Sets M2_HOME and MAVEN_HOME to the Maven install path, and adds Maven's bin directory to the system PATH so the mvn command is available everywhere.
## Make the script executable and load it into the current session
sudo chmod +x /etc/profile.d/maven.sh
source /etc/profile.d/maven.sh
Purpose: Apply the environment changes
Explanation: Makes the script executable, then loads it into your current terminal session so you don't need to log out and back in to use mvn right away.
Note: Files in /etc/profile.d/ are picked up automatically on new login shells. Running source manually is what makes the variables available in your current terminal without needing a fresh login.

Step 7: Verify the Installation

# Should show Apache Maven 3.9.16
mvn -version
Purpose: Verify Maven installation
Explanation: Displays the installed Maven version along with the Java version and OS it's running on, confirming everything is wired up correctly.

While you're here, it's worth double-checking Ant too, since DSpace needs both build tools:

# Should show Apache Ant 1.10.x
ant -version
Purpose: Verify Ant installation
Explanation: Confirms Apache Ant is installed and meets DSpace's minimum version requirement of 1.10.x or later.

Where People Usually Go Wrong

  • Leaving the distro's apt Maven installed alongside a manual install — creates a PATH conflict that's confusing to debug later.
  • Symlinking /opt/maven to a version folder that doesn't exist — fails silently until mvn -version is actually run.
  • Editing PATH only in a personal shell config — works for you, but breaks for every other user or service account on the box.
  • Forgetting to source the new profile script — the install is technically correct, but the current terminal session doesn't know about it yet.
Building this for DSpace specifically? Maven is just one of several backend requirements — Java 21, Ant, PostgreSQL, and Solr 9.x round out the list. See the full DSpace Backend Installation Guide for the complete, ordered walkthrough this fits into.

Comments