2015年6月13日土曜日

素のUbuntu14.04でRailsの新規アプリケーションを作成するまで

(1)事前準備(必要なツールたちをインストール)


各種パッケージのインストール(コンパイルとかするので必要みたい)
$ sudo apt-get install build-essential libssl-dev libreadline-dev

Git
$ sudo apt-get install git
$ git --version
git version 1.9.1

rbenv
$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv

rbenvのパスなど
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc

このあと、ターミナルを再起動

ruby-build
$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

nodejs (NVMでインストール)
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.25.4/install.sh | bash
ターミナルを閉じて開き直す

インストール可能なNode.js を確認する
$ nvm ls-remote
v0.1.14
v0.1.15
v0.1.16
...
v0.12.4
v0.12.5
v0.12.6
v0.12.7
iojs-v1.0.0
iojs-v1.0.1
iojs-v1.0.2
iojs-v1.0.3
...
ウジャーといっぱ出てくる

現時点(2015/07/12)で最新の v0.12.7 をインストール
$ nvm install 0.12.7

$ node -v
v0.12.7

(2)Rubyのインストール


インストールバージョンの一覧の表示
$ rbenv install -l
Available versions:
  1.8.6-p383
  1.8.6-p420
  1.8.7-p249
  1.8.7-p302
  ...

現時点で最新の安定版の2.2.2をインストール
$ rbenv install 2.2.2

インストールしたrubyの確認
$ rbenv versions
* 2.2.2 (set by /home/taoka/.rbenv/version)

rubyの切替え
$ rbenv global 2.2.2
$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]

(3)Railsのインストール


riとrdocドキュメントを無効化
$ echo 'install: --no-rdoc --no-ri' >> ~/.gemrc
$ echo 'update:  --no-rdoc --no-ri' >> ~/.gemrc

Railsのインストール
$ gem install rails
$ rails -v
Rails 4.2.1

(4)Railsプロジェクト用のフォルダを作成


Railsプロジェクトを作成するための適当なフォルダを作成する
$ mkdir RailsProjects
$ cd RailsProjects

(5)新規アプリケーションの作成

$ rails new アプリケーション名 [オプション]
オプションなどの詳しい説明は http://railsdoc.com/rails を参照

例:HelloWorld というアプリケーションを bundle install せずに、Test::Unit を組み込まずに作成する
$ rails new HelloWorld --skip-bundle --skip-test-unit
パスを指定して bundle install
$ bundle install --path vendor/bundle

ここ(bundle install)で、以下のエラーが発生
...
Installing spring 1.3.6
with native extensions 
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

/home/taoka/.rbenv/versions/2.2.2/bin/ruby -r ./siteconf20150613-32308-1s83jhf.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=/home/taoka/.rbenv/versions/2.2.2/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

extconf failed, exit code 1

Gem files will remain installed in /home/taoka/Documents/GitLab/vendor/bundle/ruby/2.2.0/gems/sqlite3-1.3.10 for inspection.
Results logged to /home/taoka/Documents/GitLab/vendor/bundle/ruby/2.2.0/extensions/x86_64-linux/2.2.0-static/sqlite3-1.3.10/gem_make.out
An error occurred while installing sqlite3 (1.3.10), and Bundler cannot
continue.
Make sure that `gem install sqlite3 -v '1.3.10'` succeeds before bundling.
お言葉どおり、以下を実行
$ sudo apt-get install libsqlite3-dev

再度、「bundle install --path vendor/bundle」を実行
今度はうまくいった。

(6)実行してみる

rails s

ここ(rails s)で、以下のエラーが発生
/HelloWorld/vendor/bundle/ruby/2.2.0/gems/execjs-2.5.2/lib/execjs/runtimes.rb:48:in
 `autodetect': Could not find a JavaScript runtime.
See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

Gemfileに以下を追加
gem 'therubyracer', platforms: :ruby

再度 bundle install
$ bundle install

※2回目の「bundle install」では path の指定は不要
$ bundle config
Set for your local app (/home/taoka/RailsProjects/HelloWorld/.bundle/config): "vendor/bundle"
(↑1回目に設定したパスになっている)

再び実行してみる
rails s

今度はうまく行きました
http://localhost:3000にアクセス

以上

0 件のコメント:

コメントを投稿