How to Install Maven on Mac OS

Filed Under: Maven
Install Maven Mac

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

Comments

  1. Xuan Trieu says:

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

  2. Jyoti says:

    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

  3. iliyaz says:

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

    1. freejack says:

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

  4. Helen says:

    Thanks a bunch! This worked 100%.

  5. Tarun says:

    Helped alot! Thanks

  6. rogerhuang says:

    Thanks for that

  7. Arun says:

    Thanks for that

  8. bikshapathi says:

    Thanks for the article.

  9. Igor says:

    Thanks a lot for this manual. Much appreciated)

  10. Priya says:

    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!

    1. Pankaj says:

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

  11. Brian says:

    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.

    1. Pankaj says:

      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.

  12. Sang Min Park says:

    thank you so much. 🙂

  13. Gibran Castillo says:

    Excellent tutorial. Simple and to the point

  14. A_V says:

    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

  15. Silvio Di Pasquale says:

    Thank you so much for the wonderful tutorial!

  16. Afreen says:

    Thank you for the wonderful tutorial! (y)

  17. alex says:

    I would recommend using homebrew (http://brew.sh/) and simply do:

    brew install maven

  18. flybrid says:

    I got the error below

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

    1. flybrid says:

      I resolved it myself.

      1. Fijai Cairo says:

        How did you resolve it?

        1. Fijai Cairo says:

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

        2. Wiem SGHAIER says:

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

  19. James says:

    Thanks for that!

  20. cindy says:

    thanks! this instructions helped me a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages