💡

mac os でGiNZAをdockerコンテナ上にインストールして動かしてみる

2024/04/25に公開

動作環境

  • Apple M2 14.4.1(23E224)
  • docker desktop 4.29.0
    • os ubuntu:22.04
    • Python 3.9.18

はじめに

GiNZAとはUniversal Dependenciesに基づくオープンソース日本語NLPライブラリです。
公式
公式の説明を見るとなんだとpip installするだけか、、とか思っていましたが、意外とめんどくさかったの記事にします。

TL;TR

はい。とりあえず正解みせろよ!!という人のためにさきに動作したDockerfileとdocker-compose.ymlを貼っておきます。
ベースイメージにubuntu:22.04を使っているのでクッソ重いかもですが、今後cudaを使う可能性があるのでベースイメージのubuntu osの上にPythonをインストールしました。
gitのリポジトリ
Dockerfile

FROM ubuntu:22.04

ENV TZ=Asia/Tokyo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone


ENV PYTHON_VERSION 3.9.18
# ENV HOME /root
# ENV PYTHON_ROOT $HOME/local/python-$PYTHON_VERSION
# ENV PATH $PYTHON_ROOT/bin:$PATH
# ENV PYENV_ROOT $HOME/.pyenv
#gitのインストール
RUN apt-get update -y && apt-get install -y build-essential vim \
    wget curl git zip gcc make openssl \
    libssl-dev libbz2-dev libreadline-dev \
    libsqlite3-dev python3-tk tk-dev python-tk \
    libfreetype6-dev libffi-dev liblzma-dev




RUN git clone https://github.com/pyenv/pyenv.git /root/.pyenv
ENV HOME  /root
ENV PYENV_ROOT $HOME/.pyenv
ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
RUN pyenv --version
RUN pyenv install $PYTHON_VERSION
RUN pyenv global $PYTHON_VERSION
RUN eval "$(pyenv init --path)"


# Install rust (* なぜかmac osのときだけ必要らしい)
ENV PATH=$PATH:/root/.cargo/bin
RUN curl https://sh.rustup.rs -sSf > /rust.sh
RUN sh /rust.sh -y

# COPY requirements.txt /workspace/
RUN python3 -m pip install --upgrade pip

RUN pip install jupyterlab

# install dependencies for SudachiPy
RUN apt-get update && apt-get install -y build-essential

# install SudachiPy
RUN pip install SudachiPy

# install GiNZA and new transfomers model
RUN pip install -U ginza ja_ginza_electra 

docker-compose.yml

version: '3.9'
services:
  app:
    build: .
    # dockerfile: Dockerfile
    container_name: 'GiNZA'
    working_dir: '/workspace'
    volumes:
      - ./:/workspace/
    ports:
      - 8888:8888

    tty: true
    networks:
      - db_network

networks:
  db_network:
    external: true

実行
必ず、Dockerfileとdocker-compose.ymlを同じディレクトリに入れて、そのディレクトリ直下で実行してください

docker-compose up -d app
docker-compose exec app /bin/bash

つまったところ

この、GiNZAをつかうためにはSudachiPyが必要で、pip install -U ginza ja_ginza_electraだけを実行するとSudachiPyをうまくインストールできないみたいなエラーが発生します。
そこで私は、pip install -U ginza ja_ginza_electraの前にpip install SudachiPyをDockerfileに記載しました。
そうして実行すると、、

 => ERROR [app 15/16] RUN pip install SudachiPy                                                                                        2.6s
------
 > [app 15/16] RUN pip install SudachiPy:
0.527 Collecting SudachiPy
0.649   Downloading SudachiPy-0.6.8.tar.gz (161 kB)
0.695      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 161.9/161.9 kB 3.5 MB/s eta 0:00:00
0.725   Installing build dependencies: started
2.136   Installing build dependencies: finished with status 'done'
2.137   Getting requirements to build wheel: started
2.239   Getting requirements to build wheel: finished with status 'done'
2.240   Preparing metadata (pyproject.toml): started
2.370   Preparing metadata (pyproject.toml): finished with status 'done'
2.374 Building wheels for collected packages: SudachiPy
2.375   Building wheel for SudachiPy (pyproject.toml): started
2.474   Building wheel for SudachiPy (pyproject.toml): finished with status 'error'
2.478   error: subprocess-exited-with-error
2.478
2.478   × Building wheel for SudachiPy (pyproject.toml) did not run successfully.
2.478   │ exit code: 1
2.478   ╰─> [37 lines of output]
2.478       running bdist_wheel
2.478       running build
2.478       running build_py
2.478       creating build
2.478       creating build/lib.linux-aarch64-cpython-39
2.478       creating build/lib.linux-aarch64-cpython-39/sudachipy
2.478       copying py_src/sudachipy/config.py -> build/lib.linux-aarch64-cpython-39/sudachipy
2.478       copying py_src/sudachipy/errors.py -> build/lib.linux-aarch64-cpython-39/sudachipy
2.478       copying py_src/sudachipy/__init__.py -> build/lib.linux-aarch64-cpython-39/sudachipy
2.478       copying py_src/sudachipy/command_line.py -> build/lib.linux-aarch64-cpython-39/sudachipy
2.478       creating build/lib.linux-aarch64-cpython-39/sudachipy/dictionary
2.478       copying py_src/sudachipy/dictionary/__init__.py -> build/lib.linux-aarch64-cpython-39/sudachipy/dictionary
2.478       creating build/lib.linux-aarch64-cpython-39/sudachipy/tokenizer
2.478       copying py_src/sudachipy/tokenizer/__init__.py -> build/lib.linux-aarch64-cpython-39/sudachipy/tokenizer
2.478       creating build/lib.linux-aarch64-cpython-39/sudachipy/morphemelist
2.478       copying py_src/sudachipy/morphemelist/__init__.py -> build/lib.linux-aarch64-cpython-39/sudachipy/morphemelist
2.478       creating build/lib.linux-aarch64-cpython-39/sudachipy/morpheme
2.478       copying py_src/sudachipy/morpheme/__init__.py -> build/lib.linux-aarch64-cpython-39/sudachipy/morpheme
2.478       copying py_src/sudachipy/sudachipy.pyi -> build/lib.linux-aarch64-cpython-39/sudachipy
2.478       creating build/lib.linux-aarch64-cpython-39/sudachipy/resources
2.478       copying py_src/sudachipy/resources/sudachi.json -> build/lib.linux-aarch64-cpython-39/sudachipy/resources
2.478       copying py_src/sudachipy/resources/char.def -> build/lib.linux-aarch64-cpython-39/sudachipy/resources
2.478       copying py_src/sudachipy/resources/unk.def -> build/lib.linux-aarch64-cpython-39/sudachipy/resources
2.478       copying py_src/sudachipy/resources/rewrite.def -> build/lib.linux-aarch64-cpython-39/sudachipy/resources
2.478       running build_ext
2.478       running build_rust
2.478       error: can't find Rust compiler
2.478
2.478       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.
2.478
2.478       To update pip, run:
2.478
2.478           pip install --upgrade pip
2.478
2.478       and then retry package installation.
2.478
2.478       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.
2.478       [end of output]
2.478
2.478   note: This error originates from a subprocess, and is likely not a problem with pip.
2.479   ERROR: Failed building wheel for SudachiPy
2.479 Failed to build SudachiPy
2.479 ERROR: Could not build wheels for SudachiPy, which is required to install pyproject.toml-based projects
------
failed to solve: process "/bin/sh -c pip install SudachiPy" did not complete successfully: exit code: 1

この中身をよく見ると、、

2.478       error: can't find Rust compiler
2.478
2.478       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.

うん、どうやらrustのコンパイラがないらしい。それでは入れるしかない。

RUSTのインストール

RUSTの公式をみてみるとどうやら以下のようにインストールができるらしい

# rustのインストール先を指定
ENV PATH=$PATH:/root/.cargo/bin
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

しかし、これだとexit 1となってしまう。curlにあまり詳しくないがどうやらネットワーク系のエラーらしい。
そこで、https://sh.rustup.rs内容をコンテナにもってきてからそのシェルファイルを持ってくるようにしてみる。

# rustのインストール先を指定
ENV PATH=$PATH:/root/.cargo/bin
RUN curl https://sh.rustup.rs -sSf > /rust.sh
RUN sh /rust.sh -y

成功した!

最後に

mac特有の問題なのかな、、と思ってググってみたらまったくおんなじような記事を見つけてしまった、、、、
[Mac / python / sudachipy] pip install 時にMacマシンでのみ error: can't find Rust compiler 発生
怒られたら消しますが、エラーハンドリングの様子も書いているので、多少は差別化できてるかな、、、、

今後は、cudaを利用するためのGiNZA環境の作成とかできたらなと思います。

Discussion