[Mac / python / sudachipy] pip install 時にMacマシンでのみ error: can't find Rust compiler 発生

# 概要

Dockerイメージ上にも関わらず、 **Mac マシン上でのみ `sudacipy` の pip install に失敗**し、 **EC2 上では問題なく install できる** という不可解な事象に遭遇した。

その解決策である。

# 環境

||バージョンなど|
|:--|:--|
|OS|MacOS Ventura 13.6|
|CPU|Apple M2|


# エラー発生状況

## エラー概要

重要なポイントはここ。

```bash
#0 49.44       error: can't find Rust compiler
#0 49.44       
#0 49.44       If you are using an outdated pip version, it is possible a prebuilt wheel is available for this package but pip is not able to install from it. Installing from the wheel would avoid the need for a Rust compiler.

...(略)

#0 49.44   ERROR: Failed building wheel for sudachipy

...(略)

#0 52.05 Successfully built ipadic sentence-transformers psutil
#0 52.05 Failed to build sudachipy
```

## エラー全文

一応全文も載せておくね。

<details><summary>全文を表示</summary>


```bash:errorログ
$ docker compose build

...(略)

 > [7/7] RUN pip3 install -r requirements.txt: 

...(略)

#0 49.35 Building wheels for collected packages: sudachipy, ipadic, sentence-transformers, psutil
#0 49.35   Building wheel for sudachipy (pyproject.toml): started
#0 49.44   Building wheel for sudachipy (pyproject.toml): finished with status 'error'
#0 49.44   error: subprocess-exited-with-error
#0 49.44   
#0 49.44   × Building wheel for sudachipy (pyproject.toml) did not run successfully.
#0 49.44   │ exit code: 1
#0 49.44   ╰─> [35 lines of output]
#0 49.44       running bdist_wheel
#0 49.44       running build
#0 49.44       running build_py
#0 49.44       creating build
#0 49.44       creating build/lib.linux-aarch64-cpython-310
#0 49.44       creating build/lib.linux-aarch64-cpython-310/sudachipy
#0 49.44       copying py_src/sudachipy/__init__.py -> build/lib.linux-aarch64-cpython-310/sudachipy
#0 49.44       copying py_src/sudachipy/command_line.py -> build/lib.linux-aarch64-cpython-310/sudachipy
#0 49.44       creating build/lib.linux-aarch64-cpython-310/sudachipy/dictionary
#0 49.44       copying py_src/sudachipy/dictionary/__init__.py -> build/lib.linux-aarch64-cpython-310/sudachipy/dictionary
#0 49.44       creating build/lib.linux-aarch64-cpython-310/sudachipy/tokenizer
#0 49.44       copying py_src/sudachipy/tokenizer/__init__.py -> build/lib.linux-aarch64-cpython-310/sudachipy/tokenizer
#0 49.44       creating build/lib.linux-aarch64-cpython-310/sudachipy/morphemelist
#0 49.44       copying py_src/sudachipy/morphemelist/__init__.py -> build/lib.linux-aarch64-cpython-310/sudachipy/morphemelist
#0 49.44       creating build/lib.linux-aarch64-cpython-310/sudachipy/morpheme
#0 49.44       copying py_src/sudachipy/morpheme/__init__.py -> build/lib.linux-aarch64-cpython-310/sudachipy/morpheme
#0 49.44       copying py_src/sudachipy/sudachipy.pyi -> build/lib.linux-aarch64-cpython-310/sudachipy
#0 49.44       creating build/lib.linux-aarch64-cpython-310/sudachipy/resources
#0 49.44       copying py_src/sudachipy/resources/sudachi.json -> build/lib.linux-aarch64-cpython-310/sudachipy/resources
#0 49.44       copying py_src/sudachipy/resources/rewrite.def -> build/lib.linux-aarch64-cpython-310/sudachipy/resources
#0 49.44       copying py_src/sudachipy/resources/unk.def -> build/lib.linux-aarch64-cpython-310/sudachipy/resources
#0 49.44       copying py_src/sudachipy/resources/char.def -> build/lib.linux-aarch64-cpython-310/sudachipy/resources
#0 49.44       running build_ext
#0 49.44       running build_rust
#0 49.44       error: can't find Rust compiler
#0 49.44       
#0 49.44       If you are using an outdated pip version, it is possible a prebuilt wheel is available for this package but pip is not able to install from it. Installing from the wheel would avoid the need for a Rust compiler.
#0 49.44       
#0 49.44       To update pip, run:
#0 49.44       
#0 49.44           pip install --upgrade pip
#0 49.44       
#0 49.44       and then retry package installation.
#0 49.44       
#0 49.44       If you did intend to build this package from source, try installing a Rust compiler from your system package manager and ensure it is on the PATH during installation. Alternatively, rustup (available at https://rustup.rs) is the recommended way to download and update the Rust compiler toolchain.
#0 49.44       [end of output]
#0 49.44   
#0 49.44   note: This error originates from a subprocess, and is likely not a problem with pip.
#0 49.44   ERROR: Failed building wheel for sudachipy
#0 49.44   Building wheel for ipadic (setup.py): started
#0 51.23   Building wheel for ipadic (setup.py): finished with status 'done'
#0 51.24   Created wheel for ipadic: filename=ipadic-1.0.0-py3-none-any.whl size=13556704 sha256=3f72f8b25f8b35307d04d3029c81b59d6bfb49e851ecabd58c075bb2b2b7557d
#0 51.24   Stored in directory: /root/.cache/pip/wheels/5b/ea/e3/2f6e0860a327daba3b030853fce4483ed37468bbf1101c59c3
#0 51.24   Building wheel for sentence-transformers (setup.py): started
#0 51.40   Building wheel for sentence-transformers (setup.py): finished with status 'done'
#0 51.40   Created wheel for sentence-transformers: filename=sentence_transformers-2.2.2-py3-none-any.whl size=125924 sha256=465bf6936028f9600b1eb5373bda99c705700b5f645b7e7d7458647d0fdec226
#0 51.40   Stored in directory: /root/.cache/pip/wheels/62/f2/10/1e606fd5f02395388f74e7462910fe851042f97238cbbd902f
#0 51.40   Building wheel for psutil (pyproject.toml): started
#0 52.05   Building wheel for psutil (pyproject.toml): finished with status 'done'
#0 52.05   Created wheel for psutil: filename=psutil-5.9.6-cp310-abi3-linux_aarch64.whl size=285215 sha256=415f3468eafc34011585104af17d4e4a82b02d3660e7b76dba97f279502bdba4
#0 52.05   Stored in directory: /root/.cache/pip/wheels/5e/96/5c/772c1e1c9ec27f726ee70b6681e2058fcfbf9e84c76df62027
#0 52.05 Successfully built ipadic sentence-transformers psutil
#0 52.05 Failed to build sudachipy
#0 52.05 ERROR: Could not build wheels for sudachipy, which is required to install pyproject.toml-based projects
------
failed to solve: executor failed running [/bin/sh -c pip3 install -r requirements.txt]: exit code: 1
```

</details>



# 当初使っていたソース

- Dockerfile
- docker-compose.yml
- requirements.txt

以上の3ファイルの、関係ありそうなところだけ共有します。

```dockerfile:Dockerfile
FROM python:3.10.12-slim

RUN export DEBIAN_FRONTEND=noninteractive \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
    curl \
    gcc \
    git \
    net-tools \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && pip3 install --upgrade pip


WORKDIR /workspace

COPY ./requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
```

```yaml:docker-compose.yml
# NOTE: 関係ない部分は消してあります
version: '3.7'
services:
  sudachi:
    build:
      context: <Dockerfile が入っている directory の path>
    working_dir: /workspace
```

```python:requirements.txt
sudachipy==0.6.7
sudachidict_core==20230927
```

EC2 上であれば、上記の `Dockerfile` のままで、 `sudachipy` のインストールに成功していた。
なので、おそらく Mac 特有の問題。


# 対策

以下のように修正することで、 `sudachipy` の install に成功しました。


```dockerfile:Dockerfile
FROM python:3.10.12-slim

RUN export DEBIAN_FRONTEND=noninteractive \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
    curl \
    gcc \
    git \
    net-tools \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && pip3 install --upgrade pip


# --------------------------------------------------------------
# Install rust (*** 追加した部分!!! ***)
#
# NOTE: Mac PC で build する場合のみ Rust が必要な模様
# --------------------------------------------------------------
ENV PATH=$PATH:/root/.cargo/bin
RUN curl https://sh.rustup.rs -sSf > /rust.sh && sh /rust.sh -y \
    && rustup install stable


WORKDIR /workspace

COPY ./requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
```

あとは、一応動作確認しよう。

```bash
$ docker compose build
$ docker compose run sudachi bash

# docker container 内で
$ python
> import sudachipy
```


# 参考サイト

## sudachipy と Mac の関係性について

- sudachipy を Mac Arm で使用する場合は Rust が必要と記載されていた。

https://qiita.com/eiennohito/items/20472187b236cf45fe12#:~:text=ARM%E3%81%AEMac%20(Apple%20Silicon)%20%E3%81%AE%E5%A0%B4%E5%90%88

- Mac の話かはわからないが、 Rust が必要と記載されていた。

https://scrapbox.io/nikkie-memos/SudachiPy%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%A7%E3%82%A8%E3%83%A9%E3%83%BC%EF%BC%882022%2F04%EF%BC%89

## Rust の interactive なインストールコマンド

https://github.com/huggingface/transformers/issues/2831#issuecomment-600141935

## 役に立った

`Rust` の non-interactive なインストールコマンドが書かれている。
`sudachipy` と関係ない記事だけど、これで解決できた。

https://qiita.com/jishi77/items/e7a0db2408b76e5e271e


## 直接は関係ないが、周辺情報が記載されていた

https://github.com/docker/compose/issues/8105


- Can't Find Rust Compiler の対処方法

https://dev.to/emilossola/troubleshooting-guide-fixing-error-cant-find-rust-compiler-1pad