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.


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.


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), ...).

export JAVA_HOME

export M2_HOME
export M2

export PATH
  • 1
    Any reason not to just put the JAVA_HOME, M2_HOME, and M2 definitions in /etc/environment, instead? – Eliah Kagan Apr 13 '13 at 2:39
  • That's definitely the semantically correct thing to do (because /etc/environment is the recommended place to store system-wide environment variables). But I just wanted to keep all of my environment variables in the same place. If there was a way I could dynamically build my PATH variable using only /etc/environment, I would immediately switch. – Connor Brinton Apr 13 '13 at 2:53
  • 1
    Since that's your motivation, you might want to add using /etc/environment as an alternative, considering that the goal of putting all your environment variable assignments in the same place is not necessarily this OP's goal or the goal of most other people who come by this question. Also, you may want to re-examine that goal: Most of the time, environment variables should be added at the user account level, to affect only a single user. (Then they can go in ~/.pam_environment or ~/.profile.) It's true that some environment variable assignments are dynamic and must be in scripts. – Eliah Kagan Apr 13 '13 at 2:56
  • @connor.brinton though I said the path using the above method ( editing the /etc/environment) my path is still not set – Kasun Siyambalapitiya Jan 6 '17 at 7:12

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:


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.