64

Before marking this as duplicate, I went through these posts, but nothing helped.

Some are specific to windows and did not help. A couple of them on Mac OS X gave suggestions, that I tried but did not help.

What I tried (this is exactly what Maven suggests):

Extract the distribution archive, i.e. apache-maven-3.1.1-bin.tar.gz to the directory you wish to install Maven 3.1.1. These instructions assume you chose /usr/local/apache-maven. The subdirectory apache-maven-3.1.1 will be created from the archive. In a command terminal, add the M2_HOME environment variable, e.g. export M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1. Add the M2 environment variable, e.g. export M2=$M2_HOME/bin. Optional: Add the MAVEN_OPTS environment variable to specify JVM properties, e.g. export MAVEN_OPTS="-Xms256m -Xmx512m". This environment variable can be used to supply extra options to Maven. Add M2 environment variable to your path, e.g. export PATH=$M2:$PATH. Make sure that JAVA_HOME is set to the location of your JDK, e.g. export JAVA_HOME=/usr/java/jdk1.5.0_02 and that $JAVA_HOME/bin is in your PATH environment variable. Run mvn --version to verify that it is correctly installed.

I see that on the terminal that I used for installation, it works fine. I do not have this issue. but when I tried on a new terminal, I get command not found.

I also added export PATH=$M2 to my .bashrc, I did source and then restarted the terminal, still it did not help.

can someone suggest how to make it available in all sessions of terminal?

Thanks

Kaan
  • 5,434
  • 3
  • 19
  • 41
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
  • if you are on osx mavericks, I believe `mvn` has been replaced by `mvn3`. – Christian Fritz Jan 09 '14 at 19:14
  • @ChristianF: yes I am on mavericks, but I tried `mvn3` and still I get `command not found`. The issue is it works with the terminal used for installation (after following above steps pointed by maven) but not inherited across terminals – eagertoLearn Jan 09 '14 at 19:20
  • try adding the path to .bash_profile then run the command: "source .bash_profile" (without ") to refresh it or open a new terminal – tshenolo Jan 09 '14 at 19:22
  • @tshenolo: I tried this initially before I tried bashrc. now I did again, but it still did not help. This is my entire `bash_profie`: `PATH=$PATH:$HOME/bin export PATH export PATH=$M2:$PATH` – eagertoLearn Jan 09 '14 at 19:29
  • This might help: http://stackoverflow.com/questions/9134712/setting-up-maven-3-properly – tshenolo Jan 09 '14 at 19:47

9 Answers9

82

Try following these if these might help:

Since your installation works on the terminal you installed, all the exports you did, work on the current bash and its child process. but is not spawned to new terminals.

env variables are lost if the session is closed; using .bash_profile, you can make it available in all sessions, since when a bash session starts, it 'runs' its .bashrc and .bash_profile

Now follow these steps and see if it helps:

  1. type env | grep M2_HOME on the terminal that is working. This should give something like

    M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1

  2. typing env | grep JAVA_HOME should give like this:

    JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home

Now you have the PATH for M2_HOME and JAVA_HOME.

If you just do ls /usr/local/apache-maven/apache-maven-3.1.1/bin, you will see mvn binary there. All you have to do now is to point to this location everytime using PATH. since bash searches in all the directory path mentioned in PATH, it will find mvn.

  1. now open .bash_profile, if you dont have one just create one

    vi ~/.bash_profile

Add the following:

#set JAVA_HOME
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home
export JAVA_HOME


M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
export M2_HOME

PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
export PATH
  1. save the file and type source ~/.bash_profile. This steps executes the commands in the .bash_profile file and you are good to go now.

  2. open a new terminal and type mvn that should work.

brain storm
  • 30,124
  • 69
  • 225
  • 393
  • @RicardoGonzales: you need to create `/Users/YOURNAME/.bash_profile`. simply open terminal, enter `cd && vi .bash_profile` – brain storm May 26 '15 at 00:06
  • 3
    Everything work after execute 'source .bash_profile' but when I restart my computer there is no more mvn command found – napstercake May 27 '15 at 01:34
  • You may have your environment variables in the .zshenv file of your home directory. So you could set also the maven and path there – Bkfsec Jun 30 '23 at 03:15
63

Solutions above are good but they require ~/.bash_profile. /usr/local/bin is already in the $PATH and it can be confirmed by doing echo $PATH. Download maven and run the following commands -

$ cd ~/Downloads
$ tar xvf apache-maven-3.5.3-bin.tar.gz
$ mv apache-maven-3.5.3 /usr/local/
$ cd /usr/local/bin
$ sudo ln -s ../apache-maven-3.5.3/bin/mvn mvn
$ mvn -version
$ which mvn

Note: The version of apache maven would be the one you will download.

cronoik
  • 15,434
  • 3
  • 40
  • 78
limitlessriver
  • 751
  • 1
  • 6
  • 9
  • I am unable to run mvn commands even after defining the $M2_HOME, $M2 variables and adding the bin path to the $PATH variable and adding all the above in .bash_profile. I get permission denied. If I try running after sudo, I again get command not found. I checked I don't have mvc path defined in $PATH variable in root mode. Do I need to add it here as well? I cannot find the .bash_profile for root though. – Sandeepan Nath May 29 '19 at 13:56
  • @SandeepanNath This setup doesn't require $M2_HOME. There are other answers that use $M2_HOME. – limitlessriver Jun 02 '19 at 07:00
  • 2
    This works great on OSX Catalina with XCode command line tools already installed. After downloading the binary tar.gz archive for the newest maven, the only things I had to change were using sudo for the mv command and of course replacing the version number with the newer version i actually downloaded. It does not even need me to restart my terminal before using maven. – emery Oct 12 '20 at 17:08
17

The possible solution can be that maven is not installed in your mac system. Use this command to install maven:

brew install maven

And, to verify, that it is successfully installed, run this command:

mvn -v

If it returns you maven version, then maven is successfully installed in your system.

Aditya Aggarwal
  • 610
  • 6
  • 7
12

I got same problem, I tried all above, nothing solved my problem. Luckily, I solved the problem this way:

echo $SHELL

Output

/bin/zsh
OR 
/bin/bash

If it showing "bash" in output. You have to add env properties in .bashrc file (.bash_profile i did not tried, you can try) or else
It is showing 'zsh' in output. You have to add env properties in .zshrc file, if not exist already you create one no issue.
Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
Sun
  • 3,444
  • 7
  • 53
  • 83
11

Here is what worked for me.

First of all I checked if M2_HOME variable is set env | grep M2_HOME. I've got nothing.

I knew I had Maven installed in the folder "/usr/local/apache-maven-3.2.2", so executing the following 3 steps solved the problem for me:

  1. Set M2_HOME env variable

M2_HOME=/usr/local/apache-maven-3.2.2

  1. Set M2 env variable

M2=$M2_HOME/bin

  1. Update the PATH

export PATH=$M2:$PATH

As mentioned above you can save that sequence in the .bash_profile file if you want it to be executed automatically.

Oleg K
  • 464
  • 4
  • 8
4

steps to install maven :

  1. download the maven file from http://maven.apache.org/download.cgi
  2. $tar xvf apache-maven-3.5.4-bin.tar.gz
  3. copy the apache folder to desired place $cp -R apache-maven-3.5.4 /Users/locals
  4. go to apache directory $cd /Users/locals/apache-maven-3.5.4/
  5. create .bash_profile $vim ~/.bash_profile
  6. write these two command : export M2_HOME=/Users/manisha/apache-maven-3.5.4 export PATH=$PATH:$M2_HOME/bin 7 save and quit the vim :wq!
  7. restart the terminal and type mvn -version
Manisha Malik
  • 127
  • 2
  • 13
0

I followed brain storm's instructions and still wasn't getting different results - any new terminal windows would not recognize the mvn command. I don't know why, but breaking out the declarations in smaller chunks .bash_profile worked. As far as I can tell, I'm essentially doing the same thing he did. Here's what looks different in my .bash_profile:

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
export PATH JAVA_HOME
J2=$JAVA_HOME/bin
export PATH J2
M2_HOME=/usr/local/apache-maven/apache-maven-2.2.1
export PATH M2_HOME
M2=$M2_HOME/bin
export PATH M2
jareto
  • 1
0

You probably have 2 types of shell instances. sh vs zsh. Both can have different path defined. Check your PATH environment variable by typing the below line in terminal

echo $PATH

To test you can change shell mode -

sh to zsh -> type zsh and press enter in terminal (notice $ changes to %)

zsh to sh -> type sh/bash and press enter in terminal (notice % changes to $)

In Both shell modes check for PATH env. Make both same, or append path from other as needed.

Commands running in 1 shell and not in other would be sorted.

shubham chouhan
  • 580
  • 7
  • 8
0

For some of you the cause might be using of other variables in the path to the maven directory. More details in the answer.

Maksym Kosenko
  • 515
  • 7
  • 12