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

> Guide for cross-compiling ClickHouse from Linux for macOS systems

# Build on Linux for macOS

This is for the case when you have a Linux machine and want to use it to build `clickhouse` binary that will run on OS X.
The main use case is continuous integration checks which run on Linux machines.
If you want to build ClickHouse directly on macOS, proceed with the [native build instructions](/resources/develop-contribute/build/build-osx).

The cross-build for macOS is based on the [Build instructions](/resources/develop-contribute/build/build), follow them first.

The following sections provide a walk-through for building ClickHouse for `x86_64` macOS.
If you're targeting ARM architecture, simply substitute all occurrences of `x86_64` with `aarch64`.
For example, replace `x86_64-apple-darwin` with `aarch64-apple-darwin` throughout the steps.

<h2 id="install-cross-compilation-toolset">
  Install cross-compilation toolset
</h2>

Let's remember the path where we install `cctools` as `${CCTOOLS}`

```bash theme={null}
mkdir ~/cctools
export CCTOOLS=$(cd ~/cctools && pwd)
cd ${CCTOOLS}

git clone https://github.com/tpoechtrager/apple-libtapi.git
cd apple-libtapi
git checkout 15dfc2a8c9a2a89d06ff227560a69f5265b692f9
INSTALLPREFIX=${CCTOOLS} ./build.sh
./install.sh
cd ..

git clone https://github.com/tpoechtrager/cctools-port.git
cd cctools-port/cctools
git checkout 2a3e1c2a6ff54a30f898b70cfb9ba1692a55fad7
./configure --prefix=$(readlink -f ${CCTOOLS}) --with-libtapi=$(readlink -f ${CCTOOLS}) --target=x86_64-apple-darwin
make install
```

Also, we need to download macOS X SDK into the working tree.

```bash theme={null}
cd ClickHouse/cmake/toolchain/darwin-x86_64
curl -L 'https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX11.0.sdk.tar.xz' | tar xJ --strip-components=1
```

<h2 id="build-clickhouse">
  Build ClickHouse
</h2>

```bash theme={null}
cd ClickHouse
mkdir build-darwin
cd build-darwin
cmake -DCMAKE_AR:FILEPATH=${CCTOOLS}/bin/x86_64-apple-darwin-ar -DCMAKE_INSTALL_NAME_TOOL=${CCTOOLS}/bin/x86_64-apple-darwin-install_name_tool -DCMAKE_RANLIB:FILEPATH=${CCTOOLS}/bin/x86_64-apple-darwin-ranlib -DLINKER_NAME=${CCTOOLS}/bin/x86_64-apple-darwin-ld -DCMAKE_TOOLCHAIN_FILE=cmake/darwin/toolchain-x86_64.cmake ..
ninja
```

The resulting binary will have a Mach-O executable format and can't be run on Linux.
