Skip to content

Instantly share code, notes, and snippets.

@pdarcey
Last active November 3, 2025 11:04
Show Gist options
  • Select an option

  • Save pdarcey/67b6aa66527f715ca7a70e5e82048809 to your computer and use it in GitHub Desktop.

Select an option

Save pdarcey/67b6aa66527f715ca7a70e5e82048809 to your computer and use it in GitHub Desktop.
Automate installing a CLI product from Xcode to /usr/local/bin

To add a build phase to automate copying your built CLI executable into /usr/local/bin as part of the build process.

You can add a Run Script Phase to your build.

Steps:

  1. In Xcode, select your project in the sidebar.

  2. Choose your target → Build Phases tab.

  3. Click the “+” → New Run Script Phase.

  4. Drag it below the “Build” phase (after the binary is built).

  5. Add this script:

# Path to your built binary (adjust if needed)
APP_PATH="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}"

# Destination
DEST="/usr/local/bin/${PRODUCT_NAME}"

# Ensure /usr/local/bin exists
mkdir -p /usr/local/bin

# Copy the executable (use cp -f to overwrite)
cp -f "$APP_PATH" "$DEST"

# Make sure it's executable
chmod +x "$DEST"

echo "✅ Installed ${PRODUCT_NAME} to /usr/local/bin"

Due to sandboxing restrictions in Xcode 15 and later versions, where the ENABLE_USER_SCRIPT_SANDBOXING build setting defaults to YES, Xcode restricts access to files within the project's source root and Derived Data directory unless explicitly declared as inputs or outputs in the build phase. So, we need to explicitly declare the required input and output files, ensuring the script only accesses the files it needs.

  1. Click on the "+" under Output Files. Add "/usr/local/bin/${PRODUCT_NAME}" (without the quotes)

Now every time you Build (⌘B), Xcode will copy your compiled binary to /usr/local/bin.

You can then immediately run it from Terminal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment