pip install grpcio のインストールで死ぬ (M1 Mac)

# Env

- MacBook Pro (13-inch, M1, 2020)
- OS macOS Monterey 12.3.1


# Failure

<details>

```
...
                                              _Nonnull
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:361:13: warning: pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified) [-Wnullability-completeness]
      extern char *suboptarg;         /* getsubopt(3) external variable */
                  ^
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:361:13: note: insert '_Nullable' if the pointer may be null
      extern char *suboptarg;         /* getsubopt(3) external variable */
                  ^
                    _Nullable
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:361:13: note: insert '_Nonnull' if the pointer should never be null
      extern char *suboptarg;         /* getsubopt(3) external variable */
                  ^
                    _Nonnull
      In file included from /var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/tmpc_zn2v2r/a.c:2:
      In file included from /Users/m.naka/.pyenv/versions/3.9.4/include/python3.9/Python.h:136:
      In file included from /Users/m.naka/.pyenv/versions/3.9.4/include/python3.9/pythread.h:128:
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:328:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
      #if !_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT
           ^
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:197:2: note: expanded from macro '_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT'
              defined(SWIFT_CLASS_EXTRA) && (!defined(SWIFT_SDK_OVERLAY_PTHREAD_EPOCH) || (SWIFT_SDK_OVERLAY_PTHREAD_EPOCH < 1))
              ^
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:328:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:197:34: note: expanded from macro '_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT'
              defined(SWIFT_CLASS_EXTRA) && (!defined(SWIFT_SDK_OVERLAY_PTHREAD_EPOCH) || (SWIFT_SDK_OVERLAY_PTHREAD_EPOCH < 1))
                                              ^
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:537:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
      #if !_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT
           ^
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:197:2: note: expanded from macro '_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT'
              defined(SWIFT_CLASS_EXTRA) && (!defined(SWIFT_SDK_OVERLAY_PTHREAD_EPOCH) || (SWIFT_SDK_OVERLAY_PTHREAD_EPOCH < 1))
              ^
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:537:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
      /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:197:34: note: expanded from macro '_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT'
              defined(SWIFT_CLASS_EXTRA) && (!defined(SWIFT_SDK_OVERLAY_PTHREAD_EPOCH) || (SWIFT_SDK_OVERLAY_PTHREAD_EPOCH < 1))
                                              ^
      197 warnings generated.
      Traceback (most recent call last):
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/unixccompiler.py", line 117, in _compile
          self.spawn(compiler_so + cc_args + [src, '-o', obj] +
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/_spawn_patch.py", line 54, in _commandfile_spawn
          _classic_spawn(self, command)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/ccompiler.py", line 910, in spawn
          spawn(cmd, dry_run=self.dry_run)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/spawn.py", line 87, in spawn
          raise DistutilsExecError(
      distutils.errors.DistutilsExecError: command '/usr/bin/clang' failed with exit code 1
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/commands.py", line 280, in build_extensions
          build_ext.build_ext.build_extensions(self)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/command/build_ext.py", line 449, in build_extensions
          self._build_extensions_serial()
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/command/build_ext.py", line 474, in _build_extensions_serial
          self.build_extension(ext)
        File "/Users/m.naka/repos/nakamasato/bazel-training/venv/lib/python3.9/site-packages/setuptools/command/build_ext.py", line 208, in build_extension
          _build_ext.build_extension(self, ext)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/command/build_ext.py", line 529, in build_extension
          objects = self.compiler.compile(sources,
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/_parallel_compile_patch.py", line 58, in _parallel_compile
          multiprocessing.pool.ThreadPool(BUILD_EXT_COMPILER_JOBS).map(
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/multiprocessing/pool.py", line 364, in map
          return self._map_async(func, iterable, mapstar, chunksize).get()
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/multiprocessing/pool.py", line 771, in get
          raise self._value
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/multiprocessing/pool.py", line 125, in worker
          result = (True, func(*args, **kwds))
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/multiprocessing/pool.py", line 48, in mapstar
          return list(map(*args))
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/_parallel_compile_patch.py", line 54, in _compile_single_file
          self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/commands.py", line 263, in new_compile
          return old_compile(obj, src, ext, cc_args, extra_postargs,
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/unixccompiler.py", line 120, in _compile
          raise CompileError(msg)
      distutils.errors.CompileError: command '/usr/bin/clang' failed with exit code 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/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/setup.py", line 540, in <module>
          setuptools.setup(
        File "/Users/m.naka/repos/nakamasato/bazel-training/venv/lib/python3.9/site-packages/setuptools/__init__.py", line 165, in setup
          return distutils.core.setup(**attrs)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/core.py", line 148, in setup
          dist.run_commands()
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/dist.py", line 966, in run_commands
          self.run_command(cmd)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/Users/m.naka/repos/nakamasato/bazel-training/venv/lib/python3.9/site-packages/setuptools/command/install.py", line 61, in run
          return orig.install.run(self)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/command/install.py", line 546, in run
          self.run_command('build')
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/command/build.py", line 135, in run
          self.run_command(cmd_name)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/Users/m.naka/repos/nakamasato/bazel-training/venv/lib/python3.9/site-packages/setuptools/command/build_ext.py", line 87, in run
          _build_ext.run(self)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/command/build_ext.py", line 340, in run
          self.build_extensions()
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/commands.py", line 284, in build_extensions
          raise CommandError(
      commands.CommandError: Failed `build_ext` step:
      Traceback (most recent call last):
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/unixccompiler.py", line 117, in _compile
          self.spawn(compiler_so + cc_args + [src, '-o', obj] +
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/_spawn_patch.py", line 54, in _commandfile_spawn
          _classic_spawn(self, command)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/ccompiler.py", line 910, in spawn
          spawn(cmd, dry_run=self.dry_run)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/spawn.py", line 87, in spawn
          raise DistutilsExecError(
      distutils.errors.DistutilsExecError: command '/usr/bin/clang' failed with exit code 1
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/commands.py", line 280, in build_extensions
          build_ext.build_ext.build_extensions(self)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/command/build_ext.py", line 449, in build_extensions
          self._build_extensions_serial()
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/command/build_ext.py", line 474, in _build_extensions_serial
          self.build_extension(ext)
        File "/Users/m.naka/repos/nakamasato/bazel-training/venv/lib/python3.9/site-packages/setuptools/command/build_ext.py", line 208, in build_extension
          _build_ext.build_extension(self, ext)
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/command/build_ext.py", line 529, in build_extension
          objects = self.compiler.compile(sources,
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/_parallel_compile_patch.py", line 58, in _parallel_compile
          multiprocessing.pool.ThreadPool(BUILD_EXT_COMPILER_JOBS).map(
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/multiprocessing/pool.py", line 364, in map
          return self._map_async(func, iterable, mapstar, chunksize).get()
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/multiprocessing/pool.py", line 771, in get
          raise self._value
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/multiprocessing/pool.py", line 125, in worker
          result = (True, func(*args, **kwds))
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/multiprocessing/pool.py", line 48, in mapstar
          return list(map(*args))
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/_parallel_compile_patch.py", line 54, in _compile_single_file
          self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
        File "/private/var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/pip-install-mnmq5b97/grpcio_bddda0f598a3495da32a091c18f631b6/src/python/grpcio/commands.py", line 263, in new_compile
          return old_compile(obj, src, ext, cc_args, extra_postargs,
        File "/Users/m.naka/.pyenv/versions/3.9.4/lib/python3.9/distutils/unixccompiler.py", line 120, in _compile
          raise CompileError(msg)
      distutils.errors.CompileError: command '/usr/bin/clang' failed with exit code 1
      
      [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.
╰─> grpcio

note: This is an issue with the package mention
```

</details>

# Solution

## 1. Reinstall python

結局いろんなことしたあとにreinstallしたらうまく行った

```
pyenv uninstall 3.9.9
```

```
pyenv install 3.9.9
```

<details>

```
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.9.9.tar.xz...
-> https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tar.xz
Installing Python-3.9.9...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.9.9 to /Users/m.naka/.pyenv/versions/3.9.9
pyenv shell 3.9.9
```

</details>

## 1. Install grpcio

```
pip install grpcio
Collecting grpcio
  Using cached grpcio-1.49.1.tar.gz (22.1 MB)
Collecting six>=1.5.2
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Using legacy 'setup.py install' for grpcio, since package 'wheel' is not installed.
Installing collected packages: six, grpcio
    Running setup.py install for grpcio ... done
Successfully installed grpcio-1.49.1 six-1.16.0
WARNING: You are using pip version 21.2.4; however, version 22.3 is available.
You should consider upgrading via the '/Users/m.naka/.pyenv/versions/3.9.9/bin/python3.9 -m pip install --upgrade pip' command.
```

## うまく行かなかったSolution

1. https://github.com/grpc/grpc/issues/30064#issuecomment-1236072966

    ```
    GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=true GRPC_PYTHON_BUILD_SYSTEM_ZLIB=true pip install grpcio
    ```

1. https://github.com/grpc/grpc/issues/24677#issuecomment-729983060
    ```
    pip install wheel && GRPC_BUILD_WITH_BORING_SSL_ASM="" GRPC_PYTHON_BUILD_SYSTEM_RE2=true GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=true GRPC_PYTHON_BUILD_SYSTEM_ZLIB=true pip install grpcio
    ```
1. https://github.com/grpc/grpc/issues/24677#issuecomment-735218532

1. https://github.com/grpc/grpc/issues/24677#issuecomment-741814641
    ```
    GRPC_BUILD_WITH_BORING_SSL_ASM="" GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=true GRPC_PYTHON_BUILD_SYSTEM_ZLIB=true pip install grpcio
    ```
1. https://qiita.com/ffggss/items/721fc5464470e6d3b545
    ```
    pip install wheel && GRPC_PYTHON_BUILD_SYSTEM_ZLIB=true pip install grpcio
    ```

# Rerefences

1. https://qiita.com/niccari/items/a1d5297de929853bf314
1. https://qiita.com/ffggss/items/721fc5464470e6d3b545
1. https://github.com/grpc/grpc/issues/25082
1. https://github.com/grpc/grpc/issues/30064
1. https://github.com/grpc/grpc/issues/24677
1. https://github.com/grpc/grpc/issues/28387
1. https://qiita.com/sizumita/items/c973edbdcdf360a3a9f1