How to Install Apache Maven on Ubuntu 26.04, 24.04 and 22.04

Install Apache Maven on Ubuntu 26.04, 24.04 and 22.04. Includes version comparison, environment setup, testing, and removal steps.

Last updatedAuthorJoshua JamesRead time9 minGuide typeUbuntu

Java builds become easier to repeat when dependency resolution, testing, and packaging all run through the same tool. To install Apache Maven on Ubuntu, use the Ubuntu package for a maintained mvn command, or install the current Apache Maven 3.x binary when a project needs a newer upstream release than your LTS repository provides.

Ubuntu 26.04 (Resolute Raccoon), 24.04 (Noble Numbat), and 22.04 (Jammy Jellyfish) all provide Maven through the Universe repository component. The APT method also works in Ubuntu on WSL because it uses the same package source, while the manual Apache download path gives you the latest recommended Maven 3.x release with manual update responsibility.

Install Apache Maven on Ubuntu

Choose one install path before you start. The APT method is simpler and follows Ubuntu updates; the Apache binary tarball is useful when you need the current upstream Maven 3.x branch across older Ubuntu releases.

MethodSourceVersion behaviorUpdatesBest fit
APT packageUbuntu Universe repositoryUbuntu-packaged Maven version for each LTS releaseThrough normal APT updatesMost users, CI hosts, and systems where distro integration matters more than newest upstream point releases
Apache binary tarballOfficial Apache Maven download pageCurrent recommended Maven 3.x releaseRerun the helper script or repeat the manual checksum-verified downloadProjects that need the current Maven 3.x branch instead of the package version in their Ubuntu release

Pick one method unless you deliberately want both. The manual install prepends /opt/maven/bin to your PATH, so new shells use that Maven binary before Ubuntu’s /usr/bin/mvn. If you keep both, confirm the active binary with command -v mvn.

The current Ubuntu package matrix is:

Ubuntu releaseAPT Maven packageDefault JDK installed with default-jdkWhen the manual method helps
Ubuntu 26.04 (Resolute Raccoon)3.9.12-1OpenJDK 25Use only when Apache’s recommended Maven 3.x release has moved ahead of Ubuntu’s package.
Ubuntu 24.04 (Noble Numbat)3.8.7-2OpenJDK 21Use when a build, plugin, or team standard expects Maven 3.9.x.
Ubuntu 22.04 (Jammy Jellyfish)3.6.3-5OpenJDK 11Use when older Maven 3.6.x behavior is not enough for your project.

Apache also publishes Maven 4 preview builds on the same download page. Keep the normal install flow on Maven 3.x unless you are intentionally testing Maven 4 compatibility, because Apache labels the 4.x line as a preview that is not safe for production use.

Update Ubuntu Package Lists

Refresh APT before installing so Ubuntu uses the current package metadata for Maven and Java:

sudo apt update

These commands use sudo for package installation and system paths. If your account cannot run sudo yet, follow the guide to add a user to sudoers on Ubuntu, or run the commands from an administrative account.

If you also want to apply pending system updates before installing development tools, run the upgrade step now:

sudo apt upgrade

Install Maven with APT

Install Maven and Ubuntu’s default Java Development Kit. The maven package provides the mvn command, while default-jdk adds the Java compiler that Maven needs for normal project builds.

sudo apt install -y maven default-jdk

Older examples that use sudo apt-get install maven target the same Ubuntu package. The commands below use apt because it is the standard interactive package command on current Ubuntu releases.

Verify the installed Maven command and Java compiler:

mvn --batch-mode --no-transfer-progress -version
javac -version

Relevant output on Ubuntu 26.04 includes:

Apache Maven 3.9.12
Maven home: /usr/share/maven
Java version: 25.0.3-ea, vendor: Ubuntu, runtime: /usr/lib/jvm/java-25-openjdk-amd64
javac 25.0.3-ea

Ubuntu 24.04 reports Maven 3.8.7 with Java 21, and Ubuntu 22.04 reports Maven 3.6.3 with Java 11. Those older APT versions are expected; use the Apache binary method only when you need the current Maven 3.x release instead.

Install the Latest Apache Maven Binary

The Apache binary method downloads the recommended Maven 3.x binary from Apache, verifies the SHA512 checksum, extracts it under /opt, and exposes it through /opt/maven/bin/mvn. Use the Bash installer script when you want the safest repeatable path, or follow the manual commands afterward when you want to inspect each stage yourself.

Install Maven with a Bash Script

The script is the most practical upstream option when you want the same Maven install on multiple Ubuntu systems. It installs the required JDK and curl packages, resolves the current Maven 3 release from Apache, verifies the SHA512 checksum, installs under /opt, and writes the same /etc/profile.d/maven.sh file used by the manual steps.

Create the installer under /usr/local/bin:

sudo tee /usr/local/bin/install-apache-maven >/dev/null <<'EOF'
#!/usr/bin/env bash
set -euo pipefail

MAVEN_DOWNLOAD_PAGE="https://maven.apache.org/download.cgi"
MAVEN_ROOT="/opt"
MAVEN_LINK="${MAVEN_ROOT}/maven"
PROFILE_FILE="/etc/profile.d/maven.sh"

if [ "${EUID}" -ne 0 ]; then
  echo "Run this script with sudo: sudo install-apache-maven" >&2
  exit 1
fi

if ! command -v apt-get >/dev/null 2>&1; then
  echo "This installer expects an Ubuntu system with apt-get." >&2
  exit 1
fi

echo "Installing prerequisites..."
apt-get update
apt-get install -y default-jdk ca-certificates curl

for cmd in curl grep sed sha512sum tar java javac; do
  if ! command -v "$cmd" >/dev/null 2>&1; then
    echo "Required command is missing after prerequisite installation: $cmd" >&2
    exit 1
  fi
done

work_dir=$(mktemp -d)
cleanup() {
  rm -rf "$work_dir"
}
trap cleanup EXIT

cd "$work_dir"

download_page=$(curl -fsSL "$MAVEN_DOWNLOAD_PAGE")
maven_version=$(printf '%s\n' "$download_page" | grep -Eo 'apache-maven-3\.[0-9]+\.[0-9]+-bin\.tar\.gz' | head -n 1 | sed -E 's/apache-maven-([0-9]+\.[0-9]+\.[0-9]+)-bin\.tar\.gz/\1/')
if [ -z "$maven_version" ]; then
  echo "Could not detect the current Maven 3 release from $MAVEN_DOWNLOAD_PAGE." >&2
  exit 1
fi

maven_major=${maven_version%%.*}
archive="apache-maven-${maven_version}-bin.tar.gz"
archive_url="https://dlcdn.apache.org/maven/maven-${maven_major}/${maven_version}/binaries/${archive}"
checksum_url="https://downloads.apache.org/maven/maven-${maven_major}/${maven_version}/binaries/${archive}.sha512"

echo "Downloading Apache Maven ${maven_version}..."
curl -fL "$archive_url" -o "$archive"
curl -fL "$checksum_url" -o "${archive}.sha512"
printf '%s  %s\n' "$(cat "${archive}.sha512")" "$archive" | sha512sum -c -

if [ -e "$MAVEN_LINK" ] && [ ! -L "$MAVEN_LINK" ]; then
  echo "$MAVEN_LINK exists and is not a symlink. Move it before running this installer." >&2
  exit 1
fi

install -d -m 0755 "$MAVEN_ROOT"
tar xzf "$archive" -C "$MAVEN_ROOT"
ln -sfn "${MAVEN_ROOT}/apache-maven-${maven_version}" "$MAVEN_LINK"
# shellcheck disable=SC2016
printf '%s\n' 'export M2_HOME=/opt/maven' 'export PATH="${M2_HOME}/bin:${PATH}"' > "$PROFILE_FILE"
chmod 0644 "$PROFILE_FILE"

echo "Apache Maven ${maven_version} installed at $MAVEN_LINK."
"$MAVEN_LINK/bin/mvn" --batch-mode --no-transfer-progress -version
echo 'Open a new terminal or run: source /etc/profile.d/maven.sh'
EOF
sudo chmod 0755 /usr/local/bin/install-apache-maven

Run the installer, then load the profile script in your current terminal:

sudo install-apache-maven
source /etc/profile.d/maven.sh
command -v mvn

Relevant output follows this shape. The exact version changes when Apache publishes a newer Maven 3 release:

apache-maven-3.x.x-bin.tar.gz: OK
Apache Maven 3.x.x installed at /opt/maven.
/opt/maven/bin/mvn

The installer stops if /opt/maven already exists as a real directory instead of a symlink, which prevents accidental overwrites. Rerun the same helper later to refresh the current Maven 3 release through the same checksum-verified path.

Install Java and Download Tools Manually

The manual commands below perform the same work as the script. Use them when you want to inspect the version detection, download, checksum, extraction, and profile setup as separate stages.

Install the JDK, CA certificates, and curl. Minimal Ubuntu and WSL installations often lack curl even when Maven from APT is already installed.

sudo apt update
sudo apt install -y default-jdk ca-certificates curl

Confirm Java is available before downloading Maven:

java -version
javac -version

Relevant output on Ubuntu 26.04 includes:

openjdk version "25.0.3-ea" 2026-04-21
javac 25.0.3-ea

Ubuntu 24.04 shows OpenJDK 21, while Ubuntu 22.04 shows OpenJDK 11.

Detect the Current Maven 3 Release

Read Apache’s Maven download page and extract the current recommended Maven 3.x binary version. The pattern is limited to Maven 3 so the workflow does not drift to Maven 4 when Apache’s preview branch changes state.

cd /tmp
MAVEN_VERSION=$(curl -fsSL https://maven.apache.org/download.cgi | grep -Eo 'apache-maven-3\.[0-9]+\.[0-9]+-bin\.tar\.gz' | head -n 1 | sed -E 's/apache-maven-([0-9]+\.[0-9]+\.[0-9]+)-bin\.tar\.gz/\1/')
test -n "$MAVEN_VERSION" || { echo "Could not detect the current Maven 3 release"; exit 1; }
MAVEN_MAJOR=${MAVEN_VERSION%%.*}
printf 'Latest Maven 3 release: %s\n' "$MAVEN_VERSION"

Your version number may differ when Apache publishes a newer Maven 3 release. The output follows this shape:

Latest Maven 3 release: 3.x.x

The pipeline uses grep to match the Maven 3 tarball name and sed to keep only the version string.

Download and Verify the Maven Archive

Download the binary tarball and its SHA512 checksum from Apache. Apache also publishes a binary zip archive, but the tar.gz file fits the Linux extraction commands shown here. The checksum command formats Apache’s standalone hash file into the input shape expected by sha512sum -c.

The -fL flags make curl fail on HTTP errors and follow redirects; the curl command guide covers those options if you want more background.

curl -fL "https://dlcdn.apache.org/maven/maven-${MAVEN_MAJOR}/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz" -o "apache-maven-${MAVEN_VERSION}-bin.tar.gz"
curl -fL "https://downloads.apache.org/maven/maven-${MAVEN_MAJOR}/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512" -o "apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512"
printf '%s  %s\n' "$(cat "apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512")" "apache-maven-${MAVEN_VERSION}-bin.tar.gz" | sha512sum -c -

The filename follows the detected version. The checksum check should return:

apache-maven-3.x.x-bin.tar.gz: OK

Extract Maven and Link the Current Version

Create /opt if it is missing, extract the archive, and point /opt/maven at the extracted version. The install -d command creates the directory with readable and executable permissions for all users, which matters on minimal systems where /opt may not exist yet.

sudo install -d -m 0755 /opt
sudo tar xzf "apache-maven-${MAVEN_VERSION}-bin.tar.gz" -C /opt/
sudo ln -sfn "/opt/apache-maven-${MAVEN_VERSION}" /opt/maven

The symlink lets future updates replace the active Maven version without changing every profile or script that refers to /opt/maven.

Configure Maven Environment Variables

Add Maven to the system profile. The tee command writes the file with root privileges, while the quoted variable text remains literal until a new shell loads it.

printf '%s\n' 'export M2_HOME=/opt/maven' 'export PATH="${M2_HOME}/bin:${PATH}"' | sudo tee /etc/profile.d/maven.sh >/dev/null

Reload the profile script in the current terminal, or open a new terminal after the file is created:

source /etc/profile.d/maven.sh

Verify the Manual Maven Installation

Confirm your shell is using the manual Maven binary and then check the version:

command -v mvn
mvn --batch-mode --no-transfer-progress -version

Relevant output follows this shape. The Java version differs by Ubuntu release:

/opt/maven/bin/mvn
Apache Maven 3.x.x
Maven home: /opt/maven
Java version: 25.x.x, vendor: Ubuntu, runtime: /usr/lib/jvm/java-25-openjdk-amd64

Ubuntu 24.04 and 22.04 show the same Maven home path but different Java versions because their default JDK packages differ.

Create a Test Maven Project

A simple build test proves more than a version check. It confirms Maven can reach Central, create a project, compile Java, run tests, and package a JAR.

Generate a Maven Project

Create a basic Java application from Maven’s quickstart archetype:

mvn --batch-mode --no-transfer-progress archetype:generate -DgroupId=com.example.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Relevant output includes:

[INFO] BUILD SUCCESS

Build the Maven Project

Move into the generated project and build it:

cd my-app
mvn --batch-mode --no-transfer-progress package

Relevant output includes:

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

Newer JDKs may print warnings about the archetype’s Java 8 source and target settings. The test still passes, and real projects should set their compiler release level in pom.xml to match their own Java baseline.

Run the Maven Test Application

Run the compiled sample application:

java -cp target/my-app-1.0-SNAPSHOT.jar com.example.app.App

Expected output:

Hello World!

Troubleshoot Maven on Ubuntu

Fix Unable to Locate Package Maven

If APT cannot find maven, your Ubuntu sources probably do not include Universe:

E: Unable to locate package maven

Maven is in Universe on Ubuntu 26.04, 24.04, and 22.04. Enable that repository component, refresh APT, and rerun the install command:

sudo apt install -y software-properties-common
sudo add-apt-repository --yes universe
sudo apt update

For a fuller repository-component walkthrough, use the guide to enable Universe and Multiverse on Ubuntu. Only Universe is required for Maven.

Fix JAVA_HOME Not Set or Incorrect

Maven can fail when JAVA_HOME points at the wrong JDK or is missing entirely:

The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program.

Check the current variable and the active Java path:

printf 'JAVA_HOME=%s\n' "$JAVA_HOME"
readlink -f /usr/bin/java
readlink -f /usr/lib/jvm/default-java

If JAVA_HOME is empty or points somewhere stale, set it to Ubuntu’s default JDK symlink and reload the file:

printf '%s\n' 'export JAVA_HOME=/usr/lib/jvm/default-java' | sudo tee /etc/profile.d/java-home.sh >/dev/null
source /etc/profile.d/java-home.sh

Then verify Maven again:

mvn --batch-mode --no-transfer-progress -version

If you manage several JDKs, the separate guide to set the Java environment path on Ubuntu covers persistent JAVA_HOME choices in more detail.

Fix Maven Command Not Found After Manual Install

If a manual install completed but your shell still cannot find mvn, the profile script has not loaded in that terminal:

mvn: command not found

Check whether the manual binary exists and whether your PATH can see it:

command -v mvn || echo "mvn not found in PATH"
if [ -x /opt/maven/bin/mvn ]; then
  echo "/opt/maven/bin/mvn exists"
fi

If the binary exists, reload the profile script or open a new terminal:

source /etc/profile.d/maven.sh
command -v mvn
mvn --batch-mode --no-transfer-progress -version

Update Apache Maven

Update APT-Installed Maven

APT-installed Maven updates through Ubuntu’s package manager. Use the single-package upgrade form when you only want to refresh Maven:

sudo apt update
sudo apt install --only-upgrade maven

If APT reports that Maven is already the newest version, that means your Ubuntu release is already on its packaged Maven branch. Switch to the Apache binary method only if you need a newer upstream Maven release.

Update Manually Installed Maven

If you created the helper script, rerun it to repeat the Apache download, checksum verification, extraction, symlink refresh, and version check:

sudo install-apache-maven

If you prefer the manual path, run the same download and refresh sequence in one terminal so the version variables stay available:

cd /tmp
MAVEN_VERSION=$(curl -fsSL https://maven.apache.org/download.cgi | grep -Eo 'apache-maven-3\.[0-9]+\.[0-9]+-bin\.tar\.gz' | head -n 1 | sed -E 's/apache-maven-([0-9]+\.[0-9]+\.[0-9]+)-bin\.tar\.gz/\1/')
test -n "$MAVEN_VERSION" || { echo "Could not detect the current Maven 3 release"; exit 1; }
MAVEN_MAJOR=${MAVEN_VERSION%%.*}
curl -fL "https://dlcdn.apache.org/maven/maven-${MAVEN_MAJOR}/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz" -o "apache-maven-${MAVEN_VERSION}-bin.tar.gz"
curl -fL "https://downloads.apache.org/maven/maven-${MAVEN_MAJOR}/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512" -o "apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512"
printf '%s  %s\n' "$(cat "apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512")" "apache-maven-${MAVEN_VERSION}-bin.tar.gz" | sha512sum -c -
sudo install -d -m 0755 /opt
sudo tar xzf "apache-maven-${MAVEN_VERSION}-bin.tar.gz" -C /opt/
sudo ln -sfn "/opt/apache-maven-${MAVEN_VERSION}" /opt/maven
source /etc/profile.d/maven.sh
mvn --batch-mode --no-transfer-progress -version

Review Maven release notes and test your project build before replacing Maven on production CI hosts. Do not automate Maven upgrades with cron unless your build pipeline already validates the new release before rollout.

Remove Apache Maven

Remove APT-Installed Maven

Remove the Ubuntu package with APT:

sudo apt remove maven

This leaves default-jdk installed, which is usually the safer choice on development systems. If you installed Java only for Maven and no other Java tools need it, remove the metapackage separately:

sudo apt remove default-jdk

If APT lists Java libraries as automatically installed and no longer required, review the package list before accepting an autoremove. Shared Java packages may still matter to other tools on a development workstation.

sudo apt autoremove

Confirm the package state instead of relying only on command -v, because a manual Maven install may still provide mvn:

apt-cache policy maven

Expected package state after removal:

maven:
  Installed: (none)

Remove Manually Installed Maven

The next commands permanently delete the Maven files installed under /opt. Check the paths before running them, especially if you keep custom files under /opt.

Remove the extracted Maven directories, the current-version symlink, the Maven profile script, and the optional helper script if you created it:

sudo rm -rf /opt/apache-maven-*
sudo rm -f /opt/maven
sudo rm -f /etc/profile.d/maven.sh
sudo rm -f /usr/local/bin/install-apache-maven

If you created a separate JAVA_HOME profile script only for this Maven setup, remove it too:

sudo rm -f /etc/profile.d/java-home.sh

Open a new terminal or clear the command cache, then check what mvn resolves to:

hash -r
command -v mvn || echo "mvn not found in PATH"

If the APT package is still installed, this check may show /usr/bin/mvn. That means the manual copy was removed and Ubuntu’s packaged Maven is still available.

The manual cleanup leaves Java installed. Remove default-jdk only if you installed it just for Maven and no other Java tools need it.

Remove Maven User Data

The next command permanently deletes your Maven configuration, cached dependencies, and local repository under ~/.m2. Skip it if you want to keep downloaded JAR files or custom settings for future projects.

rm -rf ~/.m2

Conclusion

Apache Maven is ready on Ubuntu with the mvn command, a working JDK, and a verified sample build. Keep the APT package when Ubuntu’s Maven branch fits your project, or use the Apache binary path when you need the current Maven 3.x release. For typical Java project work, install Git on Ubuntu next so Maven builds and source control sit in the same workflow.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show our tutorials more often in Top Stories and mark them as preferred in AI Mode and AI Overviews when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<a href="https://example.com">link</a> link
<blockquote>quote</blockquote> quote block

Add to the discussion

Questions, fixes, command output, and version notes help keep this guide current.

Verify before posting: