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
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
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
Explanation: Installs OpenJDK 21, the JDK version Maven and DSpace's build process require.
java -version
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
Explanation: Removes any Maven version installed via apt, so the manually-installed 3.9.x release is the only one on your system.
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.
cd /opt
Explanation: /opt is the standard Linux location for manually-installed, self-contained software packages.
sudo wget https://dlcdn.apache.org/maven/maven-3/3.9.16/binaries/apache-maven-3.9.16-bin.tar.gz
Explanation: Downloads the official Maven 3.9.16 release archive directly from the Apache Software Foundation.
sudo tar xzf apache-maven-3.9.16-bin.tar.gz
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
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
Explanation: Confirms /opt/maven correctly points to the apache-maven-3.9.16 directory.
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
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.
sudo nano /etc/profile.d/maven.sh
Explanation: Opens a new file that will set Maven's environment variables for every user on the system.
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
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.
sudo chmod +x /etc/profile.d/maven.sh
source /etc/profile.d/maven.sh
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.
/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
mvn -version
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:
ant -version
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
aptMaven installed alongside a manual install — creates aPATHconflict that's confusing to debug later. - Symlinking
/opt/mavento a version folder that doesn't exist — fails silently untilmvn -versionis actually run. - Editing
PATHonly in a personal shell config — works for you, but breaks for every other user or service account on the box. - Forgetting to
sourcethe new profile script — the install is technically correct, but the current terminal session doesn't know about it yet.
Comments
Post a Comment