This post is spawned by this question

In my Xcode I added the new Build Phase to copy .dylib files to /usr/local/lib. And then I tried to rebuiold the application by doing "Command + B".

However, since the destination should be accessed thru the root account I received an error couldn't create directory /usr/local/lib: Permission denied.

This is correct as the Xcode is started by the regular user and not root. So now I am looking for a way to execute just this build phase as root.

In the question mentioned above I got a direction that OSX is a *nix and the process is the same. While I agree that OSX is a *nix, the process of building/installing are different. On *nix you issue make as a regular user, just as you do Command + B in Xcode. But then when it finishes building in *nix you issue a separate command sudo make install, which installs all the libraries and binaries in /usr/local/lib and /usr/local/bin. All IDE on *nix (such as Anjuta and KDevelop) has extra menu to it which when selected ask about the root password and perform the install as root

And so what I am looking for is for Xcode to ask me about the root password in order to execute this Build Phase I added to copy my dylib's, because I am not familiar with this IDE.



Igors-MacBook-Air:~ igorkorot$ ls -la /Users/igorkorot/dbhandler/dbhandler/Build/Products/Debug/
total 17344
drwxr-xr-x  13 igorkorot  staff      416 Jun  9 18:35 .
drwxr-xr-x@  4 igorkorot  staff      128 Nov 16  2018 ..
drwxr-xr-x   3 igorkorot  staff       96 Jun 10 20:05 dbhandler.app
-rwxr-xr-x   1 igorkorot  staff   129856 Jun  9 18:34 liblibdbloader.dylib
-rwxr-xr-x   1 igorkorot  staff  1002940 Jun  9 18:35 liblibdbwindow.dylib
-rwxr-xr-x   1 igorkorot  staff  1671068 Jun  9 18:35 liblibdialogs.dylib
-rwxr-xr-x   1 igorkorot  staff   148244 Jun  9 18:34 liblibfieldswindow.dylib
-rwxr-xr-x   1 igorkorot  staff   587976 Jun  9 18:33 liblibmysql.dylib
-rwxr-xr-x   1 igorkorot  staff   799272 Jun  9 18:33 liblibodbc.dylib
-rwxr-xr-x   1 igorkorot  staff   611680 Jun  9 18:33 liblibpostgres.dylib
-rwxr-xr-x   1 igorkorot  staff   482852 Jun  9 18:33 liblibpropertypages.dylib
-rwxr-xr-x   1 igorkorot  staff  1619172 Jun  9 18:34 liblibshapeframework.dylib
-rwxr-xr-x   1 igorkorot  staff  1810304 Jun  9 18:33 liblibsqlite.dylib

And so I tried to add LD_LIBRARY_PATH as /Users/igorkorot/dbhandler/dbhandler/Build/Products/Debug/, but trying to run it (dbhandler.app) from inside Xcode, the program still crashes. Changing the variable to be DYLD_LIBRARY_PATH doesn't make a difference.

I also thought that having the libraries (dylib's) in the same directory as application bundle is all there is to need to successfully start the application and keep the linker satisfied. Apparently I was wrong.



Please refer to the screenshot below.



And this is how it looks like in Xcode:


enter image description here enter image description here

  • While you can ask this question here, you'll probably get a better/faster response on SO. There's many questions with accepted answers on running build phases as root on that site, one of them might resolve your problem.
    – fsb
    Jun 10, 2019 at 17:04
  • @fsb, I tried googling and came up with nothing, unfortunately. Also, this question is specific to the Xcode environment and not to the programming per se. Hence asking here.
    – Igor
    Jun 10, 2019 at 17:19
  • 1
    I checked on SO and found about 15 different answers for running build phase scripts as root. It is on-topic for this site, as I mentioned, but SO will expose your question to many more Xcode developers. I'm just trying to help you get this answered in the shortest possible timespan.
    – fsb
    Jun 10, 2019 at 18:28
  • 1
    You can just make a script that simply does “cp sourcefile destination”. That’s one way of doing it.
    – jksoegaard
    Jun 10, 2019 at 18:43
  • 1
    About all Linux IDEs having this special button and macOS being very different - it is simply not correct. Not all Linux IDEs have this button, and macOS does not in any way whatsoever dictate or neccesitate that the button can’t be there. You can run KDevelop on macOS if you want.
    – jksoegaard
    Jun 10, 2019 at 18:45

1 Answer 1


Like I mentioned in your previous question, you need to use “sudo” in front of your copy command to get it to run. Just like you run “sudo make install” on Linux.

To get this working easily, I will suggest setting up sudo so that no password is necessary to run that specific command.

Another possibility is to use an AppleScript instead of a shell script - that will allow you to pop up the graphical dialog asking for the sudo password.

A different option is setting your buildscript as suid root, so you do not need to use sudo at all.

If you can make do with an alternative that is not centered about installing in /use/local/lib, I would strongly recommend not installing your library there - instead install it in the application bundle.

For Xcode you can set the LD_LIBRARY_PATH / DYLD_LIBRARY_PATH environment variables to include your build directory, so you do not have to copy the library anywhere. You set environment variables by choosing the menu item Product => Scheme => Edit scheme... And then under "Run" and "Arguments" you'll find "Environment Variables".

For distribution I would recommend using run-path libraries that work by telling the dynamic linker that it should look for the library within the application bundle (or where you choose). The docs on that are available here:


  • as you probably can tell I'm not familiar with Xcode. I'd like to not to install anything in /usr/local/lib, but have no idea on how to do that in Xcode. Also, if I set LD_LIBRARY_PATH in the Terminal it will not be picked up the IDE as they are running in a different environment. So how can I do that inside Xcode?
    – Igor
    Jun 10, 2019 at 19:25
  • @Igor I have updated the answer to include how you set an environment variable.
    – jksoegaard
    Jun 10, 2019 at 19:36
  • 1
    BTW: Have you considered using the create project wizard to create a new library project? - This includes the build settings to deploy the library, etc. This way everything is setup for you in advance.
    – jksoegaard
    Jun 10, 2019 at 19:37
  • I followed the instruction here: apple.stackexchange.com/questions/231511/… to create a "workspace" - projects for binary and libraries...
    – Igor
    Jun 10, 2019 at 19:45
  • Sounds like you have done something incorrectly then - if you follow that guide, it should not be necessary to do any of this (i.e. manually copying over library files, etc.). BTW: Why not use a recent Xcode? - Xcode 7 is very old!
    – jksoegaard
    Jun 10, 2019 at 19:56

You must log in to answer this question.

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