HRR Co., Ltd.

技術的な記録を残していくことを目的としています。1次情報を大事にしています。

Composer本体をバージョン指定でダウンロードする

はじめに

そんなに難しい話ではなく、ヘルプを見れば書いてあるのですが…メモがてら記載します。

公式サイトはこちら:
getcomposer.org

まずは本体のダウンロード

このへんは公式サイトのコマンドのとおりです。
※2行目はファイルの更新によりHash値が変わるので、公式サイトを見るのが無難です!

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified

このあとcomposer-setup.phpを叩くのですが…
ここでヘルプを見ると、バージョン指定に限らずいろんなオプションがあることがわかります。

$ php composer-setup.php --help
Composer Installer
------------------
Options
--help               this help
--check              for checking environment only
--force              forces the installation
--ansi               force ANSI color output
--no-ansi            disable ANSI color output
--quiet              do not output unimportant messages
--install-dir="..."  accepts a target installation directory
--preview            install the latest version from the preview (alpha/beta/rc) channel instead of stable
--snapshot           install the latest version from the snapshot (dev builds) channel instead of stable
--1                  install the latest stable Composer 1.x version
--2                  install the latest stable Composer 2.x version
--version="..."      accepts a specific version to install instead of the latest
--filename="..."     accepts a target filename (default: composer.phar)
--disable-tls        disable SSL/TLS security for file downloads
--cafile="..."       accepts a path to a Certificate Authority (CA) certificate file for SSL/TLS verification

今回はバージョン指定なので、--versionオプションを付けて、バージョンを明示してあげることで可能となります。

$ php composer-setup.php --version="2.1.3"
All settings correct for using Composer
Downloading...

Composer (version 2.1.3) successfully installed to: /home/apuser/composer.phar
Use it: php composer.phar

使い方

ダウンロードできたcomposer.pharファイルの使い方は、上記最後に表示されているように、

php composer.phar --version

みたいにするだけです。
(上記はバージョン情報の確認コマンド)

公式サイトには/usr/local/bin/配下に持っていくような流れですが、バージョン指定したものはグローバルな場所に置くときには要注意です。

以上でした!