36

It looks like xcode's $PATH environment setting is different from my user shell environment.

Where does xcode get the $PATH setting from and what's the best way to append to the search path?

lajos
  • 25,525
  • 19
  • 65
  • 75
  • If XCode can't find watchman or node installed by homebrew, can do this trick: `sudo ln -s /opt/homebrew/bin/{watchman,node} /usr/local/bin` – GutenYe Mar 29 '23 at 01:59

10 Answers10

23

if you're writing a Run Shell Script build phase, you can just do:

PATH=${PATH}:/opt/local/bin

or whatever inside the script content.

13

There's some confusion in these answers, as some of them are trying to solve the $PATH for the built executable being run by Xcode. But the question is about Xcode, implying that it's about the build process itself.

For example, in a Build Phase Run Script step that runs an executable installed by Homebrew. It's not a good idea to hard-code the build process to include a path that is specific to one build machine (New macOS versions come out, new developers join the team, etc.)

The problem has multiple layers:

Changing $PATH in bashrc/zshrc/profile takes effect on shell sessions, but not in macOS applications

To solve this, you can set the PATH for applications using:

sudo launchctl config user path $PATH

You will then need to restart your machine for the change to take effect. You will need to run this again if you change your $PATH.

(This came from a comment on GitHub.)

Xcode by default does not use the system $PATH, and replaces it with its own sanitized value

This is solved by changing a User Default. This probably has some risk, since Xcode does this sanitization to ensure that its own build tools are used, and if you have executables with the same name in other places, they might be run instead. Caveat emptor!

defaults write com.apple.dt.Xcode UseSanitizedBuildSystemEnvironment -bool NO

And it looks like this gets reset on every restart of macOS, so be prepared to issue this command every time you restart.

(This part came from this answer.)

Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
8

The easiest solution is to add the PATH variable in Xcode.

PATH=${PATH}:/usr/local/bin

enter image description here

Skwiggs
  • 1,348
  • 2
  • 17
  • 42
ABCD
  • 7,914
  • 9
  • 54
  • 90
7

This applies for OSX 10.7 and earlier ONLY.

XCode gets its environment variables the same way as other OS X processes, from ~/.MacOSX/environment.plist.

Check developer.apple.com/qa/qa2001/qa1067.html for details on how to set things.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
4

In Xcode 5 you can add your PATH as a variable to either a target or the project settings.

  1. Add a custom variable with the +sign on the top of the page
  2. Edit the name of the variable to be PATH and add your preferred value (e.g. /usr/local/bin for a default install of homebrew.

Target Build Settings

GhostLyrics
  • 150
  • 5
  • 4
    I just tried this solution with Xcode 6, and it seems that the IDE silently overwrites the PATH variable with values to the Xcode.app folder. So this does not work anymore. Is there any alternative solution or a way to force Xcode to use the PATH set here? – naich May 31 '15 at 20:10
  • 1
    Xcode 6 adds its own tools' path before yours. So, for example, if you say you want PATH to be `/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin`, Xcode will actually set it to `/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin`. This is unfortunate if you want your PATH to override Xcode's, but actually is a good solution if you just want to add `/usr/local/bin` or some other directory to the PATH. – Kristopher Johnson Aug 14 '15 at 00:45
2

This is an update for later versions of macOS and Xcode as things have altered. This is with Xcode 11.0 and macOS 10.14

The biggest issue is that ~/.MacOSX/environment.plist does not get read now.

  1. Build Settings

This means that if in the build you need the PATH set, e.g. for external builds and they run executables there is no simple solution. /etc/paths does not seem to be read either.

The solution is as in @GhostLyrics answer to add the PATH variable in Build Settings. However as noted in comments Xcode will not just use that value but it puts its own values before that. Also it does a straight textual substitution and so you need to also add the separator that PATH uses i.e. the : (colon). The value I have added is :opt/local/bin I also found that you can only do this for a target and not at the project level.

  1. Run Shell Script

This is the simple case as in this answer

PATH=${PATH}:/opt/local/bin

or whatever inside the script content.

Alternatively put this change in your non login shell starter file e.g. ~/.bashrc ~/.zshrc

  1. Running the executable

This is done in the Schema in the Run portion. Set PATH in the environment variables as stated in answers here. Note I have not tried this and I am not certain how much of the PATH needs setting.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
2

If you are talking specifically about the executable search path environment variable named PATH, then there are a few places that it is set:

  • In your shell settings if it is a command line tool. Depending on your shell, this could be ~/.cshrc, ~/.profile, ~/.bash_profile, etc.
  • In the environment.plist file that was mentioned earlier.
  • If you are in a debugger, then it is whatever gdb uses. I believe that gdb will read commands from ~/.gdbinit if it exists.
  • XCode lets you set environment variables within the Info page for executables.
D.Shawley
  • 58,213
  • 10
  • 98
  • 113
  • This won't help if you need to pass search paths to a script that is trying to find executables in places like /usr/local/bin. This is a deficiency in Xcode's support for external build targets (Xcode 4.2 as of this writing). – Bored Astronaut Feb 17 '12 at 17:24
1

Nothing was working for me in XCode 7.
You need to set the PATH variable in XCode schemes.

Found the solution at: Where to set environment variables for app?

Community
  • 1
  • 1
1

Xcode doesn't look at your shell path environment.

Have a look at NSProcessInfo; and do an NSLog to see what comes up.

If you want a path to apply to all graphical programs you need to set up the ~/.MacOSX/environment.plist. as described.

The recommended way to set the environmen variables are actually in /etc/paths and etc/paths.d although these are also not picked up by Xcode.

I asked about this here.

Community
  • 1
  • 1
Abizern
  • 146,289
  • 39
  • 203
  • 257
-3

Try opening your xcode project from the terminal, this worked for me: open some.xcodeproj

Instead of opening xcode and then loading the project or double clicking on it.

I know... silly

OscarRyz
  • 196,001
  • 113
  • 385
  • 569