How do I set the global PATH environment variable on OS X?

Solution 1:

palmer's GUI information is correct, but there is a more maintainable way to modify the path seen by the shell. Like mediaslave said, you can edit /etc/paths, but even better you can drop a text file in /etc/paths.d/ that has a path in it and all shells will construct the path correctly.

For example, on my system:

$ cat /etc/paths
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
$ ls /etc/paths.d
X11       git      postgres
$ cat /etc/paths.d/postgres
/Library/PostgreSQL/8.4/bin
$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/Library/PostgreSQL/8.4/bin:/usr/X11/bin:/usr/local/mysql/bin

Solution 2:

/etc/launchd.conf

The launchd.conf file is **the only complete solution that will work for both command line and GUI applications on OS X v10.8 (Mountain Lion) and v10.9 (Mavericks), one that will work with GUI and console applications, for all users.

sudo touch /etc/launchd.conf
sudo nano /etc/launchd.conf

Add

setenv PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

In the example above I added /usr/local/bin to the default environment values for PATH.

Keep in mind that this file is not a script and you do not have the option to use substitutions. Also, to have these applied you need to reboot.

Remember, all others are only partial solutions:

  • environment.plist does not work for applications launched via Spotlight.
  • /etc/paths - only for console
  • /etc/csh.cshrc or /etc/bashrc - only for some shells

This answer is based on the same question from Setting environment variables on Mac OS X.

Solution 3:

You're going to have to set it on a shell-by-shell basis; Bash and csh-like shells do not share the same configuration files and syntax for adjusting the PATH.

Trying to do this in launchctl will not work, because environment variables are set on login; they do not exist system wide in Unix outside of a shell session.

So you're going to want to add

setenv PATH "$PATH:/add/my/extra/path"

to /etc/csh.cshrc and

export PATH="$PATH:/more/paths:/

to /etc/bashrc.

If you want environment variables in GUI applications, that's more complicated. You have to create a .MacOSX/environment.plist file in each user's home directory. The .MacOSX directory will likely not exist by default, so you'll have to create it.

The format of the file is like so:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

    <key>PRINTER</key>
    <string>myprinter</string>

    <key>PATH</key>
    <string>/path/to/thing/I/need</string>

    <key>DISPLAY</key>
    <string>0:1</string>

</dict>
</plist>

More on the environment.plist is on Apple's site.

Solution 4:

You can edit your global path by adding lines to /etc/paths, one path per line.

sudo nano /etc/paths should get you there.