railsの勉強のためオンラインの無料チュートリアルをやってみています。ありがたやありがたや。
https://railstutorial.jp/
とはいえそのままやっているつもりがいろいろつまずきがあるのでメモを残していきます。
あとこのチュートリアルではcloud9というawsを使っているのでそれもみておくと良いでしょう。
●rubyのインストール
rubyのインストールはチュートリアルにないのでここは自分で頑張る。
以下のサイトを参考にインストールした。
https://qiita.com/Fendo181/items/d14ebfb148223c8e5ecb
・rbenvをインストール
rbenvはrubyのバージョンを簡単に変えられるコマンドラインツール。
>yum remove -y ruby >yum install git >git clone git://github.com/sstephenson/rbenv.git .rbenv >echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile >echo 'eval "$(rbenv init -)"' >> ~/.bash_profile >source ~/.bash_profile
インストール確認
>rbenv -v
・ruby-buildをインストール
ruby-buildはrubyをインストールするためのrbenvのプラグイン。
>git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build >cd ~/.rbenv/plugins/ruby-build >sudo ./install.sh
バージョン確認
>rbenv install -l
rubyのインストール
>rbenv install 2.5.0 Downloading ruby-2.5.0.tar.bz2... -> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.bz2 error: failed to download ruby-2.5.0.tar.bz2 BUILD FAILED (CentOS Linux 7 using ruby-build 20180224)
しかし失敗・・・。調べるとOpenSSLを入れると良いとのこと。
OpenSSLライブラリのインストールコマンド
>sudo yum -y install openssl-devel >rbenv install 2.5.0 Downloading ruby-2.5.0.tar.bz2... -> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.bz2 error: failed to download ruby-2.5.0.tar.bz2 BUILD FAILED (CentOS Linux 7 using ruby-build 20180224)
だめだ。ログを調べてみると
> cat /tmp/ruby-build.20180302070310.4613.log curl: (35) Peer reports incompatible or unsupported protocol version.
なんかオプションつけてインストールするといいらしい
>RUBY_BUILD_CURL_OPTS=--tlsv1.2 rbenv install 2.5.0 Downloading ruby-2.5.0.tar.bz2... -> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.bz2 Installing ruby-2.5.0... BUILD FAILED (CentOS Linux 7 using ruby-build 20180224) Inspect or clean up the working tree at /tmp/ruby-build.20180302070757.4725 Results logged to /tmp/ruby-build.20180302070757.4725.log Last 10 log lines: installing rdoc: /root/.rbenv/versions/2.5.0/share/ri/2.5.0/system installing capi-docs: /root/.rbenv/versions/2.5.0/share/doc/ruby The Ruby readline extension was not compiled. ERROR: Ruby install aborted due to missing extensions Try running `yum install -y readline-devel` to fetch missing dependencies. Configure options used: --prefix=/root/.rbenv/versions/2.5.0 LDFLAGS=-L/root/.rbenv/versions/2.5.0/lib CPPFLAGS=-I/root/.rbenv/versions/2.5.0/include
しかしまた失敗。なんか「yum install -y readline-devel」をやってみろ的なことが書いてあるからやってみる。
>yum install -y readline-devel いっぱい落ちてくる・・・
そしてもう一度、
>RUBY_BUILD_CURL_OPTS=--tlsv1.2 rbenv install 2.5.0 Downloading ruby-2.5.0.tar.bz2... -> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.bz2 Installing ruby-2.5.0... Installed ruby-2.5.0 to /root/.rbenv/versions/2.5.0
はいったっぽいのでrubyのバージョンを設定する
>rbenv global 2.5.0
確認
>ruby -v ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
やっと入った。かなりの達成感だが本当の目的はこれではない。
railsを入れなければ。この先はチュートリアルを読み進めながらやっていく…
>gem install rails -v 5.1.4 かなりいろいろなファイルが落ちてくる・・・
どこにディレクトリ作ればいいかわからないからとりあえず
>cd /home >mkdir environment >cd environment
そして新しいプロジェクトを作る
>rails _5.1.4_ new hello_app ・・・ An error occurred while installing sqlite3 (1.3.13), and Bundler cannot continue. Make sure that `gem install sqlite3 -v '1.3.13'` succeeds before bundling. In Gemfile: sqlite3 run bundle exec spring binstub --all Could not find gem 'sqlite3' in any of the gem sources listed in your Gemfile. Run `bundle install` to install missing gems.
しかしまたも失敗。すんなり行くことはないのか。
とりあえず指示に従って
>gem install sqlite3 -v '1.3.13' Building native extensions. This could take a while... ERROR: Error installing sqlite3: ERROR: Failed to build gem native extension. current directory: /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/sqlite3-1.3.13/ext/sqlite3 /root/.rbenv/versions/2.5.0/bin/ruby -r ./siteconf20180302-15327-1mmntf4.rb extconf.rb checking for sqlite3.h... no sqlite3.h is missing. Try 'brew install sqlite3', '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=/root/.rbenv/versions/2.5.0/bin/$(RUBY_BASE_NAME) --with-sqlite3-config --without-sqlite3-config --with-pkg-config --without-pkg-config --with-sqlite3-dir --without-sqlite3-dir --with-sqlite3-include --without-sqlite3-include=${sqlite3-dir}/include --with-sqlite3-lib --without-sqlite3-lib=${sqlite3-dir}/lib To see why this extension failed to compile, please check the mkmf.log which can be found here: /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/sqlite3-1.3.13/mkmf.log extconf failed, exit code 1 Gem files will remain installed in /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/sqlite3-1.3.13 for inspection. Results logged to /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/sqlite3-1.3.13/gem_make.out
もちろん失敗。いくつかやってみて欲しいみたいなので簡単にやれそうな
>yum install sqlite-devel
一応入ったのでもう一度
>gem install sqlite3 -v '1.3.13' Building native extensions. This could take a while... Successfully installed sqlite3-1.3.13 Parsing documentation for sqlite3-1.3.13 Installing ri documentation for sqlite3-1.3.13 Done installing documentation for sqlite3 after 0 seconds 1 gem installed
おお!今度は普通に入った。
このあとどうすればいいのかわからないがとりあえずBundleを更新してみる。
>bundle update Fetching turbolinks-source 5.1.0 Installing turbolinks-source 5.1.0 Fetching turbolinks 5.1.0 Installing turbolinks 5.1.0 Fetching uglifier 4.1.6 Installing uglifier 4.1.6 Fetching web-console 3.5.1 Installing web-console 3.5.1 Bundle updated!
なんかはいったっぽい。正常に終わったから大丈夫なのかな。
チュートリアルでは指定のGemfileを使って欲しいみたいだからコピペして
>bundle install Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine. The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`. Fetching gem metadata from https://rubygems.org/.......... Fetching gem metadata from https://rubygems.org/. Resolving dependencies... Bundler could not find compatible versions for gem "activesupport": In snapshot (Gemfile.lock): activesupport (= 5.1.5) In Gemfile: rails (= 5.1.4) was resolved to 5.1.4, which depends on activesupport (= 5.1.4) coffee-rails (= 4.2.2) was resolved to 4.2.2, which depends on railties (>= 4.0.0) was resolved to 5.1.5, which depends on activesupport (= 5.1.5) Running `bundle update` will rebuild your snapshot from scratch, using only the gems in your Gemfile, which may resolve the conflict.
はい、また失敗。なんかアップデートをやれってことみたいなので
>bundle update ・・・
今度は成功。
ブラウザで確認できるということでサーバーを立ててやる
> rails server Traceback (most recent call last): 19: from bin/rails:4:in `<main>' 18: from bin/rails:4:in `require' 17: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>' 16: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke' 15: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform' 14: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch' 13: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command' 12: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run' 11: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform' 10: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap' 9: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `block in perform' 8: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `require' 7: from /home/environment/hello_app/config/application.rb:7:in `<top (required)>' 6: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler.rb:114:in `require' 5: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:65:in `require' 4: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:65:in `each' 3: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:76:in `block in require' 2: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:76:in `each' 1: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:80:in `block (2 levels) in require' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:84:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError) Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. Backtrace for gem load error is: /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/uglifier-3.2.0/lib/uglifier.rb:5:in `require' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/uglifier-3.2.0/lib/uglifier.rb:5:in `<top (required)>' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:81:in `require' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:81:in `block (2 levels) in require' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:76:in `each' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:76:in `block in require' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:65:in `each' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:65:in `require' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler.rb:114:in `require' /home/environment/hello_app/config/application.rb:7:in `<top (required)>' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `require' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `block in perform' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke' /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>' bin/rails:4:in `require' bin/rails:4:in `<main>' Bundler Error Backtrace:
はいまた失敗。出力を見ると「Gem Load Error is: Could not find a JavaScript runtime.」とかなんとか。
ググるとすぐ出てくる。これはこれでありがたい。
どうやらGemfileでコメントしてあった「gem ‘therubyracer’, platforms: :ruby」をコメントアウトしてやると良いらしい。v8エンジンがないのね。
チュートリアルひどいよ。これじゃあ絶対失敗するんじゃん。
気をとり直してGemfileを変更してまた
> bundle update
無事インストール
今度こそ!
>rails server => Booting Puma => Rails 5.1.4 application starting in development => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.9.1 (ruby 2.5.0-p0), codename: Private Caller * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://0.0.0.0:3000 Use Ctrl-C to stop
無事サーバーがたったっぽい。
http://localhost:3000/へアクセスすると確かに「Yay! You’re on Rails!」!!