garbagetown

個人の日記です

cygwin で rails する

職場のウインドーズrails したいんだけどコマンドプロンプトが大嫌いなのでイバラの道とは知りつつ cygwinrails してみた。

なお、過去の経験上、生の cygwinrails どころか ruby を入れるだけで二人月くらい掛かるので、昨年からもともと ruby が入っている gnupack に oh-my-zsh を入れて使っている。

以下、作業ログ。いつか誰か (三ヶ月後の自分を含む) がググるかもしれないので、長いけどエラーログもすべて貼る。

gem

ruby は 2.2.2 が入っていた。意識低めなので今のところ rbenv は使わない。

gem が入っていなかったので apt-cyg でさくっとインストールする。

$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [i386-cygwin]

$ gem -v
zsh: command not found: gem

$ apt-cyg install rubygems
(snip)
Package ruby-rdoc installed
Package ca-certificates is already installed, skipping
Package cygwin is already installed, skipping
Package rubygems installed

$ gem -v
2.4.8

rails

どうせどこかで失敗するんだろうなと予感しつつ rails を入れたら、お馴染みの nokogiri でコケた。

$ gem install rails -v 4.2.2
(snip)
Fetching: nokogiri-1.6.7.2.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing rails:
        ERROR: Failed to build gem native extension.

    /usr/bin/ruby.exe -r ./siteconf20160121-1180-1s3cu4.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
        --without-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=/usr/bin/$(RUBY_BASE_NAME)
        --help
        --clean
/usr/share/ruby/2.2.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
        from /usr/share/ruby/2.2.0/mkmf.rb:571:in `block in try_compile'
        from /usr/share/ruby/2.2.0/mkmf.rb:522:in `with_werror'
        from /usr/share/ruby/2.2.0/mkmf.rb:571:in `try_compile'
        from extconf.rb:80:in `nokogiri_try_compile'
        from extconf.rb:87:in `block in add_cflags'
        from /usr/share/ruby/2.2.0/mkmf.rb:619:in `with_cflags'
        from extconf.rb:86:in `add_cflags'
        from extconf.rb:336:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /home/.gem/ruby/gems/nokogiri-1.6.7.2 for inspection.
Results logged to /home/.gem/ruby/extensions/x86-cygwin/nokogiri-1.6.7.2/gem_make.out

ざっと調べて libxml2, libxslt, libxml2-devel, libxslt-devel を入れてみても駄目。

$ apt-cyg install libxml2 libxslt libxml2-devel libxslt-devel
(snip)
Package libxml2-devel is already installed, skipping
Package libxslt is already installed, skipping
Package zlib-devel is already installed, skipping
Package libxslt-devel installed

$ gem install rails -v 4.2.2
(snip)
Building native extensions.  This could take a while...
ERROR:  Error installing rails:
        ERROR: Failed to build gem native extension.

    /usr/bin/ruby.exe -r ./siteconf20160121-4560-flmffc.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
        --without-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=/usr/bin/$(RUBY_BASE_NAME)
        --help
        --clean
/usr/share/ruby/2.2.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
        from /usr/share/ruby/2.2.0/mkmf.rb:571:in `block in try_compile'
        from /usr/share/ruby/2.2.0/mkmf.rb:522:in `with_werror'
        from /usr/share/ruby/2.2.0/mkmf.rb:571:in `try_compile'
        from extconf.rb:80:in `nokogiri_try_compile'
        from extconf.rb:87:in `block in add_cflags'
        from /usr/share/ruby/2.2.0/mkmf.rb:619:in `with_cflags'
        from extconf.rb:86:in `add_cflags'
        from extconf.rb:336:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /home/.gem/ruby/gems/nokogiri-1.6.7.2 for inspection.
Results logged to /home/.gem/ruby/extensions/x86-cygwin/nokogiri-1.6.7.2/gem_make.out

ログを確認すると /usr/lib/gcc/i686-pc-cygwin/4.9.2/../../../../i686-pc-cygwin/bin/ld: -lgmp が見つかりません とのことだったので、libgmp-devel を入れたら無事に rails が入った。

$ apt-cyg install libgmp-devel
(snip)
Package gmp installed
Package cygwin is already installed, skipping
Package libgmp-devel installed

$ gem install rails -v 4.2.2
(snip)
Done installing documentation for rack, concurrent-ruby, sprockets, nokogiri, loofah, rails-html-sanitizer, rails-deprecated_sanitizer, rails-dom-testing, rack-test, actionview, actionpack, sprockets-rails, thor, railties, bundler, arel, activemodel, activerecord, globalid, activejob, mime-types, mail, actionmailer, rails after 397 seconds
24 gems installed

が、パスが通っていなかったので .zshrc に home/bin を追加。

$ rails -v
zsh: command not found: rails

$ /home/bin/rails -v
Rails 4.2.2

$ vi ~/.zshrc
$ tail -1 ~/.zshrc
export PATH=$PATH:/home/bin

$ source ~/.zshrc
$ rails -v
Rails 4.2.2

sqlite3

雑に作った rails アプリケーションに bundle install したら今度は sqlite3 で失敗。

$ bundle install --without production
(snip)
Installing sqlite3 1.3.9 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /home/.gem/ruby/gems/sqlite3-1.3.9/ext/sqlite3
/usr/bin/ruby.exe -r ./siteconf20160121-2500-vm72n8.rb extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'port install sqlite3 +universal',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).
*** 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
        --without-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=/usr/bin/$(RUBY_BASE_NAME)
        --with-sqlite3-dir
        --without-sqlite3-dir
        --with-sqlite3-include
        --without-sqlite3-include=${sqlite3-dir}/include
        --with-sqlite3-lib
        --without-sqlite3-lib=${sqlite3-dir}/lib
        --enable-local
        --disable-local

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

  /home/.gem/ruby/extensions/x86-cygwin/sqlite3-1.3.9/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/.gem/ruby/gems/sqlite3-1.3.9 for inspection.
Results logged to /home/.gem/ruby/extensions/x86-cygwin/sqlite3-1.3.9/gem_make.out

sqlite3 と libsqlite3-devel を入れたら動いた。

$ apt-cyg install sqlite3
(snip)
Package sqlite3-vfslog requires the following packages, installing:
cygwin libsqlite3_0
Package cygwin is already installed, skipping
Package libsqlite3_0 is already installed, skipping
Package sqlite3-vfslog installed
Package sqlite3 installed

$ apt-cyg install libsqlite3-devel
(snip)
Package libsqlite3-devel requires the following packages, installing:
libsqlite3_0 cygwin
Package libsqlite3_0 is already installed, skipping
Package cygwin is already installed, skipping
Package libsqlite3-devel installed

$ bundle install --without production
Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
(snip)
Bundle complete! 17 Gemfile dependencies, 75 gems now installed.
Gems in the group production were not installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

以上。