ブラックボックス

プログラミングのノウハウやライフハックをどんどん投稿していきたい

Jun 26, 2017 - 2 minute read - ノウハウ

【PHP】Composerインストール時にConnection timed outが発生する

Vagrantで新たに環境を構築して色々試そうとした所、Composerのインストール時にConnection timed outが発生し、インストールが完了しなかった。
解決したのでメモ。

環境:

  • Vagrant:1.9.3
  • VirtualBox:5.1.22
  • ゲストOS:CentOS7.3

参考: Connection timed out #4142

一応失敗時のログ。
まずはComposerのダウンロードから。

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
PHP Warning:  copy(https://getcomposer.org/installer): failed to open stream: Connection timed out in Command line code on line 1

この時点で既におかしかった。
仕方がないのでwgetでダウンロード。

$ wget https://getcomposer.org/installer
--2017-06-26 12:21:26--  https://getcomposer.org/installer
Resolving getcomposer.org (getcomposer.org)... 2001:41d0:a:7b19::2, 87.98.253.108
Connecting to getcomposer.org (getcomposer.org)|2001:41d0:a:7b19::2|:443... failed: Connection timed out.
Connecting to getcomposer.org (getcomposer.org)|87.98.253.108|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 305728 (299K) [application/octet-stream]
Saving to: ‘installer’

100%[======================================>] 305,728     21.4KB/s   in 14s

2017-06-26 12:23:49 (21.4 KB/s) - ‘installer’ saved [305728/305728]

よく見ると、4行目と5行目、IPv6で失敗していて、IPv4でつなぎ直している。

ひとまず成功したので続き。

$ mv installer composer-setup.php
$ php composer-setup.php
All settings correct for using Composer
Downloading...
The "https://getcomposer.org/versions" file could not be downloaded: failed to open stream: Connection timed out
Retrying...
The "https://getcomposer.org/versions" file could not be downloaded: failed to open stream: Connection timed out
Retrying...
The "https://getcomposer.org/versions" file could not be downloaded: failed to open stream: Connection timed out
The download failed repeatedly, aborting.

ここでタイムアウトが発生してどうにもならなくなった。

そこで参考に記載してある通りのコマンドを実行。

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

その後再度インストール。

$ php composer-setup.php                                   All settings correct for using Composer
Downloading...

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

これで成功。
wgetでも発生していた通り、IPv6に繋がらないのにIPv6を優先して繋いでいたことが原因のよう。
そもそもIPv6に繋がらないのは何故なのかというのもあるが。

解決のコマンド実行後にwgetをすると、IPv4にのみ繋ぎに行くようになっている。

$ wget https://getcomposer.org/installer                   --2017-06-26 12:58:40--  https://getcomposer.org/installer
Resolving getcomposer.org (getcomposer.org)... 87.98.253.108, 2001:41d0:a:7b19::2
Connecting to getcomposer.org (getcomposer.org)|87.98.253.108|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 305728 (299K) [application/octet-stream]
Saving to: ‘installer’

100%[======================================>] 305,728     27.1KB/s   in 11s

2017-06-26 12:58:52 (27.1 KB/s) - ‘installer’ saved [305728/305728]