CentOS7でRailsを動かしてみる

CentOS7でRubyフレームワークRailsを動かしてみる。

とりあえず、最新のRubyを入れるために、rbenvをインストールする。

$ sudo yum install -y git gcc gcc-c++ openssl-devel readline-devel
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

今回は、2系の最新を入れる。

$ rbenv install -l
・・・
2.7.2
・・・

$ rbenv install 2.7.2
$ rbenv global 2.7.2

$ ruby --version
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]

Railsをインストール。

$ gem install rails

$ rails --version
Rails 6.1.0

プロジェクトを作成しようとしたら、sqlite-develが無いと怒られたので、

$ rails new hoge
・・・
sqlite3.h is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
・・・

sqlite-develをインストール。

$ sudo yum install -y sqlite-devel

改めてプロジェクトを作成しようとしたら、今度はnodejsが無いと怒られる。

$ rm -rf hoge
$ rails new hoge
・・・
sh: node: command not found
sh: nodejs: command not found
Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/
・・・

nodejsは普通には入れられないようなので、epelを入れてインストール。

$ sudo yum install -y epel-release
$ sudo yum install -y nodejs

$ node --version
v6.17.1

プロジェクトを作成しようとしたら、今度はnodejsのバージョンが古いと怒られる。

$ rm -rf hoge
$ rails new hoge
・・・
Webpacker requires Node.js ">=10.17.0" and you are using v6.17.1
・・・

改めて最新のnodejsをインストールしようとしたら、エラーがでるので、

$ curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
$ sudo yum install -y nodejs
・・・
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

一旦、削除して、改めてインストール。

$ sudo yum remove -y nodejs
$ sudo yum install -y nodejs

これでnodejsが入った。

$ node --version
v14.15.3

改めてプロジェクトを作成しようとしたら、次はYarnが無い、と怒られる。

$ rm -rf hoge
$ rails new hoge
・・・
Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/
・・・

Yarnをインストール。

$ sudo yum install -y wget
$ sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
$ sudo yum install -y yarn

$ yarn --version
1.22.5

これで、ようやくプロジェクトが作成できた。

$ rm -rf hoge
$ rails new hoge

プロジェクトに移動して、起動。

$ cd hoge
$ rails server -b 0.0.0.0

ブラウザからアクセスすると、SQLiteが古い、と怒られる。

http://サーバーのIPアドレス:3000/
f:id:yk5656:20210103150953j:plain

sqlite3の最新をソースコードからインストールする。

$ curl -O https://www.sqlite.org/2020/sqlite-autoconf-3330000.tar.gz
$ tar xzvf sqlite-autoconf-3330000.tar.gz
$ cd sqlite-autoconf-3330000
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
$ cd ..
$ rm sqlite-autoconf-3330000.tar.gz
$ rm -rf ./sqlite-autoconf-3330000
$ echo 'export LD_LIBRARY_PATH="/usr/local/lib"' >> ~/.bash_profile
$ source ~/.bash_profile

sqlite3が最新になった。

$ sqlite3 --version
3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668

改めて、railsを起動。

$ rails server -b 0.0.0.0

これで、Railsの画面が表示できた。

http://サーバーのIPアドレス:3000/
f:id:yk5656:20210103151003j:plain

Railsのインストール、面倒くさい。