How to set environment variables on OS X

bashenvironment-variablesterminal

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?

Best Answer

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.