49

In Lion (and previous releases) it was in .MacOSX/environment.plist. After upgrading to 10.8, the file is still there, but the variables aren’t taking.

I’ve also tried adding them to /etc/launchd.conf and they’ll show up in Terminal, but not GUI applications (like my IDE).

Zearin
  • 321
  • 1
  • 2
  • 14
Felix
  • 829
  • 1
  • 7
  • 9

5 Answers5

26

I know this is an old question but I thought I would point out that in Yosemite /etc/launchd.conf no longer works (as confirmed by the man page). So here is an alternative.

Launch AppleScript Editor, enter a command like this:

do shell script "launchctl setenv variablename value"

(add as many lines as you like)

Now save (⌘S) as File format: Application. Finally open System SettingsUsers & GroupsLogin Items and add your new application.

grg
  • 201,078
ruario
  • 361
  • 3
  • 4
  • Note that it might nor work with other apps that are opened as Login items and perhaps those you open quickly after login – mmmmmm Sep 20 '14 at 17:04
  • This worked for me. Thank you so much. This has been driving crazy all day! – wholladay Oct 22 '14 at 02:06
  • 2
    Looks like the only real&universal solution so far. But it needs 9 more upvotes to go up and become first answer. :c – Display Name Feb 17 '15 at 18:41
  • This worked for me on El Capitan.

    As noted above by @Mark, there are issues with ordering (eg. resumed applications may or may not have the environment), but once logged in this works as you would expect. That is, applications started from Spotlight or the Dock have the correct environment.

    – Alec Thomas Mar 02 '16 at 23:49
  • @Mark You can try this answer, making use of the Launch Agent. In my case the Launch Agent starts after the Login Items, so I move the target application (like Eclipse) from Login Item to Launch Agent, so that they will be started after this special application setting environment variables. – Franklin Yu May 22 '17 at 05:08
  • Thank you! This is still working in Mojave. It's a pity Apple doesn't provide a simple solution for that. – Sky Mar 29 '19 at 19:24
  • Or just create a bash script with just the part "launchctl *" and add it to Login Items – SwiftMango Jun 20 '22 at 23:59
23

I asked this question on the Apple Developer Forums as well and got back this, official response.

Change the Info.plist of the .app itself to contain an "LSEnvironment" dictionary with the environment variables you want.
~/.MacOSX/environment.plist is no longer supported.

Felix
  • 829
  • 1
  • 7
  • 9
  • 3
    Where is that located? – orome Dec 12 '12 at 19:10
  • 1
    It would be useful to provide a link to a more specific example. – Blaisorblade Feb 02 '13 at 04:41
  • 1
    Update: I tried changing that file, after figuring out the syntax (I think), but it does not work at all for me. – Blaisorblade Feb 02 '13 at 05:04
  • 4
    There will be an Info.plist per application in WhatEver.app/Content/Info.plist. You can edit it with XCode. Click the "+" on the Information Property List and add a dictionary called LSEnvironment. If you type it correctly it will rename it to Environment variables. Then you click the "+" by that for each environment variable you want to add. – Felix Feb 06 '13 at 01:28
  • 2
    This is a nice solution, but it no longer works as of Yosemite 10.10.5. – Dalmazio Oct 28 '15 at 18:13
  • the solution about Info.plist of the .app itself doesn't work at 10.14.6 mojave – Alexey Sh. Aug 29 '19 at 09:10
10

As you have discovered, the use of environment.plist file is no longer followed, the variables stored in Info.plist as LSEnvironment strings are only set by launchd.

You cannot depend on them to set a general environment variable that the terminal will set if you call your program from the terminal/shell directly. The good news is the open -a command does trigger launchd to do the launching, so the variables will be set consistently for the app environment if not for the local shell environment.

bmike
  • 235,889
4

Setting environment variables in /etc/launchd.conf is the way to go: Every application launched after a reboot by local shells, Finder or Spotlight inherits these variables - I have tested this extensively with Mountain Lion 10.8.3.

2 caveats:

halloleo
  • 1,345
  • 2
    /etc/launchd.conf is no longer supported since Yosemite. See https://apple.stackexchange.com/questions/106355/setting-the-system-wide-path-environment-variable-in-mavericks/106814#106814 – Franklin Yu May 22 '17 at 02:45
3

Try launchctl setenv NAME VALUE. I don't know if/what it does differently from editing launchd.conf, but it works for me in both Terminal and GUI apps. More details.

Note this doesn't survive across a reboot. Here's a way to fix that.

ryan
  • 1,448
  • 3
    That's quite a long page you are linking to here. Where exactly is the relevant part? Could you include it directly into your answer to make it easier for everybody? – nohillside Mar 17 '13 at 08:24
  • 2
    oops, good point, thought i'd included more of a fragment in that url. fixed. – ryan Mar 18 '13 at 14:30
  • 1
    The way to survive across a reboot given there uses the environment.plist which stopped working under Lion – mmmmmm Sep 20 '14 at 17:06