0

I need to set a number of Environment variables that can be seen by GUI apps (i.e. Xcode). When running from a terminal this is trivial and is simply a case of adding a. number of export commands to my .bash_profile e.g.

export BOOST_INCLUDE=~/boost/boost-XXXX/boost

Unfortunately GUI apps do not see values set in .bash_profile. I am using Catalina and it appears that Apple modifies the mechanism every time MacOs is updated. Many online guides are out of date....

This article: http://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-mac-os-x-launchd-plist/ suggests that launchctl should do what I need but even the following simple example does not work:

In a terminal:

/bin/launchctl setenv FOO BAR

Then close the terminal and open a new one

Check the value:

echo $FOO

The output should be

BAR

But it is unset. Note it is also unset in the original terminal so I am not convinced that the command /bin/launchctl setenv FOO BAR is doing anything.

Note: This is about setting the environment variable for GUI apps NOT just for the terminal.

5
  • Do you need to set these variables particularly in Xcode? Because there's more specific way to set env vars in Xcode
    – Olha
    Apr 21 '20 at 10:01
  • I am looking for a way to set the variables such that I do not need to manually set them on a per project basis. My current work around is to use some symbolic links point to the latest version of the 3rd part library I am using (e.g. boost or range-v3). That way in the Xcode projects I can use the links and adjust them when I upgrade the library,
    – mark
    Apr 21 '20 at 19:22
  • Have you tried starting Xcode from Terminal like this: open /Applications/Xcode.app? Xcode should inherit variables set in that shell.
    – jaume
    Jun 29 '20 at 12:30
  • @jaume are you sure that works. Open will launch from desktop
    – mmmmmm
    Jun 29 '20 at 13:40
  • @mmmmmm The folder from where you run open in Terminal is not relevant as long as the environment variables are set. The OP should confirm whether it works for him.
    – jaume
    Jun 29 '20 at 13:43
4

Run launchctl setenv FOO BAR in Terminal.app, then quit (Cmd-Q) and restart Terminal.

7
  • That is exactly what the OP says he has done and that it does not work
    – mmmmmm
    Jun 29 '20 at 13:41
  • OP says a terminal, I say Terminal.app.
    – Damien L
    Jun 29 '20 at 13:45
  • 1
    Ah you need to emphasise the difference
    – mmmmmm
    Jun 29 '20 at 13:46
  • Given your question apple.stackexchange.com/q/394979/237 will this work for xcode as the op asks?
    – mmmmmm
    Jun 29 '20 at 13:54
  • No idea, this is why I formulated my answer as Try [...] but it was then reformulated to @nohillside. What I am sure is that setting global variables has never worked for me in iTerm but it has worked using Terminal.app. I hope Xcode is just like other GUI apps.
    – Damien L
    Jun 29 '20 at 14:12
0

As far as I know “environment variables” set via launchctl are not visible in the shell and vice versa. Therefore I set some variables with launchctl and as shell variables (in my shell init file), if I need them in both, e.g. JAVA_HOME

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .