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?

  • For macOS Catalina and greater (Mojave, Catalina, Big Sur) you have to set them inside ~/.zshrc as shown here. Nov 4, 2021 at 0:07

9 Answers 9


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, 2013 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, 2013 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, 2013 at 9:37
  • 6
    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, 2014 at 19:50

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

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


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

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

  • 2
    Or in .zprofile
    – timetofly
    May 16, 2019 at 12:58
  • Some of my application add something to PATH, but I didn't see the ~/.zshrc or .zprofile you said? @timetofly
    – Raining
    Apr 16, 2020 at 6:57
  • You can try creating that file in your home directory and see if zsh picks it up.
    – timetofly
    Apr 17, 2020 at 17:27
  • 3
    Starting with macOS Catalina, Macs use zsh as the default login shell and interactive shell - support.apple.com/en-us/HT208050 Nov 20, 2020 at 11:18
  • yes!! thank you sir!
    – ennth
    Feb 13, 2021 at 21:28

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, 2016 at 21:54

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, 2015 at 8:08
launchctl setenv environmentvariablename environmentvariablevalue


launchctl setenv environmentvariablename `command that will generate value`

use proper ` and remember to restart application or terminal for the environment variable to take effect.

you can check environment variable by printenv command.

  • 1
    thanks, this works Oct 1, 2021 at 13:05
  • 2
    it works! in VSC you have to close all windows to actually restart the terminal.
    – GoTo
    Oct 20, 2021 at 20:10

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, 2013 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, 2013 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)
    – mmmmmm
    Oct 27, 2013 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, 2013 at 22:37
  • You really should not set variables without background you have to know what they do
    – mmmmmm
    Oct 30, 2013 at 22:55

Open terminal


input : echo $SHELL output : /bin/zsh

if your machine uses zsh then use below commands|

Command - 2

vi ~/.zshrc

it will open new file or edits existing file.

now add below line and press esc and enter :wq to save and close the file. export JAVA_HOME=$(/usr/libexec/java_home)

Command 3:: to apply the changes

source ~/.zshr

Command 4::

echo $JAVA_HOME /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

you will see output as per above line.

You must log in to answer this question.

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