I am new to Ubuntu and Maven. Recently I installed Maven in my system as user and the installation went perfectly, but I can't set path environment variable permanently. I also referred to the following questions but I couldn't solve my problem.

Maven environment variable not working on other terminal

How to permanently set environmental variables PATH and M2_HOME in ubuntu for maven3?

I can set m2_home path in my system as user but when I check if the path is set with mvn -v I get the following error:

Error: JAVA_HOME is not defined correctly. We cannot execute /usr/lib/jvm/jdk1.8.0/bin/java

When I restart the terminal I can't find the path of M2_home. I don't have root access.

  • 1
    Which one of shell you use? If bash, you can set environment variables via .bashrc file.
    – mariaczi
    Commented Jul 4, 2018 at 13:46
  • Linux based systems are case sensitive: M2_HOME, m2_home and M2_home are 3 different variables. Double check that Commented Jul 4, 2018 at 16:51
  • @EskanderBejaoui you are right it is case sensitive. there are gramer mistake in my question only not in command which i tried.
    – mak
    Commented Jul 5, 2018 at 6:55

3 Answers 3


Exporting the M2_HOME environment variable is no longer required if you're using Maven 3. The important thing is to have the /bin directory of you maven installation added to the PATH environment variable.

Anyway, personally I find that exporting the M2_HOME env variable makes reconfiguring the location easier at a later time if needed.

The actual error you're reporting is related to JAVA_HOME not being set correctly, but I'll get to that after setting up Maven.

First of all, to have M2_HOME set between terminal restarts you'll have to add the export statement to ~/.bashrc (assuming your shell is bash). This will override any of these environment variables set system wide by /etc/environment

My Maven installation is located at /opt/apache-maven-3.5.4, so to add that to my ~/.bashrc file I could do

echo 'export M2_HOME=/opt/apache-maven-3.5.4' >> ~/.bashrc

And then I would utilize M2_HOME when I add maven to PATH, like so

echo 'export PATH=${M2_HOME}/bin:${PATH}' >> ~/.bashrc

Now configure JAVA_HOME the same way. My java installation is located at /usr/lib/jvm/java-8-oracle/jre/bin/java but when configuring JAVA_HOME some of that path will need to be stripped away.

echo 'export JAVA_HOME=/usr/lib/jvm/java-8-oracle' >> ~/.bashrc

Then, if you start a new terminal you should be able to run mvn -v and get the desired result.

  • i just fourgot to tell that my java is perfactly running without any error...befour ant after maven installatin. but just maven not working
    – mak
    Commented Jul 5, 2018 at 7:03
  • Could be, but the actual error message from Maven is that JAVA_HOME is not correctly defined. What is the output of echo $JAVA_HOME
    – Misantorp
    Commented Jul 5, 2018 at 7:06
  • it is give me path of java installed path
    – mak
    Commented Jul 5, 2018 at 8:11
  • i just try this solution but after i exit terminal and restart terminal,after check $M2_HOME .....its not seted yet
    – mak
    Commented Jul 5, 2018 at 8:17
  • thankyou and done....using this solution after root give some access....
    – mak
    Commented Jul 5, 2018 at 8:30

Setup Maven and Specific Java version for particular user on Ubuntu 16.04. Download Apache Maven 3.3.6, Java 1.8 and extract them to certain directories. Do following steps:

  1. open terminal and Go to Particular User
  2. gedit ~/.profile
  3. Add below lines export JAVA_HOME=/usr/local/java/jdk1.8.0_251 export M2_HOME=/usr/local/maven/apache-maven-3.3.9 PATH="$HOME/bin:$HOME/.local/bin:$PATH:$JAVA_HOME/bin:$M2_HOME/bin"

  4. Save the Changes

  5. source ~/.profile

Then Run Command to verify mvn -version

It is working for me.


After Maven Install, If you doesn't accessible globally in Ubuntu 22. then you need to set exporting the M2_HOME env variable makes.

My Maven installation is located at /opt/apache-maven-3.6.3.

echo 'export M2_HOME=/opt/apache-maven-3.6.3' >> ~/.bashrc
echo 'export PATH=${M2_HOME}/bin:${PATH}' >> ~/.bashrc

NOTE :- after add path in .bashrc file you need to restart your terminal. then try run mvn -version command for verify


Your can Manaully Add below 2 lines (exporting path) in .bashrc file

export M2_HOME=/opt/apache-maven-3.6.3
export PATH=${M2_HOME}/bin:${PATH}

Additional Info : for JDK & Maven Installation steps you can follow this blog

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .