zsh: command not found: mvn

command-not-found

Published ยท Updated

zsh: command not found: mvn means zsh looked through every folder in your PATH and found no mvn. macOS does not include Maven, and neither does a JDK, so on a new Mac this error is expected. Pick the fix that matches your situation:

  • The project has an mvnw file in its root: use the Maven Wrapper, no install needed.
  • You want mvn everywhere: install Maven with Homebrew.
  • You unpacked Maven by hand or use an IDE's copy: put its bin folder on PATH.

Check first

command -v mvn java
java -version
ls mvnw 2>/dev/null

/usr/bin/java exists on every Mac, but without a JDK it only prints "Unable to locate a Java Runtime". Maven needs a real JDK either way.

Fix 1: use the project's Maven Wrapper

Many Spring Boot and other Java projects ship mvnw. It downloads the Maven version the project was built with, so you do not need a global install:

./mvnw clean install

Two errors come up here:

  • zsh: command not found: mvnw: you typed mvnw without ./. The current folder is not on PATH, so zsh will not look there.
  • zsh: permission denied: ./mvnw: the file lost its execute bit, often after a zip download. Run chmod +x mvnw once.

The wrapper still needs Java. If it complains about JAVA_HOME, see below.

Fix 2: install Maven with Homebrew

brew install maven
rehash
mvn -v

Homebrew's maven formula pulls in Homebrew's openjdk and uses it unless you set JAVA_HOME yourself, so this is the shortest path to a working mvn clean install. mvn lands in /opt/homebrew/bin on Apple silicon and /usr/local/bin on Intel Macs.

If brew install worked but mvn is still not found, the Homebrew folder is not on your PATH. Add Homebrew's setup line to ~/.zprofile and open a new terminal tab:

eval "$(/opt/homebrew/bin/brew shellenv)"

If brew itself is missing, start with zsh: command not found: brew.

Fix 3: a manual Maven download

If you downloaded the binary archive from maven.apache.org, unpack it somewhere permanent and add its bin folder to ~/.zprofile:

mkdir -p ~/tools
tar -xzf ~/Downloads/apache-maven-*-bin.tar.gz -C ~/tools
ls ~/tools                 # note the exact folder name

# ~/.zprofile: replace X.Y.Z with the folder name ls printed
export MAVEN_HOME="$HOME/tools/apache-maven-X.Y.Z"
export PATH="$MAVEN_HOME/bin:$PATH"

Open a new tab and run mvn -v. Keep the archive out of ~/Downloads; a cleanup there would break mvn again.

JAVA_HOME errors after mvn is found

The JAVA_HOME environment variable is not defined correctly means Maven started but cannot find a JDK. macOS has a helper that prints the path of an installed JDK:

/usr/libexec/java_home -V            # list installed JDKs
export JAVA_HOME="$(/usr/libexec/java_home)"

Put the export line in ~/.zprofile. Use java_home -v 21 to pick a version. java_home only sees JDKs in /Library/Java/JavaVirtualMachines. A JDK from Homebrew's openjdk formula is not there until you link it, as brew info openjdk explains:

sudo ln -sfn "$(brew --prefix)/opt/openjdk/libexec/openjdk.jdk" \
  /Library/Java/JavaVirtualMachines/openjdk.jdk

mvn works in IntelliJ but not in the terminal

IntelliJ IDEA bundles its own Maven and uses it for the Maven tool window. That copy is not on your shell's PATH, so mvn in the IDE's terminal fails while the Maven buttons work. Use ./mvnw if the project has it, or install Maven with Homebrew. You can point IntelliJ at the Homebrew copy later under Settings โ†’ Build, Execution, Deployment โ†’ Build Tools โ†’ Maven so both use the same version.

Why the fix belongs in ~/.zprofile

Terminal.app, iTerm2 and the VS Code terminal start zsh as a login shell, and login shells read ~/.zprofile before ~/.zshrc. Homebrew's installer writes its setup there for that reason. Adding PATH lines to ~/.zshrc also works in most terminals, but mixing the two makes it hard to tell which one wins. zsh: command not found on macOS: how PATH works walks through the order.

Sources

related.

Ruslan

Ruslan

About the author