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.
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 newer—Apache Maven 3.9.x requires JDK 8+; OpenJDK 17 or 21 is recommended on current Ubuntu.
- Outbound HTTPS to
archive.ubuntu.comandrepo.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):
sudo apt update
sudo apt install -y openjdk-17-jdk
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64Option B — default-jdk metapackage (tracks the current Ubuntu default, 21 on 25.04):
sudo apt update
sudo apt install -y default-jdk
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64Verify Java:
java -version
javac -versionopenjdk 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.
Step 2: Install Maven from apt (recommended)
Inspect the candidate before installing:
apt-cache policy mavenExample on Ubuntu 25.04:
maven:
Installed: (none)
Candidate: 3.9.9-1
Version table:
3.9.9-1 500
500 http://archive.ubuntu.com/ubuntu plucky/universe amd64 PackagesInstall with apt:
sudo apt install -y mavenVerify:
mvn -version
which mvn
dpkg -l mavenApache 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 toolApt 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:
sudo apt purge -y mavenDownload and extract (example 3.9.16, latest stable per maven.apache.org):
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 /optCreate a system-wide environment snippet:
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.shAdjust JAVA_HOME if you use 21 (/usr/lib/jvm/java-21-openjdk-amd64) or another JDK path.
Confirm:
mvn -versionApache 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-amd64Log 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:
mkdir -p ~/maven-hello && cd ~/maven-hello
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=hello \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 \
-DinteractiveMode=false[INFO] Project created from Archetype in dir: …/maven-hello/hello
[INFO] BUILD SUCCESSThe quickstart archetype still targets Java 7 in its pom.xml. Modern JDKs reject that—override the release when building:
cd hello
mvn package -Dmaven.compiler.release=21
java -cp target/hello-1.0-SNAPSHOT.jar com.example.AppHello 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:
sudo apt purge -y maven
sudo apt autoremove -yManual /opt install:
sudo rm -rf /opt/apache-maven-*
sudo rm -f /etc/profile.d/maven.shOptional cache cleanup:
rm -rf ~/.m2/repositoryTroubleshooting
| 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
- Apache Maven — Installation — prerequisites, apt, binary steps
- Apache Maven — Download — current 3.9.16 tarball
- Stack Overflow: install latest Maven on Ubuntu
- On-site: apt command, wget command, install Java
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+.

