Composerのインストール・使い方

ホームフォルダ内で以下のシェルスクリプトを実行。シェルスクリプトには実行権を付与すること。

$ sudo chmod +x composer_install.sh

スクリプトの内容は下記サイトにて確認のこと。

#!/bin/sh

EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
exit $RESULT

スクリプトの実行

$ sudo ./composer_install.sh

composer.pharというファイルが作成されます。composerの実行は、

$ php composer.phar

グローバル環境で実行させるため、/user/local/binにファイル名composerで移動します。

$ sudo mv composer.phar /usr/local/bin/composer

環境変数を確認して実行パスに/usr/local/binが含まれているか確認します。

$ echo $PATH

bashの設定を再読込します。

$ source ~/.profile

任意のディレクトリで"composer"が実行できることを確認して下さい。

$ composer

Composerインストールとその使い方

Ubuntu 20.04へのインストール

Packagist The PHP Package Repository

composer.json バージョン記述ルール

"require": {
    "vendor/package": "1.3.2", // exactly 1.3.2

    // >, <, >=, <= | specify upper / lower bounds
    "vendor/package": ">=1.3.2", // anything above or equal to 1.3.2
    "vendor/package": "<1.3.2", // anything below 1.3.2

    // * | wildcard
    "vendor/package": "1.3.*", // >=1.3.0 <1.4.0

    // ~ | allows last digit specified to go up
    "vendor/package": "~1.3.2", // >=1.3.2 <1.4.0
    "vendor/package": "~1.3", // >=1.3.0 <2.0.0

    // ^ | doesn't allow breaking changes (major version fixed - following semver)
    "vendor/package": "^1.3.2", // >=1.3.2 <2.0.0
    "vendor/package": "^0.3.2", // >=0.3.2 <0.4.0 // except if major version is 0
}

Operation timed out (IPv6 issues)

https://getcomposer.org/doc/articles/troubleshooting.md#operation-timed-out-ipv6-issues-

You may run into errors if IPv6 is not configured correctly. A common error is:

The "https://getcomposer.org/version" file could not be downloaded: failed to
open stream: Operation timed out

We recommend you fix your IPv6 setup. If that is not possible, you can try the following workarounds:

Workaround Linux:

On linux, it seems that running this command helps to make ipv4 traffic have a higher prio than ipv6, which is a better alternative than disabling ipv6 entirely:

sudo sh -c "echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf"

dockerファイルにcomposerを追加する場合

composer_installer.sh
2022/07/23 : スクリプト内容更新

#!/bin/sh

EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
exit $RESULT

PrestashopDocker ImageComposer 、エディターの nano を追加

dockerfile

FROM prestashop/prestashop:1.7-fpm

# Set working directory
WORKDIR /var/www/html

RUN apt-get update && apt-get install -y nano && rm -rf /var/lib/apt/lists/*

# Installing composer
COPY composer_installer.sh /var/www/html
RUN echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf \
    && ./composer_installer.sh && mv composer.phar /usr/local/bin/composer