544

When someone says "edit your .plist file" or "your .profile" or ".bash_profile" etc, this just confuses me. I have no idea where these files are, how to create them if I have to do that, etc, and also why there seem to be so many different ones (why? Do they do different things?)

So could someone please explain very patiently to a previous Windows user (wanting desperately to become more familiar with the pleasant if initially somewhat confusing OS X world) how to do this step by step?

I need the variables to be set both for GUI applications and command line applications, and at the moment it's for an ant script that needs the variables, but there will most likely be other needs as well.

Please note that I have Lion too, since many of the answers you get Googling seem to be outdated for Lion...

Also note that I have practically zero experience using the Terminal. I'm willing to learn, but please explain for a novice...

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
Anders
  • 12,556
  • 24
  • 104
  • 151

16 Answers16

406

First, one thing to recognize about OS X is that it is built on Unix. This is where the .bash_profile comes in. When you start the Terminal app in OS X you get a bash shell by default. The bash shell comes from Unix and when it loads it runs the .bash_profile script. You can modify this script for your user to change your settings. This file is located at:

~/.bash_profile

Update for Mavericks

OS X Mavericks does not use the environment.plist - at least not for OS X windows applications. You can use the launchd configuration for windowed applications. The .bash_profile is still supported since that is part of the bash shell used in Terminal.

Lion and Mountain Lion Only

OS X windowed applications receive environment variables from the your environment.plist file. This is likely what you mean by the ".plist" file. This file is located at:

~/.MacOSX/environment.plist

If you make a change to your environment.plist file then OS X windows applications, including the Terminal app, will have those environment variables set. Any environment variable you set in your .bash_profile will only affect your bash shells.

Generally I only set variables in my .bash_profile file and don't change the .plist file (or launchd file on Mavericks). Most OS X windowed applications don't need any custom environment. Only when an application actually needs a specific environment variable do I change the environment.plist (or launchd file on Mavericks).

It sounds like what you want is to change the environment.plist file, rather than the .bash_profile.

One last thing, if you look for those files, I think you will not find them. If I recall correctly, they were not on my initial install of Lion.

Edit: Here are some instructions for creating a plist file.

  1. Open Xcode
  2. Select File -> New -> New File...
  3. Under Mac OS X select Resources
  4. Choose a plist file
  5. Follow the rest of the prompts

To edit the file, you can Control-click to get a menu and select Add Row. You then can add a key value pair. For environment variables, the key is the environment variable name and the value is the actual value for that environment variable.

Once the plist file is created you can open it with Xcode to modify it anytime you wish.

Community
  • 1
  • 1
David V
  • 11,531
  • 5
  • 42
  • 66
  • Ok, thanks. I think I have understood that the tilde means my so called home folder, right (i.e. /Users/Anders in my case)? So if they do not exist, I should create a new empty text file, with the names you mentioned (will I be allowed to create a file that starts with a dot?)? And then add what in those files to set an environment variable? For instance, I need to add a variable AHF_OPT which should point to a folder needed by an ant script, how do I add that in these two files? – Anders Sep 21 '11 at 15:08
  • Yes, the tilde is your home directory. Do you have Xcode installed? It is the only application I know of for creating and editing plist files. – David V Sep 21 '11 at 15:10
  • I added some instructions for editing a plist file with Xcode. If you don't have Xcode, you can find an existing plist file and change it in any text editor. It is quite straight forward. – David V Sep 21 '11 at 15:18
  • Control-click inside the document to get a menu to add a row? Is that correct? Anyway, thanks I'll try it out. And if I understand your answer correctly, by doing this I wouldn't even have to set the bash_profile file? And this works in Lion too? I had read some comments that there were problems with the environment.plist file in Lion... – Anders Sep 21 '11 at 15:38
  • Yes, control-click. Or, if you have a trackpad, click with two fingers (even better!). You should not have to change the .bash_profile since Terminal gets the environment from the plist. In previous versions of OS X there was a problem where launching an application from spotlight where it did not load from the plist file, but it does in Lion. – David V Sep 21 '11 at 16:30
  • Something is not working... I created the directory .MacOSX as /Users/Anders/.MacOSX, and then in that directory I created the file environment.plist in XCode, setting the AXF_DIR variable (another variable that is more urgent than the other one). The resulting xml file looks like this: – Anders Sep 21 '11 at 20:31
  • AXF_DIR /Applications/Antenna/AHFormatterV53 But it doesn't work. The ant process still says it can't find a value for the environment variable – Anders Sep 21 '11 at 20:32
  • 1
    Two things to try: First, is your pasted string has an incorrect semicolon after ".dtd>" Maybe it is a copy and paste error, but I had to remove it to get Xcode to read the file. Second, you may need to log out and back in for the effects to take place. – David V Sep 21 '11 at 21:04
  • One good way to check what environment variables you have set, is to run set or env and pipe the result to grep. Here is an example: env | grep AXF – David V Sep 21 '11 at 21:08
  • Ok, yes the semicolon was just something that got added here, it isn't actually in the file. I tried the set command in Terminal, and it did in fact show the variable set, so that seems right. But then why doesn't the ant script find it? The ant script has references like this: ${env.AXF_DIR} and they have always worked with the environment variables in Windows, but now it says it isn't set... – Anders Sep 21 '11 at 22:00
  • +1 David V You *do* need to logout on back in for the changes to take effect. A pox on Apple for constructing this mess when regular old environment variables work fine. – nont Apr 17 '12 at 18:00
  • 9
    Also it's useful to know about `~/.bashrc`. `~/.bashrc` file runs every time you open a new non-login bash shell such as xterm / aterm, and `~/.bash_profile` runs only with login shells i.e when you first log in into system. – DanSkeel May 11 '12 at 13:55
  • 2
    Found a nice clear explanation about adding environment.plist here http://developer.apple.com/library/mac/#qa/qa1067/_index.html. – And Finally May 19 '12 at 09:28
  • 1
    Great answer, with the exception of the awkward phrase "OS X windows application." That's even worse than referring to X11 as "X Windows." Better to call it Aqua or just ".app applications". – SigmaX Nov 07 '13 at 22:36
226

Your .profile or .bash_profile are simply files that are present in your "home" folder. If you open a Finder window and click your account name in the Favorites pane, you won't see them. If you open a Terminal window and type ls to list files you still won't see them. However, you can find them by using ls -a in the terminal. Or if you open your favorite text editor (say TextEdit since it comes with OS X) and do File->Open and then press Command+Shift+. and click on your account name (home folder) you will see them as well. If you do not see them, then you can create one in your favorite text editor.

Now, adding environment variables is relatively straightforward and remarkably similar to windows conceptually. In your .profile just add, one per line, the variable name and its value as follows:

export JAVA_HOME=/Library/Java/Home
export JRE_HOME=/Library/Java/Home

etc.

If you are modifying your "PATH" variable, be sure to include the system's default PATH that was already set for you:

export PATH=$PATH:/path/to/my/stuff

Now here is the quirky part, you can either open a new Terminal window to have the new variables take effect, or you will need to type .profile or .bash_profile to reload the file and have the contents be applied to your current Terminal's environment.

You can check that your changes took effect using the "set" command in your Terminal. Just type set (or set | more if you prefer a paginated list) and be sure what you added to the file is there.

As for adding environment variables to GUI apps, that is normally not necessary and I'd like to hear more about what you are specifically trying to do to better give you an answer for it.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
tokkov
  • 2,959
  • 1
  • 13
  • 9
  • 2
    Thanks, I didn't quite understand the last part though, I either have to open a Terminal window to have them set, or type .profile? But wouldn't I have to open a Terminal window to type .profile? And if they are automatically set just by opening a Terminal window, then why do I need to type that...? And when I have opened one, will they be set permanently then? I probably misunderstood you, because not even my question makes sense to me... – Anders Sep 21 '11 at 15:36
  • 5
    If you have a Terminal window open prior to changing your profile, those changes will not get reflected in the existing Terminal's environment. Your quick solution is to just open a new Terminal window. That's all. Every new window will have everything in the .profile set on it. Always. The way to fix old, already open windows is to type ". .profile" 2 periods, 1 space between them. The "set" command is to set environment variables that die when you end the session you set them in. Using "set" without arguments lists the variables currently set. – tokkov Sep 21 '11 at 15:44
  • 1
    Ahhh, I see the confusion I created. If you had no terminal windows open, then each new window will load the .profile when it is opened and you need to do nothing. If on the other hand you had edited the .profile in your terminal window or had terminal windows open previously and wanted the changes in them, then you need to simply type `. .profile` to load them. – tokkov Sep 21 '11 at 15:53
  • 2
    I can't make it work. I added a file .bash_profile under /Users/Anders, with the line export AXF_DIR=/Applications/Antenna/AHFormatterV53, but the ant process still doesn't find a variable AXF_DIR... Also, I tried the set command in Terminal, and got a lot of info, but nothing about this variable. Note that I also restarted the system just to be sure, and I also started a new Terminal window... – Anders Sep 21 '11 at 20:35
  • 3
    Try naming it just `.profile`. I just peeked at a machine I recently bought and they seem to be using `.profile` these days and not `.bash_profile` – tokkov Sep 23 '11 at 13:20
  • 1
    Thanks for the answer. One small clarification. If you modified the .profile from a different location other than the user home, then run . ~/.profile for the changes to take affect in the current terminal. – picmate 涅 Apr 28 '17 at 21:27
  • I don't understand your question. Are you asking about the `. .profile` syntax? If so the first period is a synonym for the bash source command that causes the specified file, in this case .profile, to be run. It is the simplest way to refresh your profile in the current shell. Alternately you could just open a new terminal window. – tokkov Nov 06 '17 at 16:17
  • Thanks a BUNCH for `shift + command + .` in Finder. This is very useful to finally be able to browse hidden files – Jim Aho Nov 01 '18 at 21:04
77

Here's a bit more information specifically regarding the PATH variable in Lion OS 10.7.x:

If you need to set the PATH globally, the PATH is built by the system in the following order:

  1. Parsing the contents of the file /private/etc/paths, one path per line
  2. Parsing the contents of the folder /private/etc/paths.d. Each file in that folder can contain multiple paths, one path per line. Load order is determined by the file name first, and then the order of the lines in the file.
  3. A setenv PATH statement in /private/etc/launchd.conf, which will append that path to the path already built in #1 and #2 (you must not use $PATH to reference the PATH variable that has been built so far). But, setting the PATH here is completely unnecessary given the other two options, although this is the place where other global environment variables can be set for all users.

These paths and variables are inherited by all users and applications, so they are truly global -- logging out and in will not reset these paths -- they're built for the system and are created before any user is given the opportunity to login, so changes to these require a system restart to take effect.

BTW, a clean install of OS 10.7.x Lion doesn't have an environment.plist that I can find, so it may work but may also be deprecated.

jonphipps
  • 946
  • 7
  • 6
  • 1
    Yep, thanks a lot! This is a bit more involved than a simple .profile. – Felixyz Jul 19 '12 at 14:27
  • 2
    If you use the paths.d way and you have a path that contains folders containing spaces, just leave them unescaped. if you try to escape them (which is common practice i think?) it doesn't work (at least for me). i read this here:http://www.sweeting.org/mark/blog/2008/05/26/mac-os-x-tip-setting-path-environment-variables (search for 'spaces'). Hope this helps anyone else! – Michahell Nov 04 '12 at 15:19
  • 1
    @MichaelTrouw Your comment helped me a tonne! I was adding escapes for the spaces, but that wasn't required! I am on Mac OS 10.9.2 – kpsfoo Apr 15 '14 at 15:19
  • @jon, How does `paths.d` differ from `paths`? Also, does the former override the latter? – Pacerier Nov 02 '17 at 01:21
57
echo $PATH

it prints current path value

Then do vim ~/.bash_profile and write

export PATH=$PATH:/new/path/to/be/added

here you are appending to the old path, so preserves the old path and adds your new path to it

then do

source ~/.bash_profile

this will execute it and add the path

then again check with

echo $PATH
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
learner_19
  • 3,851
  • 1
  • 19
  • 8
31

Unfortunately none of these answers solved the specific problem I had.

Here's a simple solution without having to mess with bash. In my case, it was getting gradle to work (for Android Studio).

Btw, These steps relate to OSX (Mountain Lion 10.8.5)

  • Open up Terminal.
  • Run the following command:

    sudo nano /etc/paths (or sudo vim /etc/paths for vim)

    nano

  • Go to the bottom of the file, and enter the path you wish to add.
  • Hit control-x to quit.
  • Enter 'Y' to save the modified buffer.
  • Open a new terminal window then type:

    echo $PATH

You should see the new path appended to the end of the PATH

I got these details from this post:

http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.UkED3rxPp3Q

I hope that can help someone else

wired00
  • 13,930
  • 7
  • 70
  • 73
23

Simplified Explanation

This post/question is kind of old, so I will answer a simplified version for OS X Lion users. By default, OSX Lion does not have any of the following files:

  • ~/.bashrc
  • ~/.bash_profile
  • ~/.profile

At most, if you've done anything in the terminal you might see ~/.bash_history

What It Means

You must create the file to set your default bash commands (commonly in ~/.bashrc). To do this, use any sort of editor, though it's more simple to do it within the terminal:

  1. %> emacs .profile
  2. [from w/in emacs type:] source ~/.bashrc
  3. [from w/in emacs type:] Ctrl + x Ctrl + s (to save the file)
  4. [from w/in emacs type:] Ctrl + x Ctrl + c (to close emacs)
  5. %> emacs .bashrc
  6. [from w/in emacs type/paste all your bash commands, save, and exit]

The next time you quit and reload the terminal, it should load all your bash preferences. For good measure, it's usually a good idea to separate your commands into useful file names. For instance, from within ~/.bashrc, you should have a source ~/.bash_aliases and put all your alias commands in ~/.bash_aliases.

Community
  • 1
  • 1
vol7ron
  • 40,809
  • 21
  • 119
  • 172
19

What worked for me is to create a .launchd.conf with the variables I needed:

setenv FOO barbaz

This file is read by launchd at login. You can add a variable 'on the fly' to the running launchd using:

launchctl setenv FOO barbaz`

In fact, .launchd.cond simply contains launchctl commands.

Variables set this way seem to be present in GUI applications properly.

If you happen to be trying to set your LANG or LC_ variables in this way, and you happen to be using iTerm2, make sure you disable the 'Set locale variables automatically' setting under the Terminal tab of the Profile you're using. That seems to override launchd's environment variables, and in my case was setting a broken LC_CTYPE causing issues on remote servers (which got passed the variable).

(The environment.plist still seems to work on my Lion though. You can use the RCenvironment preference pane to maintain the file instead of manually editing it or required Xcode. Still seems to work on Lion, though it's last update is from the Snow Leopard era. Makes it my personally preferred method.)

Gilimanjaro
  • 391
  • 2
  • 8
  • By far the easiest answer. Note that you need to close the terminal app entirely and reopen it for any changes to take effect - not just close a single terminal window. – Timmmm Dec 24 '13 at 14:00
  • 1
    This answer is far too stripped down to be of use to OP. Create how? Create where? – HenryRootTwo Oct 05 '14 at 22:16
  • a clearer and more up-to-date answer: https://stackoverflow.com/questions/135688/setting-environment-variables-on-os-x/3756686#3756686 – Atorian Mar 10 '21 at 20:37
16

Setup your PATH environment variable on Mac OS

Open the Terminal program (this is in your Applications/Utilites folder by default). Run the following command

touch ~/.bash_profile; open ~/.bash_profile

This will open the file in the your default text editor.

For ANDROID SDK as example :

You need to add the path to your Android SDK platform-tools and tools directory. In my example I will use "/Development/android-sdk-macosx" as the directory the SDK is installed in. Add the following line:

export PATH=${PATH}:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools

Save the file and quit the text editor. Execute your .bash_profile to update your PATH.

source ~/.bash_profile

Now everytime you open the Terminal program you PATH will included the Android SDK.

Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
  • Thanks. Don't know why, but only this solution worked for me, not the ones above. – kroky Jan 08 '15 at 11:57
  • I added environment variable as you said but i cannot use it in xcode, do you have an idea ? I need to use it in header search path. Thanks in advance. – Yucel Bayram Dec 17 '19 at 11:46
11

Adding Path Variables to OS X Lion

This was pretty straight forward and worked for me, in terminal:

$echo "export PATH=$PATH:/path/to/whatever" >> .bash_profile #replace "/path/to/whatever" with the location of what you want to add to your bash profile, i.e: $ echo "export PATH=$PATH:/usr/local/Cellar/nginx/1.0.12/sbin" >> .bash_profile 
$. .bash_profile #restart your bash shell

A similar response was here: http://www.mac-forums.com/forums/os-x-operating-system/255324-problems-setting-path-variable-lion.html#post1317516

Community
  • 1
  • 1
Yaw Joseph Etse
  • 151
  • 1
  • 3
  • 1
    It's worth noting that you'll need to run this from the root of your home directory. Make sure that's where your at by issuing `cd` with no arguments or `cd ~/`. – ephsmith Jul 12 '12 at 01:42
  • 3
    There's also no need to restart the shell. Use `source .bash_profile`. – ephsmith Jul 12 '12 at 01:43
  • I like this method because it's short. But it does not seem to work. I did `echo "export PATH=$PATH:/Applications/XAMPP/xamppfiles/bin/mysql" >> .bash_profile` Then the only time I can do `mysql -u root -p` is if I am already running at root. Whereas I can use `Applications/XAMPP/xamppfiles/bin/mysql -u root -p` anywhere. I tried both mentioned restart approach: Yaw's and @ephsmith's. – kasavbere Sep 25 '12 at 19:56
  • Also, if I do ech $PATH as root (i.e. sudo su) I get `/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/XAMPP/xamppfiles/bin`. When I am not root I get `/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin` – kasavbere Sep 25 '12 at 19:59
  • The `$PATH` variable is user specific (root is a user). – ephsmith Sep 26 '12 at 01:39
  • @kasavbere, you can always hand edit your .bash_profile (or .profile or .bashrc). To open it in TextEdit.app, use `open ~/.bash_profile`. Keep in mind that directories in the `PATH` variable are prioritized left to right in the string. Place your preferred directories before others. – ephsmith Sep 26 '12 at 01:43
  • I did it following a tutorial online at `http://capstone.cs.gwu.edu/2010/kb071989/2010/09/21/getting-mysql-and-jdbc-working-on-my-mac/` – kasavbere Sep 26 '12 at 02:00
8

Open Terminal:

vi ~/.bash_profile

Apply changing to system (no need restart computer):

source ~/.bash_profile

(Also work with macOS Sierra 10.12.1)

Vy Do
  • 46,709
  • 59
  • 215
  • 313
5

I had problem with Eclipse (started as GUI, not from script) on Maverics that it did not take custom PATH. I tried all the methods mentioned above to no avail. Finally I found the simplest working answer based on hints from here:

  1. Go to /Applications/eclipse/Eclipse.app/Contents folder

  2. Edit Info.plist file with text editor (or XCode), add LSEnvironment dictionary for environment variable with full path. Note that it includes also /usr/bin etc:

    <dict>
      <key>LSEnvironment</key>
      <dict>
            <key>PATH</key>
            <string>/usr/bin:/bin:/usr/sbin:/sbin:/dev/android-ndk-r9b</string>
      </dict>
      <key>CFBundleDisplayName</key>
      <string>Eclipse</string>
      ...
    
  3. Reload parameters for app with

    /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.fra‌​mework/Support/lsregister -v -f /Applications/eclipse/Eclipse.app
    
  4. Restart Eclipse

Community
  • 1
  • 1
JaakL
  • 4,107
  • 5
  • 24
  • 37
5

Let me illustrate you from my personal example in a very redundant way.

  1. First after installing JDK, make sure it's installed. enter image description here
  2. Sometimes macOS or Linux automatically sets up environment variable for you unlike Windows. But that's not the case always. So let's check it. enter image description here The line immediately after echo $JAVA_HOME would be empty if the environment variable is not set. It must be empty in your case.

  3. Now we need to check if we have bash_profile file. enter image description here You saw that in my case we already have bash_profile. If not we have to create a bash_profile file.

  4. Create a bash_profile file. enter image description here

  5. Check again to make sure bash_profile file is there. enter image description here

  6. Now let's open bash_profile file. macOS opens it using it's default TextEdit program. enter image description here

  7. This is the file where environment variables are kept. If you have opened a new bash_profile file, it must be empty. In my case, it was already set for python programming language and Anaconda distribution. Now, i need to add environment variable for Java which is just adding the first line. YOU MUST TYPE the first line VERBATIM. JUST the first line. Save and close the TextEdit. Then close the terminal. enter image description here

  8. Open the terminal again. Let's check if the environment variable is set up. enter image description here

Sumit Pokhrel
  • 2,087
  • 24
  • 18
4

I took the idiot route. Added these to the end of /etc/profile

for environment in `find /etc/environments.d -type f`
do
     . $environment
done

created a folder /etc/environments create a file in it called "oracle" or "whatever" and added the stuff I needed set globally to it.

/etc$ cat /etc/environments.d/Oracle

export PATH=$PATH:/Library/Oracle/instantclient_11_2
export DYLD_LIBRARY_PATH=/Library/Oracle/instantclient_11_2
export SQLPATH=/Library/Oracle/instantclient_11_2
export PATH=$PATH:/Library/Oracle/instantclient_11_2
export TNS_ADMIN=/Library/Oracle/instantclient_11_2/network/admin
2

It is recommended to check default terminal shell before setting any environment variables, via following commands:

$ echo $SHELL
/bin/zsh

If your default terminal is /bin/zsh (Z Shell) like in my case (Personally prefer Z Shell), then you should set these environment variable in ~/.zshenv file with following contents (In this example, setting JAVA_HOME environment variable, but same applies to others):

export JAVA_HOME="$(/usr/libexec/java_home)"

Similarly, any other terminal type not mentioned above, you should set environment variable in its respective terminal env file.

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
1

More detail, which may perhaps be helpful to someone:

Due to my own explorations, I now know how to set environment variables in 7 of 8 different ways. I was trying to get an envar through to an application I'm developing under Xcode. I set "tracer" envars using these different methods to tell me which ones get it into the scope of my application. From the below, you can see that editing the "scheme" in Xcode to add arguments works, as does "putenv". What didn't set it in that scope: ~/.MACOS/environment.plist, app-specific plist, .profile, and adding a build phase to run a custom script (I found another way in Xcode [at least] to set one but forgot what I called the tracer and can't find it now; maybe it's on another machine....)

GPU_DUMP_DEVICE_KERNEL is 3

GPU_DUMP_TRK_ENVPLIST is (null)

GPU_DUMP_TRK_APPPLIST is (null)

GPU_DUMP_TRK_DOTPROFILE is (null)

GPU_DUMP_TRK_RUNSCRIPT is (null)

GPU_DUMP_TRK_SCHARGS is 1

GPU_DUMP_TRK_PUTENV is 1

... on the other hand, if I go into Terminal and say "set", it seems the only one it gets is the one from .profile (I would have thought it would pick up environment.plist also, and I'm sure once I did see a second tracer envar in Terminal, so something's probably gone wonky since then. Long day....)

Photovore
  • 135
  • 10
-1

Step1: open ~/.bash_profile

Now a text editor opens:

Step2: variable name should be in capitals. in this example variable is NODE_ENV

Step3: export NODE_ENV=development

Save it and close.

Restart your system.

Done.

To check env variable: open terminal and type

echo $NODE_ENV 
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
Navya
  • 47
  • 6