LoginSignup
7
6

More than 1 year has passed since last update.

Laravel/sail環境でcomposerがエラーになる

Last updated at Posted at 2022-01-24

概要

下記のLaravel/Sailを使ってdocker環境を構築し、使っていました。
ある日、buildするとcomposer installでエラーになったので調べてみた。

・Github - laravel/sail
 https://github.com/laravel/sail/blob/1.x/runtimes/8.0/Dockerfile

エラー内容

以下のようなエラーが発生した。

 => ERROR [local/sail-8.0 18/23] RUN composer install                                                                                                                                   1.1s
------
 > [local/sail-8.0 18/23] RUN composer install:
#38 0.735 Do not run Composer as root/super user! See https://getcomposer.org/root for details
#38 0.946 Installing dependencies from lock file (including require-dev)
#38 0.964 Verifying lock file contents can be installed on current platform.
#38 1.004 Your lock file does not contain a compatible set of packages. Please run composer update.
#38 1.004 
#38 1.004   Problem 1
#38 1.004     - Root composer.json requires PHP extension ext-curl * but it is missing from your system. Install or enable PHP's curl extension.

...

#38 1.004 To enable extensions, verify that they are enabled in your .ini files:
#38 1.004     - /etc/php/8.1/cli/php.ini

...

#38 1.004     - /etc/php/8.1/cli/conf.d/20-tokenizer.ini
#38 1.004 You can also run `php --ini` in a terminal to see which files are used by PHP in CLI mode.
#38 1.004 Alternatively, you can run Composer with `--ignore-platform-req=ext-curl --ignore-platform-req=ext-gd --ignore-platform-req=ext-zip --ignore-platform-req=ext-simplexml --ignore-platform-req=ext-mbstring --ignore-platform-req=ext-mbstring --ignore-platform-req=ext-dom --ignore-platform-req=ext-dom --ignore-platform-req=ext-curl --ignore-platform-req=ext-dom --ignore-platform-req=ext-dom --ignore-platform-req=ext-dom --ignore-platform-req=ext-dom --ignore-platform-req=ext-simplexml` to temporarily ignore these required extensions.
------
failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c composer install]: exit code: 2

PHP拡張がインストールされていない?」といった感じでしょうか?。

しかし、Dockerfileを見てみるとちゃんとインストールされています。

RUN apt-get update \
    ...
    && apt-get install -y php8.0-cli php8.0-dev \
       php8.0-pgsql php8.0-sqlite3 php8.0-gd \
       php8.0-curl php8.0-memcached \
       php8.0-imap php8.0-mysql php8.0-mbstring \
       php8.0-xml php8.0-zip php8.0-bcmath php8.0-soap \
       php8.0-intl php8.0-readline php8.0-pcov \
       php8.0-msgpack php8.0-igbinary php8.0-ldap \
       php8.0-redis php8.0-swoole php8.0-xdebug \
   ...

下記のサイトに記載のような「php-intl」が入っていないというのも該当しないようです。

・【CakePHP】composerによるインストールでエラーが!
 https://deep-blog.jp/engineer/15243/

ふとPHPのバージョンを確認してみると...

# コンテナ内に入る
$ ./sail shell

# バージョン確認
$ php -v

PHP 8.1.1 (cli) (built: Dec 31 2021 07:26:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies

あれ?、PHP8.0を使っていたはずなのに、PHP8.1になっている...

これが原因かな?

対処方法を調べる

とりあえず、PHPコマンドを調べてみる。

# PHPコマンドの場所を確認する
$ which php
/usr/bin/php

# PHPコマンドを確認する
$ ls -la /usr/bin
/usr/bin/php → /etc/alternatives/php

※ 「alternatives」のシンボリックリンクになっているようです

Google先生に聞いてみると、複数のバージョンのPHPをインストールして切り替えできる機能のようです。

さっそくコマンドで現在の状況を確認してみる。

$ update-alternatives --list php

/usr/bin/php8.0
/usr/bin/php8.1

PHP8.0とPHP8.1がインストールされているようです。
PHP8.0に切り替えてみる。

$ update-alternatives --set php /usr/bin/php8.0

update-alternatives: using /usr/bin/php8.0 to provide /usr/bin/php (php) in manual mode
update-alternatives: warning: skip creation of /usr/share/man/man1/php.1.gz because associated file /usr/share/man/man1/php8.0.1.gz (of link group php) doesn't exist

※ 警告が出たが、マニュアル関係のようなので影響なしと判断する

再度、バージョンを確認してみる。

$ php -v

PHP 8.0.14 (cli) (built: Dec 20 2021 21:23:16) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.14, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.14, Copyright (c), by Zend Technologies

composer installを再度試したところ、正常にインストールが完了しました。

まとめ

Dockerfileに下記を追加したら、不具合は解消されました。

FROM ubuntu:21.04

LABEL maintainer="Taylor Otwell"

...

# ↓ ここを追加する!!!!!!
RUN update-alternatives --set php /usr/bin/php8.0 

RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.0

RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail

COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/8.0/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container

EXPOSE 8000

ENTRYPOINT ["start-container"]

最新のコードを見たら、2022/01/20に修正されたようですね。

以上

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6