「composer require linecorp/line-bot-sdk」がインストールできなくて躓いたお話

LaravelでLINEのMessaging APIを使った開発中に起きたエラーで解決まで時間がかかったので自分と同じことで躓く人の助けになればと記事にしました。

## 環境
・macOS Catalina v10.15.7
・Docker v20.10.5
・PHP v7.4
・Laravel v6.0.*
・MySQL v5.7

## エラー内容
エラー内容は要約すると、「linecorp/line-bot-sdk 7.0.0で要求されたPHP拡張機能socketsがねーぞ」ということでした。

```terminal:ターミナル
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for linecorp/line-bot-sdk ^7.0 -> satisfiable by linecorp/line-bot-sdk[7.0.0].
    - linecorp/line-bot-sdk 7.0.0 requires ext-sockets * -> the requested PHP extension sockets is missing from your system.

  To enable extensions, verify that they are enabled in your .ini files:
    - /usr/local/etc/php/php.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-bcmath.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-intl.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-zip.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

Installation failed, reverting ./composer.json to its original content.
```

## ぐぐった結果
こちらの記事を見つけました！

https://qiita.com/mktro/items/d164bd4a88fc51f2a99d

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/838372/f0415da2-9853-13a8-f0a0-3c59dbd523d2.png)

ということで意気揚々と`php.ini`に追記しました。

```ini:php.ini
zend.exception_ignore_args = off
expose_php = on
max_execution_time = 30
max_input_vars = 1000
upload_max_filesize = 64M
post_max_size = 128M
memory_limit = 256M
error_reporting = E_ALL
display_errors = on
display_startup_errors = on
log_errors = on
error_log = /dev/stderr
default_charset = UTF-8
extension=sockets

[Date]
date.timezone = Asia/Tokyo

[mysqlnd]
mysqlnd.collect_memory_statistics = on

[Assertion]
zend.assertions = 1

[mbstring]
mbstring.language = Japanese
```

## しかし・・
何も変わらんどころかなんか**Warning**出とんぞ

```terminal:ターミナル
Warning: PHP Startup: Unable to load dynamic library 'sockets' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/sockets (/usr/local/lib/php/extensions/no-debug-non-zts-20190902/sockets: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20190902/sockets.so (/usr/local/lib/php/extensions/no-debug-non-zts-20190902/sockets.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

  Problem 1
    - Installation request for linecorp/line-bot-sdk ^7.0 -> satisfiable by linecorp/line-bot-sdk[7.0.0].
    - linecorp/line-bot-sdk 7.0.0 requires ext-sockets * -> the requested PHP extension sockets is missing from your system.

  To enable extensions, verify that they are enabled in your .ini files:
    - /usr/local/etc/php/php.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-bcmath.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-intl.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-zip.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

Installation failed, reverting ./composer.json to its original content.
```

## もう一度エラーメッセージをじっくり見ると

「`sockets`がないだけなら、Dockerfileでインストールすればいいんじゃね」とひらめきました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/838372/afc2e984-e741-0e43-461e-96048228231d.png)

ということで、Dockerfileを書き直しました。

```docker:Dockerfile
FROM php:7.4-fpm-buster

# コンテナ内の環境変数
ENV COMPOSER_ALLOW_SUPERUSER=1 \
  COMPOSER_HOME=/composer

# composer
COPY --from=composer:1.10 /usr/bin/composer /usr/bin/composer

# node
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install -y nodejs

# package
RUN apt-get update && \
  apt-get -y install git unzip libzip-dev libicu-dev libonig-dev && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* && \
  docker-php-ext-install intl pdo_mysql zip bcmath sockets

# phpの設定ファイル
COPY ./php.ini /usr/local/etc/php/php.ini

# ディレクトリ
WORKDIR /laravel
```

以下の行で`sockets`を追加しました！

```
docker-php-ext-install intl pdo_mysql zip bcmath sockets
```

## 実行
```terminal:ターミナル
Using version ^7.0 for linecorp/line-bot-sdk
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing linecorp/line-bot-sdk (7.0.0): Downloading (100%)         
linecorp/line-bot-sdk suggests installing apigen/apigen (Install with roave/better-reflection:dev-master to generate docs)
linecorp/line-bot-sdk suggests installing roave/better-reflection (Required by apigen/apigen:dev-master)
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: linecorp/line-bot-sdk
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
67 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
```

インストール成功です！！

## 感想
[こちらの記事](https://qiita.com/mktro/items/d164bd4a88fc51f2a99d)とエラーメッセージが違うのにも関わらず鵜呑みしたのが原因でした。
やっぱりしっかりエラーメッセージ読みましょう！