🐍

macOSにLightGBMにインストールする(legacy-install-failureも解決)

2022/09/23に公開

以下の環境でLightGBMのpython packageをインストールします。

macOS Monterey 12.6
チップ Apple M1
Python 3.10.7 (pyenvでインストール)
pip 22.2.2

GithubのREADMEYou may need to install wheel via pip install wheel first.とあるのでまずwheelのインストールをします。

pip install wheel

この状態でpip install lightgbmLightGBMをインストールしようとすると以下のようなエラーが出ます。

Failed to build lightgbm
Installing collected packages: lightgbm
  Running setup.py install for lightgbm ... error
  error: subprocess-exited-with-error

  × Running setup.py install for lightgbm did not run successfully.
  │ exit code: 1
  ╰─> [39 lines of output]
      running install
      /Users/nrhk/.anyenv/envs/pyenv/versions/3.10.7/lib/python3.10/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
        warnings.warn(
      INFO:LightGBM:Starting to compile the library.
      INFO:LightGBM:Starting to compile with CMake.
      Traceback (most recent call last):
        File "/private/var/folders/3c/dhwmsmq51cg8_xb6bns7pmzm0000gn/T/pip-install-9siym08j/lightgbm_42909798c54a4cccb2f1db353162c4e4/setup.py", line 95, in silent_call
          subprocess.check_call(cmd, stderr=log, stdout=log)
        File "/Users/nrhk/.anyenv/envs/pyenv/versions/3.10.7/lib/python3.10/subprocess.py", line 369, in check_call
          raise CalledProcessError(retcode, cmd)
      subprocess.CalledProcessError: Command '['cmake', '/private/var/folders/3c/dhwmsmq51cg8_xb6bns7pmzm0000gn/T/pip-install-9siym08j/lightgbm_42909798c54a4cccb2f1db353162c4e4/compile']' returned non-zero exit status 1.

      During handling of the above exception, another exception occurred:

      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/private/var/folders/3c/dhwmsmq51cg8_xb6bns7pmzm0000gn/T/pip-install-9siym08j/lightgbm_42909798c54a4cccb2f1db353162c4e4/setup.py", line 334, in <module>
          setup(name='lightgbm',
        File "/Users/nrhk/.anyenv/envs/pyenv/versions/3.10.7/lib/python3.10/site-packages/setuptools/__init__.py", line 87, in setup
          return distutils.core.setup(**attrs)
        File "/Users/nrhk/.anyenv/envs/pyenv/versions/3.10.7/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 177, in setup
          return run_commands(dist)
        File "/Users/nrhk/.anyenv/envs/pyenv/versions/3.10.7/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 193, in run_commands
          dist.run_commands()
        File "/Users/nrhk/.anyenv/envs/pyenv/versions/3.10.7/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 968, in run_commands
          self.run_command(cmd)
        File "/Users/nrhk/.anyenv/envs/pyenv/versions/3.10.7/lib/python3.10/site-packages/setuptools/dist.py", line 1217, in run_command
          super().run_command(command)
        File "/Users/nrhk/.anyenv/envs/pyenv/versions/3.10.7/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 987, in run_command
          cmd_obj.run()
        File "/private/var/folders/3c/dhwmsmq51cg8_xb6bns7pmzm0000gn/T/pip-install-9siym08j/lightgbm_42909798c54a4cccb2f1db353162c4e4/setup.py", line 248, in run
          compile_cpp(use_mingw=self.mingw, use_gpu=self.gpu, use_cuda=self.cuda, use_mpi=self.mpi,
        File "/private/var/folders/3c/dhwmsmq51cg8_xb6bns7pmzm0000gn/T/pip-install-9siym08j/lightgbm_42909798c54a4cccb2f1db353162c4e4/setup.py", line 198, in compile_cpp
          silent_call(cmake_cmd, raise_error=True, error_msg='Please install CMake and all required dependencies first')
        File "/private/var/folders/3c/dhwmsmq51cg8_xb6bns7pmzm0000gn/T/pip-install-9siym08j/lightgbm_42909798c54a4cccb2f1db353162c4e4/setup.py", line 99, in silent_call
          raise Exception("\n".join((error_msg, LOG_NOTICE)))
      Exception: Please install CMake and all required dependencies first
      The full version of error log was saved into /Users/nrhk/LightGBM_compilation.log
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> lightgbm

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

エラー文を読んでもよくわかりません。legacy-install-failuresubprocess-exited-with-errorあたりでググっても解決策は出てきませんでした。

もう少し読み込むと、以下のような記載を見つけました。

Please install CMake and all required dependencies first The full version of error log was saved into

CMakeが必要なのかな?と、インストールをしましたが解決しません。

なんでだろう?と思いましたがREADMEにちゃんとFor macOSの項があり、以下のような記載があります。

Starting from version 2.2.1, the library file in distribution wheels is built by the Apple Clang (Xcode_8.3.3 for versions 2.2.1 - 2.3.1, Xcode_9.4.1 for versions 2.3.2 - 3.3.2 and Xcode_10.3 from version 4.0.0) compiler. This means that you don't need to install the gcc compiler anymore. Instead of that you need to install the OpenMP library, which is required for running LightGBM on the system with the Apple Clang compiler. You can install the OpenMP library by the following command: brew install libomp.

新しいバージョン(2.2.1以降)についてはAppleClangでコンパイルしている(gccは使わなくなった)と、LightGBMのコンパイル時にはOpenMPが必要なのでインストールしましょう、ということです。

この辺はエラー文読んでもたどり着けなかったので最初から公式ドキュメントにあたるべきでしたね。

brew install libomp

最後に目的のLightGBMをインストールします。

pip install lightgbm

以上です。

Discussion