LoginSignup
4
5

More than 5 years have passed since last update.

Windows10 で Bash on Ubuntu on Windows の上で開発ツールとして Rails サーバー を立ててみた話

Last updated at Posted at 2017-05-22

方針

  1. Bash on Ubuntu on Windowsを使えるようにする
  2. RubyのInstall
  3. Ruby on Railsのインストール

参考記事

素晴らしい参考記事がたくさんあり、ほぼ上記の記事を参考にすればできるのですが私の環境だとこけたのでその備忘録として書きたいと思います。

注意

ちなみに、Bash on Ubuntu on Windowsでサーバーを立てることに関して

にて、

Second, while you’ll be able to run native Bash and many Linux command-line tools on Windows, it’s important to note that this is a developer toolset to help you write and build all your code for all your scenarios and platforms. This is not a server platform upon which you will host websites, run server infrastructure, etc. For running production workloads on Ubuntu, we have some great solutions using Azure, Hyper-V, and Docker, and we have great tooling for developing containerized apps within Windows using Docker Tools for Visual Studio, Visual Studio Code and yo docker.

と言及されてる通り、これはあくまでも開発ツールの一つなので、BoW(Bash on Ubuntu on Windows)でサーバー運用しようみたいなことはやめましょう。

1. Bash on Ubuntu on Windowsを使えるようにする

こちらの方の丁寧な記事のままやればできました。
http://qiita.com/chimame/items/3787a59eca1005b89eac

2. RubyのInstall

gitからcloneする方が最新版が入るらしいのでgitを使えるようにします

$ apt-get install git

gitが使えるようになったら

$ 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

を実行します。

この後、~/.bashrcに以下を記述しました

~/.bashrc
#略
[[ -d ~/.rbenv  ]] && \
  export PATH=${HOME}/.rbenv/bin:${PATH} && \
  eval "$(rbenv init -)"

その後この変更を反映するため

$ source ~/.bashrc

をします。

次に、Rubyインストールに際して必要らしいものをinstallしておきます

$ apt-get install gcc build-essential libssl-dev libreadline-dev zlib1g-dev

ここで、rubyをinstallします(私は2.4.1を入れました)

$ rbenv install 2.4.1
$ rbenv global 2.4.1
$ ruby -v 
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

みたいな感じになります、最初は1.9.1とかが入ってました。
これが後からうまくいかなくて、bundleで使われているrubyが違って困るみたいなことになりました

3. Ruby on Railsのインストール

まず、bundlerから入れていきます、Permissionがとか怒られたら適宜対応してください

$ gem install bundler

次に作業用ディレクトリを作りそこでbundle initし、Gemfileを編集します。
gem railsのコメントアウトを外すだけです。

$ mkdir workdir && cd workdir
$ bundle install
$ vim Gemifile
source "https://rubygems.org"

- #gem "rails"
+ gem "rails"

Nokogiriインストール設定(何がおきてるかいまいちわからないが定石みたいなもののよう)

$ gem update --system
$ apt-get install libxml2 libxml2-dev libxslt-dev
$ bundle config build.nokogiri "--use-system-libraries --with-xml2-include=/usr/include/libxml2"

http://qiita.com/chobi9999/items/7b66677b3bf3da8acc14

ここでいよいよgem install bundleをするわけですがrubyのバージョンがbundleで使われてるものと違うみたいなことで怒られないように

$ rbenv exec gem install bundler
$ rbenv rehash

みたいなことをしておく

http://qiita.com/white_aspara25/items/d5e19b82be17048d9215

$ bundle install --path vendor/bundler

SQLiteが必要みたいに言われたので以下を入れました

apt-get install libsqlite3-dev

適当なプロジェクト(Hogeとしました)を作る

$ bundle exec rails new Hoge
$ cd Hoge
$ bundle install --path vendor/bundler

ここで二つのファイルを編集する
Gemfileに関して

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end
#略
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
- #gem 'therubyracer', platforms: :ruby
+ gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
#略

次にconfig/environments/development.rb
コメントアウトしないとjavascriptのRuntimeなんちゃらがなんちゃらとか色々言われてrails sできません。

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  #略
  # config.action_view.raise_on_missing_translations = true

  # Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
- config.file_watcher = ActiveSupport::EventedFileUpdateChecker
+ # config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

すると、ようやくrailsサーバーを立ち上げられます

$ bin/rails s
=> Booting Puma
=> Rails 5.1.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.8.2 (ruby 2.4.1-p111), codename: Sassy Salamander
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Consoleのoutput通り、終了はCtrl-Cでできます。

4
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5