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

DockerHub Composer

DockerHub Composer
https://hub.docker.com/_/composer

Docker 17.05 以降では、 Dockerfile 内で他のイメージを指定して組み入れることが可能。

Dockerfile

COPY --from=composer /usr/bin/composer /usr/bin/composer

composer remove [package]

# composer remove vendor/package

remove / rm

Command-line interface / Commands - Composer

The remove command removes packages from the composer.json file from the current directory.

# php composer.phar remove vendor/package vendor/package2

Note) php composer.phar = composer

After removing the requirements, the modified requirements will be uninstalled.

Options

  • –unused Remove unused packages that are not a direct or indirect dependency (anymore)
  • –dev: Remove packages from require-dev.
  • –dry-run: Simulate the command without actually doing anything.
  • –no-progress: Removes the progress display that can mess with some terminals or scripts which don’t handle backspace characters.
  • –no-update: Disables the automatic update of the dependencies (implies --no-install).
  • –no-install: Does not run the install step after updating the composer.lock file.
  • –no-audit: Does not run the audit steps after installation is complete. Also see COMPOSER_NO_AUDIT.
  • –audit-format: Audit output format. Must be “table”, “plain”, “json”, or “summary” (default).
  • –update-no-dev: Run the dependency update with the --no-dev option. Also see COMPOSER_NO_DEV.
  • –update-with-dependencies (-w): Also update dependencies of the removed packages. (Deprecated, is now default behavior)
  • –update-with-all-dependencies (-W): Allows all inherited dependencies to be updated, including those that are root requirements.
  • –ignore-platform-reqs: ignore all platform requirements (php, hhvm, lib-* and ext-*) and force the installation even if the local machine does not fulfill these. See also the platform config option.
  • –ignore-platform-req: ignore a specific platform requirement(php, hhvm, lib-* and ext-*) and force the installation even if the local machine does not fulfill it. Multiple requirements can be ignored via wildcard.
  • –optimize-autoloader (-o): Convert PSR-0/4 autoloading to classmap to get a faster autoloader. This is recommended especially for production, but can take a bit of time to run so it is currently not done by default.
  • –classmap-authoritative (-a): Autoload classes from the classmap only. Implicitly enables --optimize-autoloader.
  • –apcu-autoloader: Use APCu to cache found/not-found classes.
  • –apcu-autoloader-prefix: Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader.