Troubleshooting

zsh: command not found: mvn? Install Maven and fix PATH and JAVA_HOME

Maven is not part of the JDK. Fix zsh: command not found: mvn on macOS, Linux and Windows, plus JAVA_HOME errors, ./mvnw, sudo, CI and Docker.

Published ~9 min read
On this page

Short answer

Maven is not part of the JDK, so zsh: command not found: mvn means no folder on your PATH holds an mvn. Install it with brew install maven on a Mac or sudo apt install maven on Debian and Ubuntu. On Windows unzip Maven, add its bin folder to PATH and open a new terminal. If the project has an mvnw file, run ./mvnw instead.

Where to start

Check what the shell can see

Before installing anything, ask the shell what it finds. These lines work in bash and zsh and change nothing:

bash · zsh
command -v mvn java          # path of each one this shell would run
type -a mvn                  # every mvn on PATH, plus aliases and functions
echo "$PATH" | tr ':' '\n'   # the folders it searches, in order

No line for mvn means Maven is not installed, or it sits in a folder this shell does not search. No line for java means you also need a JDK, because Maven runs on Java. On one of our Debian 12 servers, with neither installed, bash, sh and the /bin/sh that runs Dockerfile RUN lines answer like this, each with exit status 127:

output · Debian 12
$ bash -c 'mvn -v'
bash: line 1: mvn: command not found
$ sh -c 'mvn -v'
sh: 1: mvn: not found
$ /bin/sh -c 'mvn -B package'
/bin/sh: 1: mvn: not found
$ echo $?
127

In an interactive bash the message reads bash: mvn: command not found, zsh on a Mac prints zsh: command not found: mvn, and Windows has two versions:

Windows
# Windows PowerShell 5.1
mvn : The term 'mvn' is not recognized as the name of a cmdlet, function, script file, or operable program.

# PowerShell 7
mvn: The term 'mvn' is not recognized as a name of a cmdlet, function, script file, or executable program.

# Command Prompt
'mvn' is not recognized as an internal or external command,
operable program or batch file.

They all mean the same thing. A CI log that only shows the number is explained in exit code 127.

macOS: Homebrew or the archive

zsh is the default shell on macOS, and macOS ships no Maven. The quickest route is Homebrew:

macOS · Homebrew
brew install maven
mvn -v

Homebrew’s maven formula (3.10.0 on 8 November 2026) depends on Homebrew’s own openjdk, so it brings a JDK along. The mvn it installs is a small wrapper that sets JAVA_HOME="${JAVA_HOME:-<Homebrew's openjdk>}": Homebrew’s JDK, unless you already set JAVA_HOME yourself. That also means a stale JAVA_HOME line in ~/.zshrc beats the working JDK, see JAVA_HOME.

On Apple Silicon, Homebrew lives in /opt/homebrew, and macOS does not search /opt/homebrew/bin. If even brew is not found, add Homebrew’s shellenv line to ~/.zprofile and open a new terminal:

~/.zprofile (Apple Silicon)
eval "$(/opt/homebrew/bin/brew shellenv)"

Maven’s install page also lists SDKMAN! (sdk install maven) and MacPorts (sudo port install maven3). Whichever you use, a terminal that was open during the install may still remember that mvn was missing: run rehash in zsh or open a new window.

The archive, on macOS or Linux

Without a package manager, download apache-maven-3.10.0-bin.tar.gz from maven.apache.org, unpack it and put its bin folder on PATH. Maven 3.10 needs JDK 8 or newer to run.

bash · zsh
tar xzvf apache-maven-3.10.0-bin.tar.gz
mkdir -p ~/opt && mv apache-maven-3.10.0 ~/opt/
echo 'export PATH="$HOME/opt/apache-maven-3.10.0/bin:$PATH"' >> ~/.zshrc   # bash: ~/.bashrc
source ~/.zshrc
mvn -v

The single quotes keep $HOME and $PATH unexpanded in the file, so they are read fresh in every new shell. How PATH changes become permanent, and which file each shell reads, is in how to set environment variables on Linux. Maven 4 is still a release candidate (4.0.0-rc-7), which the Maven project calls not safe for production; it needs JDK 17 or newer.

Debian and Ubuntu: apt install maven

The package is called maven. On our Debian 12 server apt offers version 3.8.7, and its package metadata shows what it depends on:

output · Debian 12
$ apt-cache policy maven
maven:
  Installed: (none)
  Candidate: 3.8.7-1
  Version table:
     3.8.7-1 500
        500 mirror+file:/etc/apt/mirrors/debian.list bookworm/main amd64 Packages
$ apt-cache show maven | grep -E '^(Package|Version|Depends):'
Package: maven
Version: 3.8.7-1
Depends: default-jre-headless (>= 2:1.7) | java7-runtime-headless, libjansi-java, libmaven3-core-java (= 3.8.7-1), libwagon-file-java, libwagon-http-shaded-java

default-jre-headless is a Java runtime. It is enough to start mvn, but it has no javac, so install a JDK in the same step. On a Java runtime that has no compiler at all, the compiler plugin stops with:

mvn compile with only a JRE
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

The JDK package avoids that:

Debian · Ubuntu
sudo apt update
sudo apt install maven default-jdk-headless
mvn -v

On Debian 12, default-jdk-headless pulls in openjdk-17-jdk-headless. The package registers /usr/bin/mvn through update-alternatives, pointing at /usr/share/maven/bin/mvn, so it is on every PATH, sudo’s included. The versions are fixed for the life of each release:

Releasemaven packageChecked with
Debian 123.8.7apt-cache on our server
Debian 133.9.9packages.debian.org
Ubuntu 24.04 LTS3.8.7packages.ubuntu.com
Ubuntu 26.04 LTS3.9.12packages.ubuntu.com
Current upstream3.10.0maven.apache.org

If a project needs a newer Maven than your release ships, use its wrapper or the archive. On Fedora and RHEL the package has the same name: sudo dnf install maven.

Windows: unzip, MAVEN_HOME and PATH

Maven has no Windows installer of its own. The manual route takes four steps:

  • Download apache-maven-3.10.0-bin.zip from maven.apache.org and unzip it to a folder that stays, for example C:\tools\apache-maven-3.10.0.
  • Install a JDK and set JAVA_HOME to its folder: the one that contains bin, not bin itself.
  • Set MAVEN_HOME to the Maven folder and add %MAVEN_HOME%\bin to Path in the Environment Variables dialog, or run the PowerShell lines below.
  • Close every terminal, open a new one and run mvn -v.
PowerShell · user variables, no admin needed
$maven = "C:\tools\apache-maven-3.10.0"
Test-Path "$maven\bin\mvn.cmd"     # must say True
[Environment]::SetEnvironmentVariable('MAVEN_HOME', $maven, 'User')
$user = [Environment]::GetEnvironmentVariable('Path', 'User')
[Environment]::SetEnvironmentVariable('Path', "$user;$maven\bin", 'User')

A terminal that was open while you changed the variables keeps the old PATH, and so does an editor’s built-in terminal until the editor restarts. Maven’s install page also names two package managers for Windows: choco install maven (Chocolatey) and scoop install main/maven (Scoop). The same rule about a new terminal applies.

mvn runs, then complains about JAVA_HOME

Once mvn is found, it looks for Java. Both launcher scripts do the same thing: with JAVA_HOME empty they take java from PATH; with JAVA_HOME set they use $JAVA_HOME/bin/java (%JAVA_HOME%\bin\java.exe on Windows) and nothing else. If that file does not exist, Maven stops with exit status 1:

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

Despite the wording, an empty JAVA_HOME is fine as long as java is on PATH. The usual causes are these:

  • No Java at all. On our Debian 12 server without a JDK, bash -c 'java -version' answers bash: line 1: java: command not found; an interactive bash says bash: java: command not found. Install a JDK first.
  • JAVA_HOME ends in bin. Maven then looks for bin/bin/java. Point it at the JDK folder one level up.
  • JAVA_HOME names a JDK that is gone. Common after an update changed the version in the folder name. Fix the line in your shell profile or the Windows variable.
bash · zsh
echo "$JAVA_HOME"
ls -l "$JAVA_HOME/bin/java"   # must exist
java -version
unset JAVA_HOME               # test: let mvn take java from PATH
mvn -v

mvn -v prints the Java version and runtime folder Maven uses, the quickest way to see which JDK a build gets.

The project has mvnw: use the Maven Wrapper

Many projects ship the Maven Wrapper: mvnw, mvnw.cmd and .mvn/wrapper/maven-wrapper.properties. The script downloads the Maven version the project names (with wget or curl, PowerShell on Windows) into ~/.m2, or MAVEN_USER_HOME if set, and runs it. No mvn on PATH is needed, only Java. Run it from the folder that holds it:

bash · zsh · PowerShell
./mvnw clean install        # Linux, macOS, Git Bash
.\mvnw.cmd clean install    # PowerShell (cmd: mvnw.cmd clean install)

To add a wrapper to a project, run mvn wrapper:wrapper once with Maven installed; -Dmaven=3.10.0 pins the version. Commit all three files. Typed the wrong way, the wrapper gives its own set of errors. In a folder without one, our server answers:

output · Debian 12, folder without mvnw
$ bash -c 'mvnw clean package'
bash: line 1: mvnw: command not found
$ bash -c './mvnw clean package'
bash: line 1: ./mvnw: No such file or directory
$ sh -c './mvnw clean package'
sh: 1: ./mvnw: not found
  • mvnw: command not found: the ./ is missing. Shells do not search the current folder for commands. PowerShell does not either: Microsoft’s docs say to use .\ for a program in the current directory.
  • No such file or directory or ./mvnw: not found: you are not in the folder that holds mvnw, usually the project root. cd there or call it by its path.
  • bash: ./mvnw: Permission denied (exit status 126, which bash returns for any file without the execute bit): typical when the file was added on Windows or copied with a tool that drops permissions. Run chmod +x mvnw, then git update-index --chmod=+x mvnw and commit, so it stays fixed for everyone and for CI. sh mvnw clean install also works without the bit.
  • ./mvnw: 1: cannot open ./.mvn/wrapper/maven-wrapper.properties: No such file (sh) or No such file or directory (bash): the hidden .mvn folder did not come along. Copy it, or run mvn wrapper:wrapper again. cannot read distributionUrl property means the file exists but has no distributionUrl line.
  • The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run. or The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run.: without wget and curl the wrapper checks Java itself and says this; otherwise it downloads Maven first and you get Maven’s own JAVA_HOME message. Either way, same fix.

Works in IntelliJ IDEA, not in the terminal

IntelliJ IDEA builds with the Maven chosen under Maven home path in its Maven settings: a bundled Maven 3, a resolved variable such as MAVEN_HOME, a folder you pick, or the project’s wrapper, which it takes automatically when .mvn/wrapper/maven-wrapper.properties exists. The bundled copy belongs to the IDE and is not installed in any PATH folder. So the Maven tool window builds fine while mvn in a terminal, IntelliJ’s own terminal included, says command not found.

Fix the terminal, not the IDE: run ./mvnw if the project has one, or install Maven as above. If the IDE and the terminal should use the same Maven, point Maven home path at that install.

sudo: mvn: command not found

sudo replaces your PATH with secure_path. Debian 12’s /etc/sudoers sets it to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin. The apt package’s /usr/bin/mvn is on it; a Maven unpacked under ~/opt and added in ~/.bashrc is not, so sudo mvn fails while mvn works. sudo -E does not change that, as the same test with npm shows.

Usually the fix is to drop sudo. A Maven build needs no root, and with sudo the target folder ends up owned by root, which breaks your next build as yourself. On a server, build as a normal user (how to create a sudo user). If one command really needs root, sudo env "PATH=$PATH" mvn ... passes your PATH along.

CI and Docker

A Dockerfile RUN line runs under /bin/sh, so a Debian- or Ubuntu-based image without Maven fails with /bin/sh: 1: mvn: not found, the line from the output above. Build in the official maven image instead, which ships Maven and a JDK:

Dockerfile
FROM maven:3.10.0-eclipse-temurin-21 AS build
WORKDIR /src
COPY . .
RUN mvn -B package

Docker Hub names the tags by Maven version, JDK build, JDK major version and an optional OS variant: 3.10.0-eclipse-temurin-21, 3.10.0-eclipse-temurin-21-alpine, 3.10.0-eclipse-temurin-21-noble. Shorter aliases such as 3.10-eclipse-temurin-21 and 3-eclipse-temurin-21 move with new releases, and latest was 3.10.0 with Eclipse Temurin 27 on 8 November 2026. Other JDK builds appear as amazoncorretto, ibm-semeru, ibmjava and sapmachine. Pin the full Maven version and the JDK major for reproducible builds. The jdk-8 style names in older tutorials (Docker Hub’s own example still shows maven:3.3-jdk-8) are not in the supported tag list.

In CI, ./mvnw is often the simplest choice: the job needs only a JDK plus curl or wget, and the build uses the Maven version committed with the project. Commit the execute bit as shown above, or call sh mvnw. If the runner cannot find docker itself, see docker: command not found.

Check that it works

bash · zsh
command -v mvn
mvn -v
./mvnw -v        # in a project with the wrapper

The first line names the mvn this shell runs: /usr/bin/mvn from apt, /opt/homebrew/bin/mvn from Homebrew on Apple Silicon, or the bin folder of the archive. mvn -v should look like the sample on Maven’s install page:

mvn -v, sample from maven.apache.org
Apache Maven 3.10.0 (c43a36b8d67be7e0805a411bc0898af1a51f5472)
Maven home: /opt/apache-maven-3.10.0
Java version: 21.0.12.1, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-arm64
Default locale: en, platform encoding: UTF-8, time zone: Etc/UTC
OS name: "linux", version: "7.0.0-1013-oracle", arch: "aarch64", family: "unix"

Repeat the check wherever it failed before: a new terminal, sudo, the CI job. Building Java services on your own server? Our Standard KVM plans come with full root access, so apt, the archive and the wrapper work exactly as shown here.

How we tested

OS image
Debian 12 (bookworm)
Versions
GNU bash 5.2.15dash 0.5.12 (/bin/sh)apt candidate maven 3.8.7-1
Tested on
Where
One of our own Debian 12 servers, without Maven or Java
Method
Run as shown: apt-cache policy and apt-cache show for maven, the not-found messages of bash, sh and /bin/sh for mvn, mvnw and java with exit status 127, bash's Permission denied answer (exit status 126) for a file without the execute bit, and the secure_path line read from /etc/sudoers; nothing was installed and sudo was not run
From vendor sources, not run
Maven download, install, CLI and 3.5.0 release-note pages (maven.apache.org), Maven Wrapper docs and the mvn, mvn.cmd and only-mvnw scripts in the Apache repositories, the plexus-compiler message, Homebrew's maven formula and Java helper, Debian and Ubuntu package pages, Docker Hub maven tags, IntelliJ IDEA Maven settings (jetbrains.com), PowerShell command precedence (learn.microsoft.com), all read 2026-11-08

Frequently asked

Why does zsh say command not found: mvn on my Mac?

macOS ships no Maven, and Maven is not part of the JDK. Install it with 'brew install maven', which also brings Homebrew's openjdk, then open a new terminal or run rehash. On Apple Silicon, Homebrew's /opt/homebrew/bin is only on PATH once its shellenv line is in ~/.zprofile.

How do I install Maven on Ubuntu or Debian?

Run 'sudo apt install maven default-jdk-headless'. The maven package alone pulls in only a Java runtime, which has no javac. The package puts mvn in /usr/bin. Debian 12 ships Maven 3.8.7 and Ubuntu 26.04 ships 3.9.12, while the current release is 3.10.0; for a newer version use the project's ./mvnw or the archive from maven.apache.org.

How do I fix 'mvn' is not recognized on Windows?

Unzip apache-maven-3.10.0-bin.zip to a permanent folder, set JAVA_HOME to your JDK folder (not its bin folder), add Maven's bin folder to Path, for example as %MAVEN_HOME%\bin, then close every terminal and open a new one. M2_HOME is not needed: Maven 3.5.0 removed it, and the mvn.cmd script works out its own folder.

What do I do when the project has an mvnw file?

Run ./mvnw from the project root (mvnw.cmd in cmd, .\mvnw.cmd in PowerShell). It downloads the Maven version the project names into ~/.m2 and needs only Java, no mvn on PATH. 'mvnw: command not found' means the ./ is missing; 'Permission denied' means the execute bit is gone: run chmod +x mvnw, then git update-index --chmod=+x mvnw and commit.

Related articles

All troubleshooting