where is $PATH set in xcode?

34,340

Solution 1

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

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

enter image description here

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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

(This part came from this answer.)

Share:
34,340
Berry Ligtermoet
Author by

Berry Ligtermoet

Updated on December 02, 2021

Comments

  • Berry Ligtermoet
    Berry Ligtermoet over 2 years

    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?