How to install Maven on macOS
This article shows how to install Maven (3.6.3) on macOS Big Sur (version 11.1).
Topics
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.
% brew install maven
1.3 Done. The Maven is installed on macOS.
% 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}
% 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/
% 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
# real path
/usr/local/Cellar/maven/3.6.3_1/libexec/conf
# symbolic links to the above path
/usr/local/opt/maven/libexec/conf
% 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/
% 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.
% 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
% 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
2.2 The below command extracts the downloaded .tar.gz
file to the current user’s home directory ~
% 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
% 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.
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.
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.
% source ~/.zshenv
2.6 Verification.
% 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.
Perhaps, you have to add ‘source ~/.bash_profile’. Otherwise is a good article.
Article is updated with
source ~/.zshenv
Now the macOS default shell is zsh.
I would recommend Homebrew for installing maven – it should almost always be the install method of choice. ‘$ brew install maven’
Article is updated with
brew install
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.
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.
how to create .zshenv file?
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.
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!
Refer to https://mkyong.com/maven/install-maven-on-mac-osx/#install-maven-manually
macOS 10.5 Catalina and later
for the
zsh
shell, puts the environment variables at~/.zshenv
macOS 14 Mojave and before
for the
bash
shell, puts the environment variables at~/.bash_profile
Hi My Friend,
MAVEN came pre installed since version 10.6.8 MacOSx Snow Leopard.
Cheers,
Flavio
Not anymore, latest macOS doesn’t come with Maven pre-installed.
Thank you for sharing your learning.
thanks a lot. resolved quickly and able to work on project
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
Thank you so much
thanks!
I love you! Thank you very much!
Thanks for the article
How do you do it for fish shell?
Thank you. This was very helpful.
Thank you, as always, clear and concise!
Thank you, it worked!
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)
Just configure the $JAVA_HOME variable.
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!).
Yes, after upgraded my Mac to OSX Mavericks, Maven was gone. I have to install the Maven manually, article is updated accordingly. Thanks for your comment.
perhaps it’s worth mentioning brew install: `brew install maven`