How to Install Maven on Ubuntu

Install Apache Maven on Ubuntu with openjdk-17-jdk or default-jdk, then sudo apt install maven (3.9.9 on 25.04): set JAVA_HOME, verify with mvn -version, install 3.9.16 from the official tarball when apt lags upstream, and build a hello-world project.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

Install Maven on Ubuntu banner with Java build tool icon and Ubuntu orange accent

Apache Maven is the standard build and project-management tool for Java. It reads pom.xml files, resolves dependencies from Maven Central, and runs lifecycle phases such as compile, test, and package. On Ubuntu you install a JDK first, then Maven from apt (simplest) or from the official binary archive when you need a newer release than your LTS repository ships.

This guide covers how to install Maven on Ubuntu, set JAVA_HOME, verify with mvn -version, install upstream 3.9.16 under /opt, and run a minimal hello-world build. Steps below were tested on Ubuntu 25.04.

Tested on: Ubuntu 25.04 (Plucky Puffin); kernel 6.14.0-37-generic; amd64.

NOTE
Pick one Maven install (apt or /opt/apache-maven-*). Mixing both on PATH makes mvn -version report the wrong Maven home. Remove apt Maven before switching to a manual tarball (sudo apt purge maven).

Quick command summary

Task Command
Install JDK (17 LTS) sudo apt install -y openjdk-17-jdk
Install JDK (default metapackage) sudo apt install -y default-jdk
Check Maven candidate apt-cache policy maven
Install Maven from apt sudo apt install -y maven
Verify mvn -version
Set JAVA_HOME (17) export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
Official binary to /opt Extract apache-maven-3.9.16-bin.tar.gz and extend PATH
Hello-world generate mvn archetype:generate … -DinteractiveMode=false
Remove apt Maven sudo apt purge -y maven

Prerequisites

  • Ubuntu 22.04 LTS, 24.04 LTS, or newer (25.04 tested here) on amd64.
  • sudo for package installation.
  • A JDK 8 or newerApache Maven 3.9.x requires JDK 8+; OpenJDK 17 or 21 is recommended on current Ubuntu.
  • Outbound HTTPS to archive.ubuntu.com and repo.maven.apache.org (or your corporate mirror).

Choose an install method

Method Best for Typical version (2026)
apt install maven Everyday Java builds on Ubuntu 3.6.3 on 22.04; 3.9.9 on 25.04
Official .tar.gz binary Latest upstream (3.9.16) on any LTS maven.apache.org/download
Maven Wrapper (mvnw) Teams pinning Maven per project Bundled in project repo; no global install

Ubuntu apt often lags the current Apache release (Stack Overflow discussion). For most developers apt is enough; use the binary when plugins or documentation require the newest 3.9.x patch.


Step 1: Install a JDK

Maven is a Java program—it will not run without a JDK.

Option A — OpenJDK 17 (widely used LTS):

bash
sudo apt update
sudo apt install -y openjdk-17-jdk
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64

Option B — default-jdk metapackage (tracks the current Ubuntu default, 21 on 25.04):

bash
sudo apt update
sudo apt install -y default-jdk
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64

Verify Java:

bash
java -version
javac -version
text
openjdk version "21.0.9" 2025-10-21
OpenJDK Runtime Environment (build 21.0.9+10-Ubuntu-125.04)
OpenJDK 64-Bit Server VM (build 21.0.9+10-Ubuntu-125.04, mixed mode, sharing)

Persist JAVA_HOME in ~/.profile or /etc/profile.d/java.sh when builds should see it in every login shell. See the on-site Java install guide for more JDK options.


Inspect the candidate before installing:

bash
apt-cache policy maven

Example on Ubuntu 25.04:

text
maven:
 Installed: (none)
 Candidate: 3.9.9-1
 Version table:
 3.9.9-1 500
 500 http://archive.ubuntu.com/ubuntu plucky/universe amd64 Packages

Install with apt:

bash
sudo apt install -y maven

Verify:

bash
mvn -version
which mvn
dpkg -l maven
text
Apache Maven 3.9.9
Maven home: /usr/share/maven
Java version: 21.0.9, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.14.0-37-generic", arch: "amd64", family: "unix"
/usr/bin/mvn
ii maven 3.9.9-1 all Java software project management and comprehension tool

Apt Maven layout:

Path Purpose
/usr/bin/mvn Launcher script
/usr/share/maven Maven home
/etc/maven/settings.xml System-wide defaults
~/.m2/repository Local dependency cache

This matches the official Maven installation guide APT section: sudo apt install maven.


Step 3: Install from the official binary archive

When apt is too old (for example 3.6.3 on Ubuntu 22.04) or you need 3.9.16, use the upstream tarball from the Apache Maven download page.

Remove apt Maven to avoid PATH conflicts:

bash
sudo apt purge -y maven

Download and extract (example 3.9.16, latest stable per maven.apache.org):

bash
cd /tmp
wget https://dlcdn.apache.org/maven/maven-3/3.9.16/binaries/apache-maven-3.9.16-bin.tar.gz
sudo tar -xzf apache-maven-3.9.16-bin.tar.gz -C /opt

Create a system-wide environment snippet:

bash
sudo tee /etc/profile.d/maven.sh >/dev/null <<'EOF'
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export M2_HOME=/opt/apache-maven-3.9.16
export MAVEN_HOME=/opt/apache-maven-3.9.16
export PATH="${MAVEN_HOME}/bin:${JAVA_HOME}/bin:${PATH}"
EOF
sudo chmod +x /etc/profile.d/maven.sh
source /etc/profile.d/maven.sh

Adjust JAVA_HOME if you use 21 (/usr/lib/jvm/java-21-openjdk-amd64) or another JDK path.

Confirm:

bash
mvn -version
text
Apache Maven 3.9.16 (…)
Maven home: /opt/apache-maven-3.9.16
Java version: 17.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-17-openjdk-amd64

Log out and back in (or open a new SSH session) if a fresh shell does not pick up /etc/profile.d/maven.sh—a common gotcha from manual install guides.


Step 4: Verify with a hello-world project

Generate a sample project:

bash
mkdir -p ~/maven-hello && cd ~/maven-hello
mvn archetype:generate \
 -DgroupId=com.example \
 -DartifactId=hello \
 -DarchetypeArtifactId=maven-archetype-quickstart \
 -DarchetypeVersion=1.4 \
 -DinteractiveMode=false
text
[INFO] Project created from Archetype in dir: …/maven-hello/hello
[INFO] BUILD SUCCESS

The quickstart archetype still targets Java 7 in its pom.xml. Modern JDKs reject that—override the release when building:

bash
cd hello
mvn package -Dmaven.compiler.release=21
java -cp target/hello-1.0-SNAPSHOT.jar com.example.App
text
Hello World!

For real projects, set <maven.compiler.release>17</maven.compiler.release> (or 21) in pom.xml instead of relying on the command-line flag.


Optional: Maven Wrapper

Teams that pin Maven per repository can use ./mvnw instead of a global install—the wrapper downloads the project’s Maven version on first run. No Ubuntu package step required beyond the JDK.


Uninstall

APT Maven:

bash
sudo apt purge -y maven
sudo apt autoremove -y

Manual /opt install:

bash
sudo rm -rf /opt/apache-maven-*
sudo rm -f /etc/profile.d/maven.sh

Optional cache cleanup:

bash
rm -rf ~/.m2/repository

Troubleshooting

Symptom Likely cause Fix
mvn: command not found Maven not installed or not on PATH sudo apt install maven or source /etc/profile.d/maven.sh
JAVA_HOME is not defined / wrong Java JDK missing or mispointed Install openjdk-17-jdk; export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
mvn -version shows old Maven after tarball install /usr/bin/mvn from apt still first sudo apt purge maven; check which mvn
Source option 7 is no longer supported Old quickstart archetype POM mvn package -Dmaven.compiler.release=17 or edit pom.xml
Apt Maven older than upstream Distro packaging lag Use official 3.9.16 tarball (Stack Overflow #67985216)
Dependency download failures Network, proxy, or mirror Configure ~/.m2/settings.xml mirrors/proxies; check HTTPS
Multiple JDKs confuse Maven JAVA_HOME not aligned sudo update-alternatives --config java; match JAVA_HOME

References


Summary

Install a JDK (openjdk-17-jdk or default-jdk), then run sudo apt install -y maven for the simplest path—3.9.9 on Ubuntu 25.04 with Maven home: /usr/share/maven. When apt trails upstream, download apache-maven-3.9.16-bin.tar.gz, extract to /opt, and wire JAVA_HOME plus PATH through /etc/profile.d/maven.sh.

Confirm with mvn -version, align JAVA_HOME with your JDK, and build a sample project—remember to set a modern Java release when using the quickstart archetype on JDK 17+.

Frequently Asked Questions

1. How do I install Maven on Ubuntu?

Install a JDK first (sudo apt install -y openjdk-17-jdk or default-jdk), then sudo apt install -y maven. Confirm with mvn -version. Maven requires Java on PATH or JAVA_HOME set to a JDK 8 or newer installation.

2. What version of Maven does Ubuntu apt install?

It depends on your release. Run apt-cache policy maven before installing. On Ubuntu 25.04 the candidate is 3.9.9-1 from universe; 22.04 LTS ships 3.6.3. Upstream stable is 3.9.16—use the official binary when apt is too old for your project.

3. Do I need to install Java before Maven?

Yes. Apache Maven is a Java application. Install openjdk-17-jdk, openjdk-21-jdk, or default-jdk, then set JAVA_HOME to the JDK directory (for example /usr/lib/jvm/java-17-openjdk-amd64). JRE-only packages are not enough for compiling Java projects.

4. How do I install the latest Maven on Ubuntu?

Download apache-maven-VERSION-bin.tar.gz from maven.apache.org/download, extract under /opt, add /opt/apache-maven-VERSION/bin to PATH via /etc/profile.d/maven.sh, and run mvn -version. Purge apt maven first if both would conflict on PATH.

5. Where is Maven installed on Ubuntu with apt?

The mvn wrapper is /usr/bin/mvn, which points into /usr/share/maven. Configuration lives under /etc/maven (settings.xml, m2.conf). User artifacts cache in ~/.m2/repository.

6. Why does mvn say command not found after a manual install?

The Maven bin directory is not on PATH. Add export PATH=/opt/apache-maven-VERSION/bin:$PATH to ~/.profile or /etc/profile.d/maven.sh, run source on the file, or open a new login shell.

7. How do I make Maven use a specific Java version?

Set JAVA_HOME to the desired JDK (export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64). With multiple JDKs installed, run sudo update-alternatives --config java and align JAVA_HOME with the selected runtime.

8. What is the difference between M2_HOME and MAVEN_HOME?

Both traditionally point at the Maven installation directory for manual tarball installs. Modern Maven on Ubuntu from apt does not require either variable—/usr/bin/mvn handles paths. Set M2_HOME only for custom /opt/apache-maven-* layouts.

9. How do I uninstall Maven from Ubuntu?

For apt: sudo apt purge -y maven. Remove /opt/apache-maven-* and /etc/profile.d/maven.sh for manual installs. Optionally delete ~/.m2 when you no longer need cached artifacts.
Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on …