104

I'd like to append to the global PATH environment variable on OS X so that all user shells and GUI applications get the same PATH environment.

I know I can append to the path in shell startup scripts, but those settings are not inherited by GUI applications.

The only way I found so far is to redefine the PATH environment variable in /etc/launchd.conf:

setenv PATH /usr/bin:/bin:/usr/sbin:/sbin:/my/path

I couldn't figure out a way to actually append to PATH in launchd.conf.

I'm a bit worried about this method, but so far this is the only thing that works. Is there a better way?

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
lajos
  • 1,315
  • 4
  • 14
  • 8
  • See: http://stackoverflow.com/questions/347286 – rjmunro Aug 25 '09 at 14:10
  • 2
    For Yosemite and later, see http://stackoverflow.com/questions/25385934/setting-environment-variables-via-launchd-conf-no-longer-works-in-os-x-yosemite/26586170#26586170 – thSoft Feb 05 '16 at 15:13
  • None of the answers here address the OP's question. But the comment here by thSoft is right on the money! Maybe this should be merged or linked to that referenced question: http://stackoverflow.com/questions/25385934/setting-environment-variables-via-launchd-conf-no-longer-works-in-os-x-yosemite/26586170#26586170 – Kevin Buchs Dec 22 '17 at 19:06

8 Answers8

73

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
Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
cogg
  • 831
  • 6
  • 3
  • 1
    Nice, thanks! Note: this requires you to log out and log back in before it works. – weronika Jan 04 '13 at 21:14
  • @weronika: I found it was sufficient to quit and re-launch `X11` (less painful than logging completely out). I didn't try it in `Terminal`. – Peter Gluck Feb 28 '13 at 22:18
  • I was referring to Terminal - you're right, I forgot to specify. – weronika Mar 01 '13 at 01:42
  • 10
    This is good to know, but it does NOT answer the OP's question in that this mechanism does not apply to GUI apps. The OP was looking for a *unified* solution that allows him to *append* to the default `$PATH`. – mklement May 31 '13 at 19:15
  • what do you mean by "my system" here, @cogg? Aren't we all using OS X here? Sorry but I'm still new to OS X. I'm using OS X 10.7 – ericn Jun 01 '13 at 09:37
  • Quite 7 years later. This does not work for me. – Dirk Schumacher May 21 '22 at 07:29
49

/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.

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
sorin
  • 7,668
  • 24
  • 75
  • 100
  • 7
    Great finding, but the question already talks about *launchd* and is asking on how to **append** to the path in it. – cregox Jul 11 '11 at 00:16
  • 22
    Did you really add a comment to the original question pointing to yourself as "the real answer"? – TJ Luoma Dec 09 '11 at 20:21
  • 7
    It's implied by your - helpful - answer, but just to spell it out: the OP's desire to *append* to the default path is not supported, because you can't reference other environment variables in `/etc/launchd.conf`. – mklement May 31 '13 at 19:27
  • It isn't global--the command line environment will discard this when /etc/profile calls path_helper and rebuilds PATH from /etc/paths and /etc/paths.d/ – Dave X Mar 31 '15 at 20:54
  • I'm on OS X 10.8, but creating `/etc/launchd.conf` and rebooting doesn't seem to change `$PATH`. I would like to move `/usr/local/bin` before `/usr/bin/`, but `/etc/path` and `/etc/paths.d` seem to take precendence... – ssc May 15 '15 at 21:08
  • 2
    according to http://apple.stackexchange.com/questions/106355/setting-the-system-wide-path-environment-variable-in-mavericks, this no long works for Yosemite or later – ericn Jul 22 '15 at 06:32
7

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.

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
palmer
  • 1,279
  • 9
  • 10
  • But the settings do take effect when set in /etc/launchd.conf... Hmmm... /me is confused. – Xerxes Jun 01 '09 at 14:59
  • Really? What environment variable are you setting there? – palmer Jun 01 '09 at 16:07
  • 2
    Just an update as of OS X 10.8: the `~/.MacOSX/environment.plist` approach is no longer supported and there's no substitute at the user level. System-wide you can use `/etc/launchd.conf` (and there's a per-*.app solution). – mklement May 31 '13 at 19:24
  • Does not answer OP's questions. – Kevin Buchs Dec 22 '17 at 18:47
3

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

sudo nano /etc/paths should get you there.

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
manumoomoo
  • 181
  • 1
  • 6
  • 2
    This is good to know, but it does not answer the OP's question in that this mechanism does not apply to GUI apps. – mklement May 31 '13 at 19:20
1

Have you had a look at the man page for the path_helper command-line utility on OS X? I answered a somewhat related question on SO that I think you may find helpful.

ayaz
  • 483
  • 3
  • 10
1

I am not sure if anyone covered the simplest and most elegant way. At least on Mac OS X v10.6 (Snow Leopard).

Messing with the [/etc/|~/.] of [profile|bashrc] files may work, but it's somewhat of a hack. The /etc/paths.d/ directory is the way to go:

  • Sudo into your favorite editor.
  • Create a new file named *name* (just don't name it something that's already in there) in /etc/paths.d/ with a path per line:

    /usr/X11/bin
    /Developer/usr/bin
    
  • Then add

    eval '/usr/libexec/path_helper -s`
    

    to profile or bashrc, and you should be good to go.

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
noboomu
  • 11
  • 2
0

I'm not sure if launchd accepts this, but try:

setenv PATH "$PATH:/my/path"
user1686
  • 8,717
  • 25
  • 38
  • 1
    Unfortunately that doesn't work. It doesn't look like launchctl has access to environment variables that easy. There is a command to get environment variables, but there's absolutely no documentation on how to use it. – lajos Jun 01 '09 at 03:18
0

I'm not sure why you'd use /etc/launchd.conf as opposed to /etc/profile - but I'm no expert in Mac OS X - I believe you in that I'm sure it works, but launchd is the Mac OS X implementation/replacement for init - Mac OS X confuses me.

Anyway, setenv PATH "$PATH:/more/paths:/and/more/paths" will work (tcsh), and the bourn shell equivalent is export PATH="$PATH:/more/paths:/and/more/paths" - I have no idea how launchd is related to a particular shell either.

I think I've asked more questions then I've answered =)

Xerxes
  • 4,133
  • 3
  • 26
  • 33
  • 4
    /etc/profile path settings are not seen by any of the OS X GUI apps. That only applies to bash. I want all applications to get the new path setting. – lajos Jun 01 '09 at 04:36
  • 1
    Unix? Sure, it's unix. I mean, sorta. Yea, we broke stuff that's worked for 30 years, but it's still unix! /sigh – Matt Simmons Jun 01 '09 at 12:48