Tutorial

How to Install Maven on Mac OS

Published on August 3, 2022
How to Install Maven on Mac OS

Install Maven on Mac

Maven is the most widely used build and project dependency management tool for Java-based applications. We can install Maven on Mac OS using a package manager such as HomeBrew or through XCode Command Line Tools. But, in this tutorial, we will learn how to install Maven on Mac OS without using any other software. Maven requires Java to execute. So we will have to first install Java and then maven into our Mac OS.

1. Installing Java on Mac

We will install OpenJDK in our Mac OS. It’s free and you don’t have to worry about licensing that comes with Oracle JDK build.

1.1) Downloading Java for Mac OS

Go to the latest JDK GA release page and download the tar file for Mac OS. Java 13 GA Release URL: https://jdk.java.net/13/ Then extract it to the directory of your choice. I prefer to keep my JDK setup at “/Library/Java/JavaVirtualMachines/” directory.

$ tar -xvf openjdk-13.0.1_osx-x64_bin.tar.gz
$ sudo mv jdk-13.0.1.jdk /Library/Java/JavaVirtualMachines/

Recommended Reading: Linux tar command to compress and extract files

1.2) Setting Environment Variables - JAVA_HOME and Path

Open .bash_profile and add the following entries at the end of it.

JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home"
PATH="${JAVA_HOME}/bin:${PATH}"
export PATH

You can relaunch the Terminal to apply these profile changes. Or you can also run source .bash_profile command to apply these environment variable changes. Recommended Reading: Linux Environment Variables

1.3) Verifying the JDK installation

Open the Terminal and run java -version command. It should show the following output.

$ java -version
openjdk version "13.0.1" 2019-10-15
OpenJDK Runtime Environment (build 13.0.1+9)
OpenJDK 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)
$

However, you might get an alert message with the following warning.

“jdk-13.0.1.jdk” cannot be opened because the developer cannot be verified.
macOS cannot verify that this app is free from malware.
Jdk13 Macos Verify Error
Jdk13 Macos Verify Error

You will have to allow the app to execute from the “Security and Privacy” settings. Allow Apps Mac Security And Privacy After that, the java command will work fine and the alert message will not be shown.

2. Install Maven on Mac OS

Now that we have successfully installed JDK, we are ready to download and install Maven in Mac OS.

2.1) Download Maven for Mac OS

Go to the Maven Download site: https://maven.apache.org/download.cgi Download the “Binary tar.gz archive” file as shown in the below image.

Maven Binary Tar Archive
Maven Binary Tar Archive

After downloading, extract it using the below command.

$ tar -xvf apache-maven-3.6.3-bin.tar.gz

The binaries will be extracted in the “apache-maven-3.6.3” directory. You can keep them anywhere, I have kept it in the Downloads directory for the sake of easy access.

2.2) Setting Maven Environment Variables - M2_HOME and Path

The next step is to set up the environment variables - M2_HOME and Path. We have to add the Maven bin directory to the Path variable. Open .bash_profile in your favorite text editor and add below lines to the end of it.

export M2_HOME="/Users/pankaj/Downloads/apache-maven-3.6.3"
PATH="${M2_HOME}/bin:${PATH}"
export PATH

You can relaunch Terminal to load these profile settings or use source .bash_profile command to apply it.

2.3) Verifying the Maven Installation

Finally, run the mvn -version command to check if Maven is installed successfully.

$ mvn -version     
OpenJDK 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/pankaj/Downloads/apache-maven-3.6.3
Java version: 13.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.1", arch: "x86_64", family: "mac"
$

The output shows maven home location, the JDK it’s using and also the Mac OS version details. Maven is successfully installed in your Mac OS. You are ready to create a maven based Java projects.

References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Pankaj Kumar
Pankaj Kumar
See author profile
Category:
Tutorial
Tags:

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 26, 2014

thanks! this instructions helped me a lot!

- cindy

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 10, 2014

    Thanks for that!

    - James

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      June 21, 2014

      I got the error below Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher

      - flybrid

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      June 21, 2014

      I resolved it myself.

      - flybrid

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      July 15, 2014

      How did you resolve it?

      - Fijai Cairo

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      July 15, 2014

      export M2_HOME=/usr/local/Cellar/maven/3.2.2/libexec

      - Fijai Cairo

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        March 17, 2015

        export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.5 export M2=$M2_HOME/bin export PATH=$M2:$PATH

        - Wiem SGHAIER

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          July 2, 2014

          I would recommend using homebrew (https://brew.sh/) and simply do: brew install maven

          - alex

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            July 19, 2014

            Thank you for the wonderful tutorial! (y)

            - Afreen

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              July 21, 2014

              Thank you so much for the wonderful tutorial!

              - Silvio Di Pasquale

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                August 8, 2014

                Very precise and easy tutorial. Thank You. ----------------------- The way to open a .bash_profile touch .bash_profile then say: open -a TextEdit.app .bash_profile

                - A_V

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  August 22, 2014

                  Excellent tutorial. Simple and to the point

                  - Gibran Castillo

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    September 4, 2014

                    thank you so much. :)

                    - Sang Min Park

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      September 13, 2014

                      May I recommend creating a symbolic link for maven once you have it installed rather than having the specific version in your path and M2_Home variable? ln -s apache-maven-3.0.5 maven That way you only have to move the symbolic link when you install a new version.

                      - Brian

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      September 13, 2014

                      Its a personal choice, I prefer to have these kind of settings in my bash profile. I have java 6, 7, 8 setup and java home is configured in bash profile.

                      - Pankaj

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        November 26, 2014

                        Hi, I am not able to locate the .m2 folder. I installed the entire thing in my Documents folder. Also, I have enabled all hidden files to be shown. Yet, I can’t see the .m2 where my local repo will be. Please help!

                        - Priya

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        November 26, 2014

                        By default it’s located in the user home directory.

                        - Pankaj

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          January 22, 2015

                          Thanks a lot for this manual. Much appreciated)

                          - Igor

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            January 23, 2015

                            Thanks for the article.

                            - bikshapathi

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              February 17, 2015

                              Thanks for that

                              - Arun

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                July 7, 2015

                                Thanks for that

                                - rogerhuang

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  August 13, 2015

                                  Helped alot! Thanks

                                  - Tarun

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    September 1, 2015

                                    Thanks a bunch! This worked 100%.

                                    - Helen

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      September 16, 2015

                                      Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher

                                      - iliyaz

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      April 27, 2016

                                      I got the same Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher

                                      - freejack

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        November 6, 2020

                                        Check if your PATH variable is set correctly. In my case, I was overriding the pre-existing PATH value which contains “/usr/local/opt/openssl/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin”.

                                        - Ajinkya

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          September 12, 2016

                                          Hi, I tried the steps. When I do this it works for single session. i.e. Next time when I close and reopen terminal and do man -version I get: -sh: mvn: command not found How to fix this? Waiting for reply

                                          - Jyoti

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          June 20, 2021

                                          I also face same issue. Every time for new terminal getting -sh: mvn: command not found To resolve the issue I need to hit source .bash_profile before executing mvn -version

                                          - Mitul

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            February 7, 2017

                                            thank you so much, I tried it and worked, after going frustrated with “how to fix Maven downloaded via Homebrew”

                                            - Xuan Trieu

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              April 2, 2020

                                              Thanks Pankaj, Found this while trying to install with Homebrew wasn’t working for me. This was truly helpful

                                              - Vickie

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                June 3, 2020

                                                manojkumar.panchal@LM0000941 ~ % mvn version [INFO] Scanning for projects… [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.069 s [INFO] Finished at: 2020-06-03T16:11:40+05:30 [INFO] ------------------------------------------------------------------------ [ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/Users/manojkumar.panchal). Please verify you invoked Maven from the correct directory. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException

                                                - manojpanchal50

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  September 28, 2020

                                                  On new MacOS, Catalina for example, the .bash_profile has been renamed to .zprofile

                                                  - Micael

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  October 19, 2021

                                                  This worked. I have been searching on internet for 2 hours. Thank a lot Micael.

                                                  - Mandar

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    October 22, 2021

                                                    thank Micael, this works for me !!

                                                    - Bhuwan

                                                      JournalDev
                                                      DigitalOcean Employee
                                                      DigitalOcean Employee badge
                                                      October 12, 2020

                                                      Thank you so much… The steps helped me

                                                      - Ramesh

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        December 16, 2020

                                                        java -version command works for me. But mvn --version still says mv command not found I have following in bash_profile JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home export PATH JAVA_HOME J2=$JAVA_HOME/bin export PATH J2 M2_HOME=/Applications/apache-maven-3.4.5 export PATH M2_HOME M2=$M2_HOME/bin export PATH M2

                                                        - Heena Jain

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          March 10, 2021

                                                          Hi, I am trying to install maven on my mac pc. I followed all your steps and getting this error “Error occurred during initialization of VM Incompatible minimum and maximum heap sizes specified” Please help to get me out from this error TIA

                                                          - Mani Gupta

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            April 21, 2021

                                                            Hi I’ve tried every possible solution online to set a maven path on MacBookAir(Apple M1 chip), but nothing worked. I’ve downloaded maven and extracted but setting the path on a terminal is troubling me. I always get the message on the terminal “zsh: command not found: PATH” whenever I try to set the path. Can someone help me with this, please?

                                                            - Rajita

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            April 21, 2021

                                                            When you execute the $PATH command, does it print some path or print nothing? If it prints nothing, then you need to set the PATH variable in the .bash_profile file. If it prints something, then see if the maven path is there or not. I have the following entries in my .bash_profile for setting the maven path. export M2_HOME=“/Users/pankaj/Downloads/apache-maven-3.6.3” PATH=“${M2_HOME}/bin:${PATH}” export PATH

                                                            - Pankaj

                                                              Join the Tech Talk
                                                              Success! Thank you! Please check your email for further details.

                                                              Please complete your information!

                                                              Become a contributor for community

                                                              Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                                              DigitalOcean Documentation

                                                              Full documentation for every DigitalOcean product.

                                                              Resources for startups and SMBs

                                                              The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                                              Get our newsletter

                                                              Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                                              New accounts only. By submitting your email you agree to our Privacy Policy

                                                              The developer cloud

                                                              Scale up as you grow — whether you're running one virtual machine or ten thousand.

                                                              Get started for free

                                                              Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                                                              *This promotional offer applies to new accounts only.