164

I have always difficulties setting the Environment variables in OS X like JAVA_HOME, M2_HOME or PATH. How to set these on OS X Mavericks?

I did some research but the answers I've found didn't really help me yet:

  1. export JAVA_HOME=/... (But it seems that the changes are temporary, just for one terminal session.)
  2. setenv JAVA_HOME=/... (But command not found.)
  3. Open .profile and write the variables inside to make the changes permanent- (.profile does not exist).
  4. Open .bash_profile and write the variables inside to make the changes permanent- (.bash_profile does not exist).
  5. vi ~/.bash_profile (Quite a challenge for somebody who doesn't know vi.)
  6. Creating your own enrivonment.plist file.

Can somebody please walk me through the steps to get that to work on OS X Mavericks, assuming no Unix knowhow?

user3439894
  • 58,676
akcasoy
  • 1,869

12 Answers12

68

I have a .profile in my home directory; it contains many export … statements for environment variables.

You can create such a file by opening a Terminal and issuing the command touch .profile.

Close Terminal.

Then you should open that file in a plain-text editor (TextWrangler for example). You can also use nano .profile in a Terminal window (current directory should be your home), which is much easier than vi. Insert lines such as export JAVA_HOME=…. Save, exit nano if you used that and quit a running Terminal.

Open Terminal and issue the command env to see all environment variables. Check that the ones you defined have the value you assigned to them. You should be good to go now but don't forget that environment variables defined in .profile are not passed to GUI applications.

Greenonline
  • 2,004
Bhas
  • 836
  • Thank you very much. This was what i really looking for. I now have set all my variables. What do u mean by GUI applications? I just needed some variables for java and maven in order to work with eclipse, spring tools etc. Eclipse is an application with GUI (Graphical User Interface). Do you mean this by GUI? – akcasoy Oct 25 '13 at 18:32
  • Ok. I have read some and i think you have mean really GUI with GUI.. Is there a way to make these variables available everywhere? What is the most common way or where is the most common location to define them then? – akcasoy Oct 25 '13 at 18:49
  • yes that is what I meant by a GUI app. 2. setting environment variables for GUI app's in OS X 10.8 appears to be quit difficult. Some apps let you define environment variables, which are to be passed to other applications, in their preferences for example. You can also use the open -a Appname method in a Terminal session.
  • – Bhas Oct 26 '13 at 09:37
  • 8
    I don't know why but for me (OS X Yosemite 10.10.1) the .profile didn't help. I had to put the export statements into the .bash_profile to make it work. Hopefully this helps someone else if he runs into the same problems... – chuky Dec 04 '14 at 19:50
  • @chuky if you're using zsh you should add them to .zprofile instead. – Ariel Mirra Jun 21 '22 at 21:39