LoginSignup
4
10

More than 5 years have passed since last update.

Rails 環境をCentos7に構築

Last updated at Posted at 2018-08-25

仮想環境にrailsを何度も構築することが多いので、メモとして作成しました。

他の方が記述してくれた手順を見ながら構築でも良いのですが、
記事の更新日時が古かったりすると内容の整合性をいちいち確認するのが面倒なので、なるべく公式の内容を
確認しながら作る記事を作成しました。

実施環境

・centos 7.2
・Ruby 2.5.1
・Rails 5.1.6
・PostgreSQL 9.6

Gitインストール

$ sudo yum update
$ sudo yum install git

Ruby インストール

Rubyのビルドに必要なパッケージをインストール

(これやらずにRubyインストールしたらエラーが起こりました)

$ sudo yum install -y openssl-devel readline-devel zlib-devel

rbenv・ruby-build インストール

・rbenvをインストール

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

・~/.bash_profileにexport PATH="$HOME/.rbenv/bin:$PATH"eval "$(rbenv init -)"を追加

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

・追加内容を反映

$ source ~/.bash_profile

・ruby-buildをインストール

$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

・rbenv-doctorで問題なくセットアップできているか確認

$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
Checking for `rbenv' in PATH: /home/vagrant/.rbenv/bin/rbenv
Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: /home/vagrant/.rbenv/plugins/ruby-build/bin/rbenv-install (ruby-build 20180822)
Counting installed Ruby versions: none
  There aren't any Ruby versions installed under `/home/vagrant/.rbenv/versions'.
  You can install Ruby versions like so: rbenv install 2.2.4
Checking RubyGems settings: OK
Auditing installed plugins: OK

インストールできるRubyのバージョンを確認

$ rbenv install -l

Rubyインストール

$ rbenv install 2.5.1

使用するrubyのバージョンを指定

$ rbenv global 2.5.1

インストールできたことを確認

$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

Rails インストール

・bundler インストール

$ gem install bundler

・Rails インストール(今回はv5.1.6を指定)

$ gem install rails -v 5.1.6 --no-rdoc --no-ri
$ rails -v
Rails 5.1.6

PostgreSQL インストール

リポジトリRPM追加

$ sudo yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

クライアント と サーバー をインストール

$ sudo yum install postgresql96 postgresql96-server postgresql-devel
$ psql --version
psql (PostgreSQL) 9.6.10

DBを初期化

$ sudo /usr/pgsql-9.6/bin/postgresql96-setup initdb
Initializing database ... OK

PostgreSQLの起動を自動化

$ sudo systemctl enable postgresql-9.6
$ sudo systemctl start postgresql-9.6

・PostgreSQLが動いていることを確認

$ systemctl list-unit-files -t service | grep postgresql
postgresql-9.6.service                      enabled 

$ systemctl status postgresql-9.6
● postgresql-9.6.service - PostgreSQL 9.6 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-9.6.service; enabled; vendor preset: disabled)
   Active: active (running) since 土 2018-08-25 19:54:15 BST; 21s ago
     Docs: https://www.postgresql.org/docs/9.6/static/
  Process: 23163 ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 23169 (postmaster)
   CGroup: /system.slice/postgresql-9.6.service
           ├─23169 /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data/
           ├─23171 postgres: logger process   
           ├─23173 postgres: checkpointer process   
           ├─23174 postgres: writer process   
           ├─23175 postgres: wal writer process   
           ├─23176 postgres: autovacuum launcher process   
           └─23177 postgres: stats collector process 

Rails new

railsのデフォルトのDBはSQLite3なので、PostgreSQLを使用するよう指定する

$ rails new アプリ名 -d postgresql

参考サイト

・Ruby インストール手順:
https://www.ruby-lang.org/ja/documentation/installation/
・Git インストール手順:
https://git-scm.com/book/ja/v1/%E4%BD%BF%E3%81%84%E5%A7%8B%E3%82%81%E3%82%8B-Git%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB
・rbenv インストール手順
https://github.com/rbenv/rbenv
・ruby-build インストール手順
https://github.com/rbenv/ruby-build#readme
・Ruby on Rails インストール手順
https://github.com/rails/rails
・PostgreSQL インストール手順
https://www.postgresql.org/download/linux/redhat/

4
10
0

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
10