30

I have downloaded the latest apache-maven3.zip file and extracted it to the folder: /home/gaurav/Java/maven3.

I don't know how to set the environmental variables for maven - such as PATH and M2_HOME.

I tried below things:

export M2_HOME=/home/gaurav/Java/maven3

export PATH= /home/gaurav/Java/maven3/bin:${PATH}

After setting that, I ran mvn --version and it is running correctly.

But when next time I start my machine, and type $M2_HOME, its not showing me the details of the path variables, neither mvn --version is getting executed.

Please help me to resolve this problem of permanently setting environment variables in Ubuntu.

Kevin Bowen
  • 20,055
  • 57
  • 82
  • 84

2 Answers2

22

Update: Eliah pointed out to me that if you are not dynamically building your environment variables, you should store them in /etc/environment. To set M2_HOME and add the bin directory to your PATH, you would modify your /etc/environment as follows. Make sure that you don't just copy/paste, because your /etc/environment file might have a different PATH variable than mine does.

M2_HOME="/home/gaurav/Java/maven3"
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/gaurav/Java/maven3/bin"


Alternative (not as recommended) method: Like Mitch said, you'll need to edit a configuration file to permanently change your PATH. I chose to edit my /etc/profile configuration file, because it applies system-wide. To edit this file, run sudo nano /etc/profile Here's the relevant excerpt from my configuration file:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

JAVA_HOME=/usr/lib/jvm/java-6-oracle/
export JAVA_HOME

M2_HOME=/usr/local/apache-maven/apache-maven-3.0.4
export M2_HOME
M2=$M2_HOME/bin
export M2

PATH=$PATH:$JAVA_HOME
PATH=$PATH:$M2
export PATH
3

You have to add your PATH to /etc/bash.bashrc as root.

From root do these steps:

  1. sudo nano /etc/bash.bashrc
  2. At the end of the file, add the following line:

    PATH=/home/computer/application/bin:$PATH  
    

This is just a pseudo address. Change it according to the address that you want and add the :$PATH after it.

This is for Ubuntu.

Eric Carvalho
  • 55,453