紙一重の積み重ね

アラフォーのエンジニアがなれる最高の自分を目指して、学んだことをこつこつ情報発信するブログです。

Windows Subsystem for Linux(WSL)上にVagrant環境を構築する際は、gccとmakeを必ずインストールするべし

f:id:yokoyantech:20190125173930p:plain

はじめに

Windows Subsystem for Linux(WSL)上にVagrant環境を構築して、vagrant-awsプラグインをインストールしようとしたら大ハマリしたのでメモ。

実行環境

  • Windows 10 64bit(ver 1803)
    • Windows Subsystem for Linux(WSL)
      • Ubuntu 18.04.1 LTS (Bionic Beaver)
      • Vagrant 2.2.3

やりたいこと

  • WSL上で起動するVagrant環境にvagrant-awsを入れたい

結論

タイトルのとおりです。

Windows Subsystem for Linux(WSL)上にVagrant環境を構築する際は、gccmakeを必ずインストールするべし。

以下、ハマった軌跡です。

発生したエラー

$ sudo vagrant plugin install vagrant-aws を実行したら下記のエラーが発生。

$ sudo vagrant plugin install vagrant-aws
Installing the 'vagrant-aws' plugin. This can take a few minutes...
Building native extensions.  This could take a while...
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:

ERROR: Failed to build gem native extension.

    current directory: /home/yokoyama/.vagrant.d/gems/2.4.4/gems/nokogiri-1.10.1/ext/nokogiri
/opt/vagrant/embedded/bin/ruby -r ./siteconf20190123-101-2zh0km.rb extconf.rb
checking if the C compiler accepts ... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/opt/vagrant/embedded/bin/$(RUBY_BASE_NAME)
        --help
        --clean
/opt/vagrant/embedded/lib/ruby/2.4.0/mkmf.rb:457:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
        from /opt/vagrant/embedded/lib/ruby/2.4.0/mkmf.rb:572:in `block in try_compile'
        from /opt/vagrant/embedded/lib/ruby/2.4.0/mkmf.rb:523:in `with_werror'
        from /opt/vagrant/embedded/lib/ruby/2.4.0/mkmf.rb:572:in `try_compile'
        from extconf.rb:138:in `nokogiri_try_compile'
        from extconf.rb:162:in `block in add_cflags'
        from /opt/vagrant/embedded/lib/ruby/2.4.0/mkmf.rb:630:in `with_cflags'
        from extconf.rb:161:in `add_cflags'
        from extconf.rb:416:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /home/yokoyama/.vagrant.d/gems/2.4.4/extensions/x86_64-linux/2.4.0/nokogiri-1.10.1/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/yokoyama/.vagrant.d/gems/2.4.4/gems/nokogiri-1.10.1 for inspection.
Results logged to /home/yokoyama/.vagrant.d/gems/2.4.4/extensions/x86_64-linux/2.4.0/nokogiri-1.10.1/gem_make.out

エラー解析

mkmf.logの確認

  • 発生したエラーに記載されているログファイルを確認。
  • gcc: not foundとのこと。
"gcc -o conftest -I/opt/vagrant/embedded/include/ruby-2.4.0/x86_64-linux -I/opt/vagrant/embedded/include/ruby-2.4.0/ruby/backward -I/opt/vagrant/embedded/include/ruby-2.4.0 -I. -I/opt/vagrant/embedded/include -I/opt/vagrant/embedded/include   -I/opt/vagrant/embedded/include -fPIC  conftest.c  -L. -L/opt/vagrant/embedded/lib -Wl,-rpath,/opt/vagrant/embedded/lib -L/opt/vagrant/embedded/lib -Wl,-rpath,/opt/vagrant/embedded/lib -L. -L/opt/vagrant/embedded/lib -L/opt/vagrant/embedded/lib64 -Wl,-rpath=XORIGIN/../lib:XORIGIN/../lib64:/opt/vagrant/embedded/lib:/opt/vagrant/embedded/lib64 -fstack-protector -rdynamic -Wl,-export-dynamic -L/opt/vagrant/embedded/lib  -Wl,-rpath,/opt/vagrant/embedded/lib     -Wl,-rpath,'/../lib' -Wl,-rpath,'/../lib' -lruby  -lpthread -lgmp -ldl -lcrypt -lm   -lc "
sh: 1: gcc: not found
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

gccコマンドの確認

  • 試しにgccを実行してみる。確かにコマンドがない。
$ gcc

Command 'gcc' not found, but can be installed with:

sudo apt install gcc

処置

gccコマンドのインストール

$ sudo apt-get update
$ sudo apt install gcc

gccコマンドの実行

  • 今度はOK
$ gcc
gcc: fatal error: no input files
compilation terminated.

再度vagrant-awsのインストール

$ sudo vagrant plugin install vagrant-aws

今度はエラーが変わった。

発生したエラーメッセージ2

  • のこぎりぃぃぃぃぃぃぃぃ!!!(涙)
  • 今度はmakeがないとのこと。
Installing the 'vagrant-aws' plugin. This can take a few minutes...
Building native extensions.  This could take a while...
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:

ERROR: Failed to build gem native extension.

    current directory: /home/yokoyama/.vagrant.d/gems/2.4.4/gems/nokogiri-1.10.1/ext/nokogiri
/opt/vagrant/embedded/bin/ruby -r ./siteconf20190123-4335-j0nfth.rb extconf.rb
checking if the C compiler accepts ... yes
Building nokogiri using system libraries.
Using pkg-config gem version 1.1.9
checking for libxml-2.0... yes
checking for libxslt... yes
checking for libexslt... yes
checking for xmlParseDoc() in libxml/parser.h... yes
checking for xsltParseStylesheetDoc() in libxslt/xslt.h... yes
checking for exsltFuncRegister() in libexslt/exslt.h... yes
checking for xmlHasFeature()... yes
checking for xmlFirstElementChild()... yes
checking for xmlRelaxNGSetParserStructuredErrors()... yes
checking for xmlRelaxNGSetParserStructuredErrors()... yes
checking for xmlRelaxNGSetValidStructuredErrors()... yes
checking for xmlSchemaSetValidStructuredErrors()... yes
checking for xmlSchemaSetParserStructuredErrors()... yes
creating Makefile

current directory: /home/yokoyama/.vagrant.d/gems/2.4.4/gems/nokogiri-1.10.1/ext/nokogiri
make "DESTDIR=" clean
sh: 1: make: not found

current directory: /home/yokoyama/.vagrant.d/gems/2.4.4/gems/nokogiri-1.10.1/ext/nokogiri
make "DESTDIR="
sh: 1: make: not found

make failed, exit code 127

Gem files will remain installed in /home/yokoyama/.vagrant.d/gems/2.4.4/gems/nokogiri-1.10.1 for inspection.
Results logged to /home/yokoyama/.vagrant.d/gems/2.4.4/extensions/x86_64-linux/2.4.0/nokogiri-1.10.1/gem_make.out

確かにmakeコマンドがない。

$ make

Command 'make' not found, but can be installed with:

sudo apt install make
sudo apt install make-guile

処置2

makeコマンドのインストール

$ sudo apt install make
$ sudo apt install make-guile

3度目のvagrant-awsのインストール

  • やっと入ったー!!!
$ sudo vagrant plugin install vagrant-aws

# 略

Fetching: vagrant-aws-0.7.2.gem (100%)
Installed the plugin 'vagrant-aws (0.7.2)'!

まとめ

nokogiriが悪いと言うより、WSLの罠にハマってしまいました。 エラーメッセージをちゃんと読めば、トラブルシューティングできるなと痛感しました。

焦っていろいろググる前にまずエラーメッセージを読むべし!