> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-fbfa8bee.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 在 macOS 系统上从源码构建 ClickHouse 的指南

# 在 macOS 上构建 ClickHouse

<Info>
  **本构建指南适用于修改 ClickHouse 本身的贡献者。**

  如果您不修改 ClickHouse 源代码，可以按照[快速入门](/zh/get-started/setup/install)中的说明安装预编译的 ClickHouse。
</Info>

ClickHouse 可在 macOS x86\_64 (Intel) 和 arm64 (Apple Silicon) 上编译，需使用 macOS 10.15 (Catalina) 或更高版本。

编译器仅支持通过 Homebrew 安装的 Clang。

<div id="install-prerequisites">
  ## 安装前置条件
</div>

首先，请参阅通用的[前置条件文档](/zh/resources/develop-contribute/introduction/developer-instruction)。

接下来，安装 [Homebrew](https://brew.sh/) 并运行

然后运行：

```bash theme={null}
brew update
brew install ccache cmake ninja libtool gettext llvm lld binutils grep findutils nasm bash rust rustup
```

<Note>
  Apple 默认使用不区分大小写的文件系统。虽然这通常不会影响编译 (尤其是 scratch make 仍可正常工作) ，但可能会给 `git mv` 之类的文件操作带来困扰。
  如果要在 macOS 上进行较为正式的开发，请确保将源代码存放在区分大小写的磁盘卷上，例如可参阅[这些说明](https://brianboyko.medium.com/a-case-sensitive-src-folder-for-mac-programmers-176cc82a3830)。
</Note>

<div id="build-clickhouse">
  ## 构建 ClickHouse
</div>

构建时必须使用 Homebrew 的 Clang 编译器：

```bash theme={null}
cd ClickHouse
mkdir build
export PATH=$(brew --prefix llvm)/bin:$PATH
cmake -S . -B build
cmake --build build
# 生成的二进制文件将位于：build/programs/clickhouse
```

<Note>
  如果你在链接过程中遇到 `ld: archive member '/' not a mach-o file in ...` 错误，可能需要
  通过设置标志 \`-DCMAKE\_AR=/opt/homebrew/opt/llvm/bin/llvm-ar\`\` 来改用 llvm-ar。
</Note>

<div id="caveats">
  ## 注意事项
</div>

如果你打算运行 `clickhouse-server`，请务必提高系统的 `maxfiles` 变量值。

<Note>
  需要使用 sudo。
</Note>

为此，请使用以下内容创建 `/Library/LaunchDaemons/limit.maxfiles.plist` 文件：

```xml theme={null}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>limit.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>524288</string>
      <string>524288</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>
```

为该文件设置正确的权限：

```bash theme={null}
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
```

验证文件是否正确：

```bash theme={null}
plutil /Library/LaunchDaemons/limit.maxfiles.plist
```

重新加载该文件 (或重启) ：

```bash theme={null}
sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
```

要检查是否生效，请使用 `ulimit -n` 或 `launchctl limit maxfiles` 命令。
