切换 macOS 当前使用的 Xcode 或命令行工具目录,并安装命令行工具。
xcode-select -p 打印当前 developer 目录,所有 xcrun、clang、make、git 等 shim 都以它为基准。两个常见路径是:只装命令行工具时的 /Library/Developer/CommandLineTools,以及完整 Xcode 的 /Applications/Xcode.app/Contents/Developer。切换用 sudo xcode-select --switch 路径,路径必须指到 Contents/Developer,不能只写 .app。
xcode-select --install 会弹出图形安装器,装的是 Command Line Tools(CLT),不包含完整 Xcode、iOS SDK 和 xcodebuild;CLT 的许可证在安装器里同意,不需要再跑 xcodebuild。若是完整 Xcode,切换后还要 sudo xcodebuild -license accept,新版 Xcode 首次运行还需 sudo xcodebuild -runFirstLaunch 安装附加组件。许可证未接受时,编译会报 “You have not agreed to the Xcode license agreements”。
无图形界面的 CI 上,--install 可能报 “Can't install the software because it is not currently available from the Software Update server”。可以先用 softwareupdate --list 找到 Command Line Tools 条目再 softwareupdate -i,或从 Apple Developer 下载页手动安装。切换 Xcode 后若工具仍指向旧路径,用 sudo xcode-select --reset 恢复默认,或检查 DEVELOPER_DIR 环境变量是否覆盖了全局设置。
# 查看当前生效的 developer 目录
% xcode-select -p
# 打开命令行工具安装器(只装 CLT,不含完整 Xcode)
% xcode-select --install
# 确认 CLT 包是否真的装上及版本
% pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
# 切换到完整 Xcode
% sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
# 完整 Xcode 首次使用:接受许可证并安装附加组件
% sudo xcodebuild -license accept
% sudo xcodebuild -runFirstLaunch
# 恢复默认路径,Xcode 被移动或删除后常用
% sudo xcode-select --reset
# 只对当前命令临时指定 Xcode,不改全局设置
% DEVELOPER_DIR=/Applications/Xcode-beta.app/Contents/Developer xcrun --find clang
# 无图形界面时查找可安装的 Command Line Tools
% softwareupdate --list
xcode-select --switch 不加 sudo 会因权限不足失败;切换是系统级设置,需要管理员权限。/Applications/Xcode.app 会报 invalid developer directory,必须写到 /Applications/Xcode.app/Contents/Developer。active developer path ... does not exist,用 sudo xcode-select --reset 回到 CLT,或切换到新位置。--install 只是打开安装器,命令立即返回,不代表装完;用 pkgutil --pkg-info=com.apple.pkg.CLTools_Executables 验证。git、clang 仍不可用:可能指向了已删除的 Xcode,或 DEVELOPER_DIR 覆盖了路径;查 xcode-select -p 和 env | grep DEVELOPER_DIR。sudo xcodebuild -license accept;只装 CLT 时没有可用的 xcodebuild,CLT 许可证应在 --install 安装器中接受,必要时重装。xcodebuild 报 requires Xcode:CLT 不包含 xcodebuild,必须安装完整 Xcode 并切换过去。git: error: unable to find utility;重新运行 xcode-select --install 或用 softwareupdate 安装匹配版本。xcrun — 按当前 developer 目录查找并执行工具xcodebuild — 完整 Xcode 提供的构建命令brew — Homebrew 从源码构建时依赖 CLTsoftwareupdate — 无界面安装 CLT 的替代路径合上页面,先回答,再展开对照。
xcode-select -p 的输出决定了什么?xcode-select --install 装完后为什么 xcodebuild 仍不可用?--install 只安装 Command Line Tools,不包含完整 Xcode 和 xcodebuild;需要安装 Xcode 并用 --switch 指过去。sudo xcode-select --switch 新路径/Contents/Developer 指向新位置,或 sudo xcode-select --reset 回到默认 CLT。
小程序版
微信扫码,随时查命令