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
  • 19,395
  • 55
  • 76
  • 81
Gaurav Dighe
  • 722
  • 3
  • 11
  • 21
  • Take a look at [This](http://www.coderanch.com/t/563245/Linux-UNIX/Setting-PATH-Ubuntu) – Mitch Mar 29 '13 at 07:08

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
Connor Brinton
  • 331
  • 1
  • 4
  • 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 02: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 02: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 02: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 06 '17 at 07:12
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
  • 53,609
  • 102
  • 137
  • 162