Running a command tool or app results in an error ‘dyld: Library not loaded’

Problem

When running macOS 10.14.3 or earlier, you encounter an error reporting dyld: Library not loaded, following which a dylib is named, along the lines of libswift[…].dylib. This occurs when you launch a command tool or app, or perhaps when a script or app might be launching a command tool.

Cause

This error was very unusual, and means that macOS was unable to find a named dylib on which that executable code depended. However, there are now circumstances in which it can more readily occur. This is because command tools built using Xcode 10.2 for macOS 10.14.4 and later assume that those Swift libraries are already installed (as they are in macOS 10.14.4) in /usr/lib/swift.

This error will therefore occur when both:

  • a command tool written using Swift and built using Xcode 10.2 is run on macOS 10.14.3 or earlier, and
  • Swift 5 Runtime Support for Command Tools hasn’t been installed.

Solution

Download the runtime support package from here and install it. This should provide the required dylibs to enable the command tool to run normally again.

Alternatively update to macOS 10.14.4 or later, which includes those supporting dylibs.

Further information

One of the major internal changes brought with 10.14.4 is that Swift ‘glue’ libraries – runtime support which enables Swift code to work with frameworks – are now built into macOS. An odd side-effect of this is that command tools built using Xcode 10.2 won’t run on any previous version of macOS, including 10.14.3, unless the user (or the command tool’s installer) installs the Swift 5 Runtime Support for Command Tools package. This not only applies to dedicated command tools, but to those embedded in app bundles and other locations. Apple has documented this in the release notes for Swift 5, but hasn’t, as far as I can see, detailed this for regular macOS users yet.

Developers who intend shipping apps with embedded command tools, or the command tools themselves, should either bundle the dylibs with their product (if this is allowed, as Apple doesn’t seem to provide advice on this), or install the Apple package, where necessary.