如何安装 Xcode 命令行工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9329243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:14:50  来源:igfitidea点击:

How to install Xcode Command Line Tools

xcodemacoscommand-line-tool

提问by Scott Wisniewski

How do I get the command-line build tools installed with the current Xcode/Mac OS X v10.8 (Mountain Lion) or later?

如何获取与当前 Xcode/Mac OS X v10.8 (Mountain Lion) 或更高版本一起安装的命令行构建工具?

Unlike Xcode there is no installer, it's just a bundle.

与 Xcode 不同的是,它没有安装程序,它只是一个包。

It looks like all the command line tools are in the bundle, under Contents/Developer, but none of the appropriate environment variables are set to use them.

看起来所有的命令行工具都在包中,在 下Contents/Developer,但没有设置适当的环境变量来使用它们。

Is there a script I can run that will set-up my environment to support building from the command line?

是否有我可以运行的脚本来设置我的环境以支持从命令行构建?

回答by zaph

Xcode 5.1 and OSX 10.9. (also works with Xcode 5.1.1 + OSX 10.10)

Xcode 5.1 和 OSX 10.9。(也适用于 Xcode 5.1.1 + OSX 10.10)

xcode-select --installworked with version 2333, failed with version 2003. So, try xcode-select --installand if that does not work download as described below.

xcode-select --install使用版本 2333,使用版本 2003 失败。因此,请尝试xcode-select --install,如果不起作用,请按如下所述下载。

In early February 2014 xcode-select --installhas been reporting that "Can't install the software because it is not currently available from the Software Update server". In late February 2014 the command started only displaying help. The solutionis to download directly, see "Separate Download" below.

2014 年 2 月上旬,xcode-select --install一直报告“无法安装该软件,因为它当前无法从软件更新服务器获得”。2014 年 2 月下旬,该命​​令开始仅显示帮助。该解决方案是直接下载,查看下面的“单独下载”。

Xcode 5.0.1 and OSX 10.9

Xcode 5.0.1 和 OSX 10.9

With Xcode 5.0.1 and Mavericks 10.9 the command line tool is no longer available through Xcode. Instead they must be downloaded from the Apple Developer Tools site: https://developer.apple.com/downloads/index.action. This requires signing in with a developer account.

对于 Xcode 5.0.1 和 Mavericks 10.9,命令行工具不再可通过 Xcode 使用。相反,它们必须从 Apple Developer Tools 站点下载:https: //developer.apple.com/downloads/index.action。这需要使用开发者帐户登录。

Or via terminal(from the release docs): The Command Line Developer Tools package can be installed on demand using "xcode-select --install” and the installed tools will be automatically updated using Software Update. OS X 10.9 is required for this feature. For earlier versions, continue to use the in-app download in Xcode.

或者通过终端(来自发布文档):可以使用“ xcode-select --install”按需安装命令行开发人员工具包,安装的工具将使用软件更新自动更新。此功能需要 OS X 10.9 . 对于更早的版本,继续使用 Xcode 中的应用内下载。

Running the command in terminal produces the following GUI:enter image description here

在终端中运行命令会产生以下 GUI:在此处输入图片说明

Inside Xcode (5.0)

Xcode (5.0) 内幕

Xcode includes a new "Downloads" preference pane to install optional components such as command line tools, and previous iOS Simulators. To open this pane click the "Xcode" button in the top left of the screen near the Apple logo, then click "Preferences", then click "Downloads".

Xcode 包含一个新的“下载”首选项窗格,用于安装可选组件,例如命令行工具和以前的 iOS 模拟器。要打开此窗格,请单击屏幕左上角 Apple 徽标附近的“Xcode”按钮,然后单击“首选项”,然后单击“下载”。

Xcode 5.0screenshot: enter image description here

Xcode 5.0截图: 在此处输入图片说明

Xcode 4.xscreenshot: screenshot of downloads pane

Xcode 4.x截图: 下载窗格的屏幕截图

Separate Download

单独下载

If you do not have Xcode, they are available as a separate download from Apple:

如果您没有 Xcode,可以从 Apple 单独下载它们:

Go to developer.apple.com/downloads/index.action, and sign in with your Apple ID (the download's free). In the pane on the left, search for "command line tools" and choose the package appropriate to your version of OS X. Requires Mac OS X 10.7.3 or later.

前往developer.apple.com/downloads/index.action,并使用您的 Apple ID 登录(下载免费)。在左侧窗格中,搜索“命令行工具”并选择适合您的 OS X 版本的软件包。需要 Mac OS X 10.7.3 或更高版本。

回答by vinzenzweber

If you take a look into "Console" while trying to pull the Command Line Tools, you will find out that there is actually an "unofficial" link to the Mountain Lion Command Line Tools!

如果您在尝试拉取命令行工具时查看“控制台”,您会发现实际上有一个指向 Mountain Lion 命令行工具的“非官方”链接!

So just try:

所以试试吧:

http://adcdownload.apple.com/ios/ios_simulator__resigned/cltools_mountainliondp2_march12.dmg

http://adcdownload.apple.com/ios/ios_simulator__resigned/cltools_mountainliondp2_march12.dmg

Here is my Console log:

这是我的控制台日志:

01/04/2012 15:41:54.258 Xcode: [MT] DVTDownloadable: Download failed. Downloadable: {
    dependencies =     (
    );
    fileSize = 141452226;
    identifier = "Xcode.CLTools.10.8";
    name = "Command Line Tools";
    source = "http://adcdownload.apple.com/ios/ios_simulator__resigned/cltools_mountainliondp2_march12.dmg";
    userInfo =     {
        ActivationPredicate = "$MAC_OS_X_VERSION >= '10.8.0' && $MAC_OS_X_VERSION < '10.9.0'";
        InstallPrefix = "/";
        InstalledIfAllSHA1SumsMatch =         {
            "/usr/bin/clang" = 600c35175775a6002452a88f17e00c570cd2e2d0;
        };
        Summary = "Before installing, note that from within Terminal you can use the XCRUN tool to launch compilers and other tools embedded within the Xcode application. Use the XCODE-SELECT tool to define which version of Xcode is active.  Type \"man xcrun\" from within Terminal to find out more.\n\nDownloading this package will install copies of the core command line tools and system headers into system folders, including the LLVM compiler, linker, and build tools.";
        "Xcode.SDKs" =         (
        );
    };
    version = "1.1.1";
}. Error: Error Domain=NSCocoaErrorDomain Code=3840 "The data couldn't be read because it has been corrupted." (Encountered unknown ampersand-escape sequence at line 18) UserInfo=0x401bc8e60 {NSDebugDescription=Encountered unknown ampersand-escape sequence at line 18, kCFPropertyListOldStyleParsingError=The data couldn't be read because it has been corrupted.}

回答by Jason T. Miller

The "unofficial" links are also available at

“非官方”链接也可在

http://developer.apple.com/downloads/

http://developer.apple.com/downloads/

and 403: Forbiddenunless logged in with a qualifying developer account. Free accounts have access to everything except prerelease software and full OS installs. In particular, the command-line tools packages that coincide with the release of Xcode 4.4 (the "July 2012" packages) were posted on July 27, for both Lion and Mountain Lion.

403:禁止,除非使用合格的开发者帐户登录。免费帐户可以访问除预发布软件和完​​整操作系统安装之外的所有内容。特别是,与 Xcode 4.4 发布(“2012 年 7 月”包)同时发布的命令行工具包于 7 月 27 日发布,适用于 Lion 和 Mountain Lion。

Unlike log files, deep-linking Web pages, and the Xcode Preferences window, this page also links to lots of other useful downloads for free and paid developers alike, including everything else that has been unbundled from Xcode (audio tools, accessibility tools, etc.), OS X kernel debug builds, and release versions of Xcode back to 1.0.

与日志文件、深层链接网页和 Xcode 首选项窗口不同,此页面还链接到许多其他有用的免费和付费开发人员下载,包括从 Xcode 中分离出来的所有其他内容(音频工具、辅助功能工具等) .)、OS X 内核调试版本,并将 Xcode 的发布版本恢复到 1.0。

回答by Krunal

Here are links for Command Line Tools, located at Apple Developer Server. You may need an apple developer (Paid/Premium) account to access them. You can download current (stable and beta) Xcode Tools. (It does not need device verification. Just ensure your macOS and XCode Tool/SDK version while choosing your command line tool)

以下是位于 Apple Developer Server 的命令行工具链接。您可能需要一个苹果开发者(付费/高级)帐户才能访问它们。您可以下载当前(稳定版和测试版)Xcode 工具。(它不需要设备验证。只需在选择命令行工具时确保您的 macOS 和 XCode Tool/SDK 版本)

Xcode 11

Xcode 10

Xcode 9

Xcode 11

Xcode 10

Xcode 9

Once you've latest command line tool installed in your system, set it from Xcode Menu.

在系统中安装最新的命令行工具后,从 Xcode 菜单设置它。

(Xcode Menu Items) Xcode ? Preferences ? Location ? Command Line Tool ? Select appropriate command line tool

(Xcode 菜单项) Xcode ? 喜好 ?地点 ?命令行工具 ? 选择合适的命令行工具

enter image description here

在此处输入图片说明

回答by Christoph Dwertmann

Instead of installing the "official" build of the command line tools from Apple, you can also install Kenneth's build from here:

除了从 Apple 安装命令行工具的“官方”版本之外,您还可以从这里安装 Kenneth 的版本:

https://github.com/kennethreitz/osx-gcc-installer

https://github.com/kennethreitz/osx-gcc-installer

It doesn't even require XCode if all you are after is the command line tools.

如果您所追求的只是命令行工具,它甚至不需要 XCode。

回答by Anon

If you want to use the version of tools provided in XCode itself you can use xcrun (e.g. xcrun git). From the help at the bottom of the download preference pane:

如果您想使用 XCode 本身提供的工具版本,您可以使用 xcrun(例如xcrun git)。从下载首选项窗格底部的帮助中:

note that from within Terminal you can use the XCRUN tool to launch compilers and other tools embedded within the Xcode application. Use the XCODE-SELECT tool to define which version of Xcode is active. Type "man xcrun" from within Terminal to find out more.

请注意,在终端中,您可以使用 XCRUN 工具启动编译器和其他嵌入 Xcode 应用程序的工具。使用 XCODE-SELECT 工具来定义哪个版本的 Xcode 处于活动状态。在终端内输入“man xcrun”以了解更多信息。

回答by Thank you

I recently had to install Xcode command line tools on Mountain Lion over SSH.

Notice:I imagine this process would work identically for any version of Xcode or OSX. Just make sure you get your paths correct.

Here's how I did it...

我最近不得不通过 SSH 在 Mountain Lion 上安装 Xcode 命令行工具。

注意:我想这个过程对于任何版本的 Xcode 或 OSX 都是一样的。只要确保你的路径正确。

这是我如何做到的...

  1. If you don't have a free Apple developer account, registerfor one

  2. Loginto https://developer.apple.com/downloads

  3. Downloadthe "Command Line Tools for Xcode" appropriate for your version of OSX

    For me, that was "Command Line Tools (OS X Mountain Lion) for Xcode - April 2014"

  4. Copythe dmg file to your remote

    In the following command, I'm using scpto securely copy the file from my local computer to the remote named remote

    $ scp ~/Downloads/command_line_tools_for_osx_mountain_lion_april_2014.dmg remote:Downloads/
    
  5. sshto your remote

    $ ssh remote
    
  6. mountthe dmg file on the remote

    Here, I'm using hdiutilto mount the image

    $ hdiutil attach ~/Downloads/command_line_tools_for_osx_mountain_lion_april_2014.dmg
    
  7. installthe package contained in the dmg

    Here, installermust be run with sudobecause this package needs to be installed on the root file system

    $ cd /Volumes/Command\ Line\ Tools\ \(Mountain\ Lion\)
    $ sudo installer -pkg Command\ Line\ Tools\ \(Mountain\ Lion\).mpkg -target /
    
  8. unmountthe dmg file

    $ hdiutil detach /Volumes/Command\ Line\ Tools\ \(Mountain\ Lion\)
    
  9. deletethe dmg file from the remote; optional

    I see no purpose keeping it around, but you can if you want.

    $ rm ~/Downloads/command_line_tools_for_osx_mountain_lion_april_2014.dmg
    
  1. 如果您没有免费的 Apple 开发者帐户,请注册一个

  2. 登录https://developer.apple.com/downloads

  3. 下载适用于您的 OSX 版本的“Xcode 命令行工具”

    对我来说,那是“Xcode 的命令行工具(OS X Mountain Lion) - 2014 年 4 月”

  4. dmg 文件复制到您的遥控器

    在以下命令中,我使用scp将文件从本地计算机安全地复制到名为remote

    $ scp ~/Downloads/command_line_tools_for_osx_mountain_lion_april_2014.dmg remote:Downloads/
    
  5. ssh到你的遥控器

    $ ssh remote
    
  6. 在远程挂载dmg 文件

    在这里,我使用hdiutil挂载图像

    $ hdiutil attach ~/Downloads/command_line_tools_for_osx_mountain_lion_april_2014.dmg
    
  7. 安装dmg 中包含的包

    在这里,安装程序必须运行,sudo因为这个包需要安装在根文件系统上

    $ cd /Volumes/Command\ Line\ Tools\ \(Mountain\ Lion\)
    $ sudo installer -pkg Command\ Line\ Tools\ \(Mountain\ Lion\).mpkg -target /
    
  8. 卸载dmg 文件

    $ hdiutil detach /Volumes/Command\ Line\ Tools\ \(Mountain\ Lion\)
    
  9. 从远程删除dmg文件;可选的

    我认为保留它没有任何意义,但如果你愿意,你可以。

    $ rm ~/Downloads/command_line_tools_for_osx_mountain_lion_april_2014.dmg
    

回答by davidcondrey

You can automate the installation of Command-Line Tools in bash. I needed to do this when I was automating the developer onboarding setup process.

您可以在 bash 中自动安装命令行工具。当我自动化开发人员入职设置过程时,我需要这样做。

xcode-select --install && sleep 1
osascript -e 'tell application "System Events"' -e 'tell process "Install Command Line Developer Tools"' -e 'keystroke return' -e 'click button "Agree" of window "License Agreement"' -e 'end tell' -e 'end tell'

回答by davidcondrey

UPDATED: On Lion 10.7.4 the Command Line Tools are already available from withing XCode. You can get it freely from App Store.

更新:在 Lion 10.7.4 上,命令行工具已经可以从 XCode 中获得。您可以从 App Store 免费获取。

The GCC for OSX won't compile some packages obtained from macports. https://github.com/kennethreitz/osx-gcc-installer/downloads

OSX 的 GCC 不会编译一些从 macports 获得的包。 https://github.com/kennethreitz/osx-gcc-installer/downloads

回答by jww

How do I get the command line builds tools installed with Xcode 4.4 / Mac OS X v10.8 (Mountain Lion) or later?

如何获取随 Xcode 4.4 / Mac OS X v10.8 (Mountain Lion) 或更高版本安装的命令行构建工具?

For OS X 10.9, you simply install Xcode. The Command Line Tools are bundled with Xcode. From Technical Note TN2339, Building from the Command Line with Xcode FAQ:

对于 OS X 10.9,您只需安装 Xcode。命令行工具与 Xcode 捆绑在一起。来自技术说明 TN2339,使用 Xcode 常见问题从命令行构建

Downloading command-line tools is not available in Xcode for OS X 10.9. How can I install them on my machine?

下载命令行工具在 OS X 10.9 的 Xcode 中不可用。如何在我的机器上安装它们?

In OS X 10.9, the Downloads pane of Xcode Preferences does not support downloading command-line tools. Use any of the following methods to install command-line tools on your system:

If Xcode is installed on your machine, then there is no need to install them. Xcode comes bundled with all your command-line tools. OS X 10.9 includes shims or wrapper executables. These shims, installed in /usr/bin, can map any tool included in /usr/bin to the corresponding one inside Xcode. xcrun is one of such shims, which allows you to find or run any tool inside Xcode from the command line. Use it to invoke any tool within Xcode from the command line. ...

在 OS X 10.9 中,Xcode 首选项的下载面板不支持下载命令行工具。使用以下任一方法在您的系统上安装命令行工具:

如果您的机器上安装了 Xcode,则无需安装它们。Xcode 与您所有的命令行工具捆绑在一起。OS X 10.9 包括垫片或包装器可执行文件。这些垫片安装在 /usr/bin 中,可以将 /usr/bin 中包含的任何工具映射到 Xcode 中的相应工具。xcrun 就是这样的垫片之一,它允许您从命令行查找或运行 Xcode 中的任何工具。使用它从命令行调用 Xcode 中的任何工具。...