where is $PATH set in xcode?
Asked Answered
I

10

38

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?

Inverse answered 31/5, 2009 at 16:44 Comment(1)
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/binPhiladelphia
P
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.

Phooey answered 31/5, 2009 at 17:11 Comment(3)
thanks you, this is what I'm doing currently, but I'm hoping to find a "global" way that would apply to the whole build processInverse
That does not work so well for e.g. ruby scripts. Especially when you need rbenv in the path.Palila
@Palila This worked for me: PATH=${PATH}:/usr/local/var/rbenv/shimsBernice
M
15

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

Mainmast answered 1/12, 2021 at 19:14 Comment(1)
Suggest to add a quote to $PATH like this: sudo launchctl config user path "$PATH". Otherwise it may break the path variable.Qp
B
10

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

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

enter image description here

Bozarth answered 27/3, 2017 at 10:15 Comment(2)
If anyone was wondering where this is found, you can click on "edit scheme". :DProstrate
For me this was under "Product > scheme > edit scheme" in the menuMemling
B
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.

Bade answered 31/5, 2009 at 16:58 Comment(2)
Hmmm... I don't have a file like that. Is there a global version of this that applies to all users?Inverse
That file is great for some things, but doesn't seem to work for the PATH variable.Cavuoto
C
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

Corabella answered 30/8, 2014 at 1:22 Comment(2)
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?Disabuse
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.Gamely
B
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.
Burson answered 31/5, 2009 at 21:6 Comment(1)
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).Cavuoto
T
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.

Tarantella answered 22/10, 2020 at 8:27 Comment(0)
G
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.

Graces answered 31/5, 2009 at 21:20 Comment(0)
I
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?

Ingratiating answered 21/5, 2016 at 21:41 Comment(0)
M
-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

Microchemistry answered 20/2, 2015 at 1:8 Comment(1)
it doesn't work. I guess open trigger opening in backgroundMcminn

© 2022 - 2024 — McMap. All rights reserved.