vim、プログラマならみんな手足のように使えますよね。

ま、自分はまったく使えませんが。

vim、マスターすれば魔法のようにコードが湧いてくるとか、VBな人もマシン語が2進で読めるようになるとか噂なので、

前々から使えるようになりたいと思ってたんですわ。

とはいえ、どっぷりwindowsな人生なので使う機会もあまりなく、sakuraとかvisual studioとかで事足りてしまうわけです。

という事で、ターミナル経由のlinuxでrailsのお勉強をして、vimをマスターしてしまおうという企画の作業メモです。

Railsのお勉強は「開発現場でちゃんと使えるRails 4入門」を参考に進めていきます。

OS

vmware にUbuntu Server 14.04.1 LTSを(簡易モード?で)インストールします。

sshとvimは apt-getでインストール。

sshにはputtyを使ってアクセスします。

git

1
2
3
cozy@ubuntu:~$ sudo apt-get install git
cozy@ubuntu:~$ git config --global user.email "cozy***@***.com"
cozy@ubuntu:~$ git config --global user.name "cozy"

やらなくても良いgit関係

1
2
3
4
cozy@ubuntu:~$ git add .bash*
cozy@ubuntu:~$ git add .profile
cozy@ubuntu:~$ git add .zlogin
cozy@ubuntu:~$ git reset HEAD --  .bash_history

curlとrvm(rubyインストールツール?)

1
2
3
4
5
cozy@ubuntu:~$ sudo apt-get install curl
cozy@ubuntu:~$ \curl -sSL https://get.rvm.io | bash -s stable
cozy@ubuntu:~$ source ~/.bash_profile
cozy@ubuntu:~$ rvm --version
rvm 1.25.28 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]

Ruby

1
2
cozy@ubuntu:~$ rvm install ruby-2.1.1 --default
cozy@ubuntu:~$ source ~/.bash_profile

SSL関係のこんな感じの

curl: (60) SSL certificate problem: unable to get local issuer certificate

エラーがでたら

1
2
3
cozy@ubuntu:~$ curl http://curl.haxx.se/ca/cacert.pem
cozy@ubuntu:~$ vim .bash_profile
export CURL_CA_BUNDLE="/home/<username>/cacert.pem"

必要なら

~/cacert.pemを編集。

pry(対話型実行環境。REPL環境とかいうらしい)

1
cozy@ubuntu:~$ gem install pry

同じくSSL関係のエラー。

Unable to download data from https://rubygems.org/ – SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://api.rubygems.org/latest_specs.4.8.gz)

このコマンドで必要な証明書を認識(できるらしい)
cozy@ubuntu:~$ openssl s_client -showcerts -connect rubygems.org:https
証明書をコピー
cozy@ubuntu:~$ mkdir ~/ca-certificates
cozy@ubuntu:~$ cd ~/ca-certificates
cozy@ubuntu:~/ca-certificates$ mkdir extra
なんとか(.SCPとか)して ~/ca-certificates/extra にcertificatesファイル(拡張子は.crtにする)をコピー
cozy@ubuntu:~/ca-certificates$ sudo ln -s ~/ca-certificates/extra /usr/share/ca-certificates/extra
cozy@ubuntu:~/ca-certificates$ sudo cp -r ~/ca-certificates/extra/ /usr/share/ca-certificates/extra
cozy@ubuntu:~/ca-certificates$ sudo dpkg-reconfigure ca-certificates

なんかいろいろ試したけどgemとopensslはCA証明書の参照先がちがうらしい。

gem用の証明書はこのコマンドで参照先確認して、証明書ファイルをコピー。既にある場合は追記するべし。

1
2
3
cozy@ubuntu:~$  ruby -ropenssl -e 'p OpenSSL::X509::DEFAULT_CERT_FILE'
"/usr/lib/ssl/cert.pem"
cozy@ubuntu:~$ sudo cp ~/cacert.pem /usr/lib/ssl/cert.pem

Railsインストール

1
2
3
cozy@ubuntu:~$ gem install rails
cozy@ubuntu:~$ rails --version
Rails 4.1.4

railsアプリ作成

1
2
3
4
cozy@ubuntu:~$ mkdir ruby
cozy@ubuntu:~$ cd ruby/
cozy@ubuntu:~/ruby$ rails new book_library
cozy@ubuntu:~/ruby$ cd book_library/

標準外のgemをインストールする

1
cozy@ubuntu:~/ruby/book_library$ bundle install --path vendor/bundle

エラー

1
`autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

対処

1
2
3
4
5
6
7
cozy@ubuntu:~/ruby/book_library$  gem install execjs
cozy@ubuntu:~/ruby/book_library$ gem install therubyracer
cozy@ubuntu:~/ruby/book_library$ vim Gemfile
 # See https://github.com/sstephenson/execjs#readme for more supported runtimes
 # gem 'therubyracer',  platforms: :ruby
+gem 'therubyracer'
cozy@ubuntu:~/ruby/book_library$ bundle install

かなり色々お仲間になるみたいです。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
cozy@ubuntu:~/ruby/book_library$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/.........
Fetching additional metadata from https://rubygems.org/..
Installing rake 10.3.2
Installing i18n 0.6.11
Using json 1.8.1
Installing minitest 5.4.0
Installing thread_safe 0.3.4
Installing tzinfo 1.2.1
Installing activesupport 4.1.4
Installing builder 3.2.2
Installing erubis 2.7.0
Installing actionview 4.1.4
Installing rack 1.5.2
Installing rack-test 0.6.2
Installing actionpack 4.1.4
Installing mime-types 1.25.1
Installing polyglot 0.3.5
Installing treetop 1.4.15
Installing mail 2.5.4
Installing actionmailer 4.1.4
Installing activemodel 4.1.4
Installing arel 5.0.1.20140414130214
Installing activerecord 4.1.4
Installing coffee-script-source 1.7.1
Installing execjs 2.2.1
Installing coffee-script 2.3.0
Installing thor 0.19.1
Installing railties 4.1.4
Installing coffee-rails 4.0.1
Installing hike 1.2.3
Installing multi_json 1.10.1
Installing jbuilder 2.1.3
Installing jquery-rails 3.1.1
Using bundler 1.6.5
Installing tilt 1.4.1
Installing sprockets 2.11.0
Installing sprockets-rails 2.1.3
Installing rails 4.1.4
Installing rdoc 4.1.1
Installing sass 3.2.19
Installing sass-rails 4.0.3
Installing sdoc 0.4.0
Installing spring 1.1.3
Installing sqlite3 1.3.9
Installing turbolinks 2.2.2
Installing uglifier 2.5.3
Your bundle is complete!
It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:

<= 1.8.6 : unsupported
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
cozy@ubuntu:~/ruby/book_library$

アプリ実行テスト

1
2
3
cozy@ubuntu:~/ruby/book_library$ bundle exec rails generate scaffold book title:string author:string outline:text
cozy@ubuntu:~/ruby/book_library$ bundle exec rake db:migrate
cozy@ubuntu:~/ruby/book_library$ bundle exec rails server

ブラウザでhttp://server:3000/booksにアクセスしてなんか出ればOK。

ここまでvimで作業はほとんどなし。

つづく(のか?)