55

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?

  • 1
    Unix (and any other OS) can be hard for new users so I can understand your frustration. Nevertheless the question as written is rather broad and won't bring the answers you are looking for. Can you please rewrite it to focus on the problem you are actually having (like "How do I set JAVA_HOME and PATH on Mavericks") which will ensure that you get good answers you can build on? – nohillside Oct 25 '13 at 13:46
  • As i see, Stuffe has already edited my question. Thanx. My question is like the title says, how to set JAVA_HOME and PATH on Mavericks. – akcasoy Oct 25 '13 at 13:50
  • @patrix: Next time, i will explain you my problem or what i want to ask, and you will write MY QUESTION WITH YOUR WORDS. OK? It seems that you moderators are very satisfied by doing this. – akcasoy Nov 16 '13 at 9:26
31

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.

  • 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
  • 1. 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 9:37
  • 4
    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 4 '14 at 19:50
21

In Yosemite, you should put export VARIABLE='something' inside .bash_profile.

I've tried writing the export VARIABLE='something' inside .profile without success.

  • Indeed. .profile works only when you manually execute it via source .profile But after a restart, the .profile isn't loaded. The .bash_profile is. – K.C. Feb 5 '15 at 8:39
  • not really correct. – Koray Tugay Apr 27 '15 at 20:41
  • 1
    @KorayTugay Could you point what is wrong, and maybe a way to fix it? – Rafael Eyng Apr 27 '15 at 21:19
7

For those who don't like to have the hassle with text files and editors, there is a GUI tool as well on GitHub.

  • Great ! I spent whole day figuring out to solve the environment variable issue and I was nowhere .The app that you posted solved all my environment hassles – Shajo Apr 22 '15 at 8:08
6

From http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac:

  • Open Terminal
  • Run touch ~/.bash_profile; open ~/.bash_profile
  • In TextEdit, add

    export PATH="$HOME/.rbenv/bin:$PATH"
    
  • Save the .bash_profile file and Quit (Command + Q) Text Edit.

  • Run source ~/.bash_profile
5

For adding a directory to a path, there is a better option in OS X: All entries in the file /etc/paths are added to the path, before any shell is started.

Edit this file with:

sudo pico /etc/paths

For more info, see: https://gist.github.com/Overbryd/1669348

  • Thank you! for me this is the most elegant way to include a new path – Asimov Oct 31 '16 at 21:54
4

OSX has had only one change re environment variables and that was in Lion where ~/.MacOSX/environment.plist was removed. Although there also has been a change in the default shell in 10.2 or 10.3 from tsch to bash.

You need to define what you want the enviroment variable set for and what environment you have.

You also need to understand shells and Unix which by your comments you have not fully done. There are several nbash tutorials around.

As to your points

  1. You understand correctly assuming you are in a sh type shell (e.g. bash or zsh)
  2. This is for C shells e.g. tcsh so won't work on recent defaults.
  3. You need to create .profile then it works
  4. It is .bash_profile or better .bashrc and you need to create the file first
  5. Totally correct :) Use TextEdit or nano (or emacs)
  6. Correct up to OSX 10.7 and the wy to do this for programs called from the Workspace e.g. from Dock or Finder (or open)

So 3 or 4 work if you are calling the program from the command line (or from a program started in the command line but not by open)

As for programs from the GUI see this question

As for internet is full of rubbish - you need to have enough background to understand an article because as you have discovered many assume things or are incorrect. StackExhnage sites should be better as you can see if an answer has been agreed to from the number of votes.

  • I still do not have any result. The linked question does not bring me to solution. I have a mac for 4 years, yet still do not have (didn't have to) experience about UNIX. I think, when the answer of "Why is it damn so hard?" is "plist was removed, change in 10.2 or 10.3, sh type shell, up to 10.7 etc.." the answer contradicts with itself. – akcasoy Oct 25 '13 at 13:37
  • It could be made very easily like in windows. Anyway.. what i was really expecting was sth. like this: 1- open home dir 2- open terminal 3- write "xxx" 4-click save etc.. one must not have enough background to set just a simple variable, at least could find a proper solution from those boilerplate answers on the internet. – akcasoy Oct 25 '13 at 13:41
  • If you need environment variables you have to understand what they are doing and that requires background. Also as you did not state what you were using the variables for I have to give all the possible differences. Note for Java setting JAVA_HOME is not necessarily the best way (see Java questions) – Mark Oct 27 '13 at 17:25
  • My question was about SETTING the variables. Neither about what they are doing, nor about any other thing which requires more info. You can obviously set them even without background. Thank you for your answer though. But as i already wrote, a simple proposal for solution was enough, which i already have thanks to Bhas. – akcasoy Oct 30 '13 at 22:37
  • You really should not set variables without background you have to know what they do – Mark Oct 30 '13 at 22:55
1

In case you're using zsh like me, you need to modify ~/.zshrc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .