Main Tutorials

How to install Maven on macOS

This article shows how to install Maven (3.6.3) on macOS Big Sur (version 11.1).

Topics

  1. Homebrew install Maven
  2. Manually install Maven

Related Articles

1. Homebrew install Maven on macOS

1.1 Install Homebrew, a package manager on macOS.

1.2 The command brew install maven will install the latest Maven.

Terminal

% brew install maven

1.3 Done. The Maven is installed on macOS.

Terminal

% mvn -version

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/Cellar/maven/3.6.3_1/libexec
Java version: 15.0.1, vendor: N/A, runtime: /usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home
Default locale: en_MY, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

P.S At the time of writing, the latest Maven version is 3.6.3

1.4 Where does Homebrew installed the Maven?
The brew will install the Maven packages at /usr/local/Cellar/maven/${version}

Terminal

% ls -lsa /usr/local/Cellar/maven/3.6.3_1
total 72
0 drwxr-xr-x  9 mkyong  admin    288 Jan 11 11:10 .
0 drwxr-xr-x  3 mkyong  admin     96 Jan 11 11:10 ..
0 drwxr-xr-x  3 mkyong  admin     96 Jan 11 11:10 .brew
8 -rw-r--r--  1 mkyong  admin    756 Jan 11 11:10 INSTALL_RECEIPT.json
40 -rw-r--r--  1 mkyong  admin  17504 Nov  7  2019 LICENSE
16 -rw-r--r--  1 mkyong  admin   5141 Nov  7  2019 NOTICE
8 -rw-r--r--  1 mkyong  admin   2612 Nov  7  2019 README.txt
0 drwxr-xr-x  5 mkyong  admin    160 Jan 11 11:10 bin
0 drwxr-xr-x  6 mkyong  admin    192 Jan 11 11:10 libexec

The brew also creates shortcut or symbolic links at /usr/local/opt/maven/

Terminal

% ls -lsa /usr/local/opt/ | grep maven

0 lrwxr-xr-x   1 mkyong  admin    23 Jan 11 11:10 maven -> ../Cellar/maven/3.6.3_1

1.5 Where does Homebrew put the Maven configuration file, settings.xml?
The settings.xml is available at /usr/local/opt/maven/libexec/conf

Terminal

# real path
/usr/local/Cellar/maven/3.6.3_1/libexec/conf

# symbolic links to the above path
/usr/local/opt/maven/libexec/conf  
Terminal

% ls -lsa /usr/local/opt/maven/libexec/conf
total 32
 0 drwxr-xr-x  5 mkyong  admin    160 Jan 14 07:02 .
 0 drwxr-xr-x  6 mkyong  admin    192 Jan 11 11:10 ..
 0 drwxr-xr-x  3 mkyong  admin     96 Nov  7  2019 logging
24 -rw-r--r--  1 mkyong  admin  10468 Nov  7  2019 settings.xml
 8 -rw-r--r--  1 mkyong  admin   3747 Nov  7  2019 toolchains.xml

1.6 Where does Homebrew put the core mvn executable file?
The mvn is at /usr/local/bin/

Terminal

% ls -lsah /usr/local/bin/mvn

0 lrwxr-xr-x  1 mkyong  admin    31B Jan 11 11:10 /usr/local/bin/mvn -> ../Cellar/maven/3.6.3_1/bin/mvn

1.7 brew info maven to show the detail of the Maven package.

Terminal

% brew info maven   

maven: stable 3.6.3
Java-based project management
https://maven.apache.org/
Conflicts with:
  mvnvm (because also installs a 'mvn' executable)
/usr/local/Cellar/maven/3.6.3_1 (87 files, 10.7MB) *
  Built from source on 2021-01-11 at 11:10:48
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/maven.rb
License: Apache-2.0
==> Dependencies
Required: openjdk ✔

1.8 brew list maven

Terminal

% brew list maven
/usr/local/Cellar/maven/3.6.3_1/bin/mvn
/usr/local/Cellar/maven/3.6.3_1/bin/mvnDebug
/usr/local/Cellar/maven/3.6.3_1/bin/mvnyjp
/usr/local/Cellar/maven/3.6.3_1/libexec/bin/ (4 files)
/usr/local/Cellar/maven/3.6.3_1/libexec/boot/ (2 files)
/usr/local/Cellar/maven/3.6.3_1/libexec/conf/ (3 files)
/usr/local/Cellar/maven/3.6.3_1/libexec/lib/ (70 files)

1.9 Useful commands:

  • brew upgrade maven to upgrade Maven.
  • brew uninstall maven to uninstall Maven.

2. Install Maven Manually

If Homebrew failed you or you don’t want the black-box magic, try to install the Maven manually.

2.1 Download the Maven, for example apache-maven-3.6.3-bin.tar.gz

Install Maven on macOS

2.2 The below command extracts the downloaded .tar.gz file to the current user’s home directory ~

Terminal

% pwd

# In this example, the ${username} is mkyong
/Users/${username}/Downloads

% tar -xvzf apache-maven-3.6.3-bin.tar.gz -C ~        

x apache-maven-3.6.3/README.txt
x apache-maven-3.6.3/LICENSE
x apache-maven-3.6.3/NOTICE
x apache-maven-3.6.3/lib/
#...

2.3 Now, the Maven folder is at this path /Users/mkyong/apache-maven-3.6.3

Terminal

% ls -lsa ~/apache-maven-3.6.3

total 64
0 drwxr-xr-x   9 mkyong  staff    288 Jan 14 11:08 .
0 drwxr-xr-x+ 29 mkyong  staff    928 Jan 14 11:08 ..
40 -rw-r--r--   1 mkyong  staff  17504 Nov  7  2019 LICENSE
16 -rw-r--r--   1 mkyong  staff   5141 Nov  7  2019 NOTICE
8 -rw-r--r--   1 mkyong  staff   2612 Nov  7  2019 README.txt
0 drwxr-xr-x   8 mkyong  staff    256 Jan 14 11:08 bin
0 drwxr-xr-x   4 mkyong  staff    128 Nov  7  2019 boot
0 drwxr-xr-x   5 mkyong  staff    160 Nov  7  2019 conf
0 drwxr-xr-x  65 mkyong  staff   2080 Nov  7  2019 lib

% cd ~/apache-maven-3.6.3
% pwd
/Users/mkyong/apache-maven-3.6.3

2.4 On macOS 10.5 Catalina or later, the default shell is zsh, and we can create the environment variables MAVEN_HOME and update the PATH in ~/.zshenv.

Open the ~/.zshenv and append the following content.

~/.zshenv

export MAVEN_HOME=~/apache-maven-3.6.3
export PATH=$PATH:$MAVEN_HOME/bin

Note
For macOS 10.14 Mojave and before, the default Terminal shell is bash, and we can create the environment variables in ~/.bash_profile.

Open the ~/.bash_profile and append the following content.

~/.bash_profile

export MAVEN_HOME=~/apache-maven-3.6.3
export PATH=$PATH:$MAVEN_HOME/bin

P.S Read this Zsh Startup Files.

2.5 Source the ~/.zshenv to reflect the changes.

Terminal

% source ~/.zshenv

2.6 Verification.

Terminal

% mvn -version

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/mkyong/apache-maven-3.6.3
Java version: 14, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home
Default locale: en_MY, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

Done.

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
28 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Milena Angelova
7 years ago

Perhaps, you have to add ‘source ~/.bash_profile’. Otherwise is a good article.

Jono
8 years ago

I would recommend Homebrew for installing maven – it should almost always be the install method of choice. ‘$ brew install maven’

Jono 2
4 years ago
Reply to  Jono

I came here because Homebrew install failed. Agreed that Homebrew should be added as first choice but rest of this article was incredibly useful. Also good if you want to install a previous version of Maven.

Anirudh
2 years ago

Manual installation worked well for me! Just wanted to add that the .zshenv file might not exist by default. It can be manually created using touch command in terminal and then the env variables can be added inside it.

Chauncey
9 months ago
Reply to  Anirudh

how to create .zshenv file?

Rahul Gupta
2 years ago

It’s working slightly differently way on my system. I have macOS bigSur v11.3.1 and I installed maven v3.6.3 manual way.

I put the path in ~/.zshenv but it would only work in same terminal. In new terminal “mvn -version” wouldn’t work (I had saved using “source ~/.zshenv“)

Finally i had to put the path in “~/.bash_profile” as well. Then it worked even after closing terminal.

Sai
4 years ago

How to set environment variables for Maven when maven installed using “brew install maven” ?

My Default folder path on mac for mvn is —> /usr/local/bin/mvn

Please help!

Flavio
10 years ago

Hi My Friend,

MAVEN came pre installed since version 10.6.8 MacOSx Snow Leopard.

Cheers,

Flavio

Kaushal
9 months ago

Thank you for sharing your learning.

amit
11 months ago

thanks a lot. resolved quickly and able to work on project

YSG
1 year ago

my mac book homebrew install maven to this directory, but I could not find it in my mac /opt/homebrew/bin/mvn . What is that mean? Thank you

Mohammed Abdul Rahman
1 year ago

Thank you so much

Jeison Muniz
1 year ago

thanks!

Nguyen
1 year ago

I love you! Thank you very much!

sam
2 years ago

Thanks for the article

Kuldeep Yadav
2 years ago

How do you do it for fish shell?

Tobe
3 years ago

Thank you. This was very helpful.

Ben
3 years ago

Thank you, as always, clear and concise!

Churro
7 years ago

Thank you, it worked!

Anuj Nagpal
8 years ago

I tried the same getting this

mvn -v

Exception in thread “main” java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)

at java.lang.ClassLoader.defineClass(ClassLoader.java:621)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)

at java.net.URLClassLoader.access$000(URLClassLoader.java:58)

at java.net.URLClassLoader$1.run(URLClassLoader.java:197)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:190)

at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(ClassRealm.java:401)

at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:42)

at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)

at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:254)

at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)

at org.codehaus.plexus.classworlds.launcher.Launcher.getMainClass(Launcher.java:144)

at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:266)

at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)

at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)

at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

Humberto Moura
8 years ago
Reply to  Anuj Nagpal

Just configure the $JAVA_HOME variable.

Richard Clayton
10 years ago

Strangely, Maven was removed in OSX Mavericks (at least after the upgrade it’s nowhere to be found). I’m having to install manually (thank you for the original path locations!).

Hagai
9 years ago
Reply to  mkyong

perhaps it’s worth mentioning brew install: `brew install maven`