Skip to content

Instantly share code, notes, and snippets.

@ryosan-470
Last active December 23, 2015 05:53
Show Gist options
  • Save ryosan-470/7de9a67965fc0db6c61e to your computer and use it in GitHub Desktop.
Save ryosan-470/7de9a67965fc0db6c61e to your computer and use it in GitHub Desktop.
setup rails app

Setup Rails

環境

  • Ubuntu 14.04
  • Ruby 2.2.4 (最新版)

最初

以下のコマンドをうち,Rubyをインストールするために必要な物をまとめてインストールする.

$ sudo apt-get update
$ sudo apt-get install git-core curl nodejs zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

Rubyのインストール

Rubyの最新版を入れる.書いた当時は2.2.4だった.

cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash

rbenv install 2.2.2
rbenv global 2.2.2
ruby -v

ちなみにUbuntu 14.04ではデフォルトのRubyが1.9系で古い(ゴミ)

次にbundlerを使えるようにする.

echo "gem: --no-ri --no-rdoc" > ~/.gemrc
gem install bundler

Railsのアプリを作成する

Railsの新しいアプリを作成してみる.ここでVagrantを利用しているユーザーは/vagrant/以下に新しいディレクトリを作成しそこにRailsのアプリを作成する. こうすることでホストマシン側からも簡単にアクセスでき非常に使いやすい.

例えば適当にtutorialというディレクトリを作成しそこにアプリを生成する.

$ rails new tutorial -d mysql  # MySQLを後々利用するため

次に起動させる.起動させるには

$ bundle exec rails s -b 0.0.0.0 -p 3000

で起動する.これでブラウザの3000番ポートにアクセスすれば起動するはずである.

MySQLのインストール

今回はデータベースとしてMySQLを選択する.とりあえずMySQLをインストールしrootユーザーのパスワードを設定する.

$ sudo apt-get install mysql-server libmysqlclient-dev

次にRailsで使うためのユーザーを作成しそのユーザーに対し権限を付与する.

$ mysql -uroot -ppassword  # mysqlにユーザーroot, パスワードpasswordでログイン
mysql> create user tutorial identified by 'password';   -- ユーザーtutorial, パスワードpasswordを設定
...
mysql> GRANT ALL on *.* TO 'tutorial'@'localhost' identified by 'password'; -- 全権限をユーザーtutorialに付与
...
mysql> show grants for tutorial;
+------------------------------------------------------------------------------------------------------------------+
| Grants for tutorial@localhost                                                                                    |
+------------------------------------------------------------------------------------------------------------------+
| GRANT CREATE ON *.* TO 'tutorial'@'localhost' IDENTIFIED BY PASSWORD '*A74AAAE111D6801C32E1042F47AC1DF0EE557559' |
+------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

以上で設定できた.あとはDB自体の設定config/database.ymlのユーザー名とパスワードを先ほど設定したものに変え,

$ bundle exec rake db:create
$ bundle exec rake db:migrate

でDBに接続できるはず

@ryosan-470
Copy link
Author

NodejsをインストールしないとJavaScriptエンジンがないとかの理由で死ぬ.そんな場合は以下を参照:
http://qiita.com/pugiemonn/items/11a2bc8403e5947a8f13

@ryosan-470
Copy link
Author

普通にrails sしても起動しない.
なぜかVagrantで3000番ポートが貫通してくれないのでSSHでポートフォワーディングを張るとかいう荒業

ssh -L 3000:localhost:3000 vagrant@localhost -p 2222

よくわからんがホストIPをlocalhostとか127.0.0.1にせず0.0.0.0にすれば動く.

bundle exec rails s -b 0.0.0.0 -p 3000

@ryosan-470
Copy link
Author

RubyをソースコードからではなくPPAを利用してインストールする:

$ sudo apt-add-repository ppa:brightbox/ruby-ng
$ sudo apt-get update
$ sudo apt-get install ruby2.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment