Bash on Ubuntu on Windows で Rails 5.0 環境構築 (3)

それでは引き続き FileShelf Plus からSSHターミナル接続してインストール作業を続けます。
「Bash on Ubuntu on Windows」のウィンドウはそのままにしておく必要があります。このウィンドウを閉じてしまうと、SSHサーバーも停止してしまいます。

20160811_Bash-12

必須モジュールのインストール

まず FileShelf Plus のSSHターミナル画面でrootユーザーに切り替えておきます。

sudo su -

必須モジュールをインストールします。

apt-get install gcc
apt-get install build-essential
apt-get install zlib1g-dev
apt-get install libssl-dev
apt-get install libreadline-dev
apt-get install git
apt-get install sqlite3 libsqlite3-dev

Rubyのインストール

rbenvでRuby環境を作ります。

git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash
git clone https://github.com/rkh/rbenv-update.git ~/.rbenv/plugins/rbenv-update

.bashrcへ追加して、rbenvへのパスを通します。

vim ~/.bashrc
....................................
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
....................................
source ~/.bashrc

最新安定板 Ruby 2.3.1 をインストールします。インストール終了まで時間がかかります。

rbenv install 2.3.1
rbenv global 2.3.1
ruby -v

ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
2.3.1 がインストールされました。

Rails 5.0 のインストール

RubyGemsを最新化

gem update --system
gem -v

現在の最新2.6.6にアップデートされました。
Ruby on Rails 最新版をインストールします。

gem install rails
rails -v

Rails 5.0.0.1 がインストールされました。
おまじないもしておきます。

gem install bundler
gem install spring
gem install therubyracer

Rails サンプルアプリケーション

サンプルアプリケーションを作成してみましょう。

cd /home/hoge
rails new sample
cd sample
rails generate scaffold post title:string body:text

Bundler::GemRequireError: There was an error while trying to load the gem ‘uglifier’.
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
therubyracerを有効にする必要があります。

vim Gemfile
....................................
gem 'therubyracer', platforms: :ruby
....................................

#を外します。

bundle install

もう一度

rails generate scaffold post title:string body:text

Usage: spring COMMAND [ARGS]
Commands for spring itself:
………………………………………..
rails aborted!

だめみたいです。Springを外してしまいましょう。

vim Gemfile
......................................
group :development do
  ......................................
 #gem 'spring'
 #gem 'spring-watcher-listen', '~> 2.0.0'
end

#を付けます。

bundle install
rails generate scaffold post title:string body:text

/root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rb-inotify-0.9.7/lib/rb-inotify/watcher.rb:74:in `initialize’: Invalid argument – Failed to watch “/root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/locale”: the given event mask contains no legal events; or fd is not an inotify file descriptor. (Errno::EINVAL)

Bash on Windows は Filesystem watch に対応していないため、EventedFileUpdateCheckerを外す必要があります。

vim config/environments/development.rb
........................................
  #config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

#を付けます。

rails generate scaffold post title:string body:text

invoke active_record
create db/migrate/20160814102355_create_posts.rb
create app/models/post.rb
invoke test_unit
create test/models/post_test.rb
create test/fixtures/posts.yml
invoke resource_route
route resources :posts
invoke scaffold_controller
create app/controllers/posts_controller.rb
invoke erb
create app/views/posts
create app/views/posts/index.html.erb
create app/views/posts/edit.html.erb
create app/views/posts/show.html.erb
create app/views/posts/new.html.erb
create app/views/posts/_form.html.erb
invoke test_unit
create test/controllers/posts_controller_test.rb
invoke helper
create app/helpers/posts_helper.rb
invoke test_unit
invoke jbuilder
create app/views/posts/index.json.jbuilder
create app/views/posts/show.json.jbuilder
create app/views/posts/_post.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/posts.coffee
invoke scss
create app/assets/stylesheets/posts.scss
invoke scss
create app/assets/stylesheets/scaffolds.scss

成功です。マイグレーションを実行して、

rake db:migrate

ルーティング設定も追加しておきます。

vim config/routes.rb
..................................
  root :to => 'posts#index'
end

開発用Webサーバ Webrick を起動してみます。

rails server

=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode…
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

ブラウザから http://localhost:3000 にアクセスしてみます。

20160811_Bash-14

New Post をクリックしてデータ入力し

20160811_Bash-15

Create Post をクリックすると

20160811_Bash-16

登録成功

ひとまず環境が整いました。

シェアする

  • このエントリーをはてなブックマークに追加

フォローする