138

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
  • 1
    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

10 Answers 10

62

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.

5
  • 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
  • 7
    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
  • @chuky if you're using zsh you should add them to .zprofile instead. Jun 21 at 21:39
36

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

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

1
  • if you're using zsh you should add them to .zprofile instead. Jun 21 at 21:39
33

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

5
  • 3
    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
32

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
1
  • This assumes that the user is using Bash as their shell. It will not work for alternative shells such as Fish Shell. May 27 at 7:41
10

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

1
  • Thank you! for me this is the most elegant way to include a new path
    – Asimov
    Oct 31, 2016 at 21:54
9

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

1
  • 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
8
launchctl setenv environmentvariablename environmentvariablevalue

or

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.

3
  • 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
  • This is the only answer (to date) that will set the environment variable for all shells AND for applications! Thank you!
    – baskren
    May 25 at 13:38
5

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.

5
  • 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
0

Background Knowledge

  • Mac's OS=Operation System

    • old ones called: OS X
      • such as your OS X Mavericks
    • now called: macOS
  • for any macOS, normally have built-in a console application, called Terminal

    • look like this (My here is Chinese name: 终端)
      • enter image description here

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

Steps to add environment variable in MacOS

Simple

  • use terminal to add export JAVA_HOME=/your/path to your launch script
    • bash's ~/.bash_profile
    • or zsh's ~/.zshrc

Detail

open terminal

Launch Pad -> Terminal

look like this:

enter image description here

prepare boot script

then make sure you already have your boot script

for basic and common case is:

you are using bash, related boot script is ~/.bash_profile

➜  ~ ls -lh ~/.bash_profile 
-rw-r--r--  1 crifan  staff   461B 10 28  2021 /Users/crifan/.bash_profile

if can not found, you can create one:

touch ~/.bash_profile

add your environment variable into your boot script

then you can edit the boot script ~/.bash_profile

using text editor, such as vi / vim / nano / echo

here use most simple:

echo "export JAVA_HOME=/your/path" >> ~/.bash_profile

here is my output for you refer:

(using cat to print file content to verify is ok)

➜  ~ cat ~/.bash_profile 
export THEOS=/opt/theos
export PATH=$PATH:$THEOS/bin

export THEOS_MAKE_PATH=$(THEOS)/makefiles
export THEOS_BIN_PATH=$(THEOS)/bin
export THEOS_LIBRARY_PATH=$(THEOS)/lib
export THEOS_VENDOR_LIBRARY_PATH=$(THEOS)/vendor/lib
export THEOS_INCLUDE_PATH=$(THEOS)/include
export THEOS_VENDOR_INCLUDE_PATH=$(THEOS)/vendor/include
export THEOS_FALLBACK_INCLUDE_PATH=$(THEOS)/include/_fallback
export THEOS_MODULE_PATH=$(THEOS)/mod
export THEOS_SDKS_PATH=$(THEOS)/sdks

➜  ~ echo "export JAVA_HOME=/your/path" >> ~/.bash_profile 
➜  ~ cat ~/.bash_profile                                  
export THEOS=/opt/theos
export PATH=$PATH:$THEOS/bin

export THEOS_MAKE_PATH=$(THEOS)/makefiles
export THEOS_BIN_PATH=$(THEOS)/bin
export THEOS_LIBRARY_PATH=$(THEOS)/lib
export THEOS_VENDOR_LIBRARY_PATH=$(THEOS)/vendor/lib
export THEOS_INCLUDE_PATH=$(THEOS)/include
export THEOS_VENDOR_INCLUDE_PATH=$(THEOS)/vendor/include
export THEOS_FALLBACK_INCLUDE_PATH=$(THEOS)/include/_fallback
export THEOS_MODULE_PATH=$(THEOS)/mod
export THEOS_SDKS_PATH=$(THEOS)/sdks

export JAVA_HOME=/your/path
➜  ~ 

[optional] make variable take effect immediately

normally above is ok. but if you want to let your added environment variable take effect immediately, you can use:

source ~/.bash_profile

then can use echo to verify worked

➜  ~ source ~/.bash_profile 
....
➜  ~ echo $JAVA_HOME
/your/path

appendix

the full log for refer:

➜  ~ ls -lh ~/.bash_profile 
-rw-r--r--  1 crifan  staff   461B 10 28  2021 /Users/crifan/.bash_profile
➜  ~ cat ~/.bash_profile 
export THEOS=/opt/theos
export PATH=$PATH:$THEOS/bin

export THEOS_MAKE_PATH=$(THEOS)/makefiles
export THEOS_BIN_PATH=$(THEOS)/bin
export THEOS_LIBRARY_PATH=$(THEOS)/lib
export THEOS_VENDOR_LIBRARY_PATH=$(THEOS)/vendor/lib
export THEOS_INCLUDE_PATH=$(THEOS)/include
export THEOS_VENDOR_INCLUDE_PATH=$(THEOS)/vendor/include
export THEOS_FALLBACK_INCLUDE_PATH=$(THEOS)/include/_fallback
export THEOS_MODULE_PATH=$(THEOS)/mod
export THEOS_SDKS_PATH=$(THEOS)/sdks

➜  ~ echo "export JAVA_HOME=/your/path" >> ~/.bash_profile 
➜  ~ cat ~/.bash_profile                                  
export THEOS=/opt/theos
export PATH=$PATH:$THEOS/bin

export THEOS_MAKE_PATH=$(THEOS)/makefiles
export THEOS_BIN_PATH=$(THEOS)/bin
export THEOS_LIBRARY_PATH=$(THEOS)/lib
export THEOS_VENDOR_LIBRARY_PATH=$(THEOS)/vendor/lib
export THEOS_INCLUDE_PATH=$(THEOS)/include
export THEOS_VENDOR_INCLUDE_PATH=$(THEOS)/vendor/include
export THEOS_FALLBACK_INCLUDE_PATH=$(THEOS)/include/_fallback
export THEOS_MODULE_PATH=$(THEOS)/mod
export THEOS_SDKS_PATH=$(THEOS)/sdks

export JAVA_HOME=/your/path
➜  ~ source ~/.bash_profile 
...
➜  ~ echo $JAVA_HOME
/your/path

enter image description here

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jun 14 at 11:09
  • Also how does it improve on other answers. Also either the OP wants to chnage things in the terminal which then makes most of this answer not useful as they know how to get to terminal or the OP wants to set variables for a GUI app and in that case you do not answer.
    – mmmmmm
    Jun 14 at 11:26
-2

Open terminal

COMMAND-1

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 .