Ubuntu 16.04 ServerにRuby On Railsをインストール

Ubuntu 16.04にrbenvを使ってRuby On Railsを導入

環境

OS: ubuntu 16.04 64bit

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"

ruby: 2.3.1

rails: 2.4.6

手順

パッケージインストール

必要なパッケージをインストール

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

Rubyのインストール

Rubyのバージョン管理ソフト「rbenv」をインストール

$ cd
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv

初期化コマンドを記述しておく

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ exec $SHELL

ruby-buildのインストール

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

rbenvでインストール可能なリストを表示

$ rbenv install -l

.
.
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
2.2.0-dev
.
.

rbenvで特定バージョンのrubyをインストール

$ rbenv install 2.3.1

アンインストールしたい時は以下のコマンド

$ rbenv uninstall 2.3.1

システムで使用しているrubyのバージョンを確認

$ rbenv global
system

現在はシステム標準のrubyが割り当てられているので変更する

$ rbenv global 2.3.1
$ rbenv global
2.3.1
$ rbenv rehash

gemをインストールした後のrbenv rehashを自動化する
このままではgemをインストールまたはアンインストールするたびにrbenv rehashする必要がある
これを自動化してくれるrbenv-rehashというgemがあるのでインストール
https://github.com/ryansouza/rbenv-rehash

$ gem i rbenv-rehash

Ruby On Railsのインストール

依存関係の解決のためNode.jsをインストール

$ curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
$ sudo apt-get install -y nodejs

gemが自動で作成するri rdocドキュメントを無効化する

$ echo 'install: --no-rdoc --no-ri' >> ~/.gemrc
$ echo 'update:  --no-rdoc --no-ri' >> ~/.gemrc

railsのインストール(version 4.2.6)

$ gem install rails -v 4.2.6

ディレクトリごとにrubyのバージョンを指定する場合は以下

railsのルートディレクト

$ vi .ruby-version

2.3.1

PostgreSQLのインストール

公式リポジトリを追加し、最新のバージョンをインストール

$ sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
$ wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install postgresql-common
$ sudo apt-get install postgresql-9.5 libpq-dev

DBにユーザーを作成

$ sudo -u postgres createuser hoge -s

# ユーザーにパスワードを設定する場合は以下
$ sudo -u postgres psql
$ postgres=# \password hoge

Railsプロジェクトの作成

SQLiteを使う場合 (性能的に非推奨)

$ rails new myapp

PostgreSQLを使う場合

# Note that this will expect a postgres user with the same username
# as your app, you may need to edit config/database.yml to match the
# user you created earlier

$ rails new myapp -d postgresql

# Move into the application directory
$ cd myapp

# If you setup MySQL or Postgres with a username/password, modify the
# config/database.yml file to contain the username/password that you specified

# Create the database
$ rake db:create

# rails s -b 0.0.0.0

アクセスを確認

以下のURLにアクセスできれば構築完了

http://<railsサーバーのIPアドレス>:3000

アンインストール

rubyRailsを消したい場合、 gem, apt-getで入れたものを削除することに加え以下を実行

$ gem uninstall railties '4.2.6'
$ rm -rf /usr/local/lib/ruby
$ rm -rf /usr/lib/ruby
$ rm -f /usr/local/bin/ruby
$ rm -f /usr/bin/ruby
$ rm -f /usr/local/bin/irb
$ rm -f /usr/bin/irb
$ rm -f /usr/local/bin/gem
$ rm -f /usr/bin/gem

豆知識

rbenvで特定のプロジェクトで使用するrubyのバージョンを指定する
$ mkdir rbenv-test
$ cd rbenv-test
$ vi .ruby-version
2.3.1を追記

これで、rbenv-testディレクトリの中ではバージョン2.3.1が使用されるようになる

rbenvのアップグレード
$ cd ~/.rbenv
$ git pull

参考

gorails.com