LoginSignup
9
6

More than 3 years have passed since last update.

CentOS7(Docker)でRuby on Railsを構築しようとしたらグダグダした話

Last updated at Posted at 2020-02-03

次のプロジェクトで扱うシステムがRuby on Railsで構築されている。
ruby及びrailsは全く触った事が無かったので、家で勉強しようとして、
centos7コンテナ上で実行するまでにぐだぐだした話を端折らずに書く。

イメージをpull

docker pull centos:7

コンテナ起動

docker run --name test -itd -p 3000:3000 centos:7 bash

コンテナに接続

docker exec -it test bash

ruby入れる

yum install ruby -y
ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

rails入れる

gem install rails

ERROR:  Error installing rails:
        i18n requires Ruby version >= 2.3.0.

公式に2.5以上が必要と書いてある。バージョン指定で入れたい。

一旦rubyを消す

yum remove ruby

rbenv入れる

rubyのインストールはrbenvを入れてやるのが定石らしい
イケてる記事を参考にやる

git入れる

yum install git -y

rbenvをとってきてパスを通す

git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
exec $SHELL -l
rbenv -v
rbenv 1.1.2-20-g143b2c9

ruby-build入れる

git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

rubyの最新安定板は2.7.0らしいのでこれを選択

rbenv install 2.7.0
tar (child): bzip2: Cannot exec: No such file or directory

bzip2がないといわれたので入れる。再びrbenv installする。

yum install -y bzip2
configure: error: no acceptable C compiler found in $PATH

C コンパイラがないといわれたのでgccを入れ、再びrbenv installする

yum install -y gcc
checking if make is GNU make... ./configure: line 29860: make: command not found

makeがないといわれたので入れた後でrbenv installする

yum install -y make
BUILD FAILED (CentOS Linux 7 using ruby-build 20200115-8-g73b926b)

Inspect or clean up the working tree at /tmp/ruby-build.20200203095258.8069.mCat66
Results logged to /tmp/ruby-build.20200203095258.8069.log
Last 10 log lines:
        from ./tool/rbinstall.rb:841:in `block (2 levels) in install_default_gem'
        from ./tool/rbinstall.rb:278:in `open_for_install'
        from ./tool/rbinstall.rb:840:in `block in install_default_gem'
        from ./tool/rbinstall.rb:830:in `each'
        from ./tool/rbinstall.rb:830:in `install_default_gem'
        from ./tool/rbinstall.rb:794:in `block in <main>'
        from ./tool/rbinstall.rb:945:in `block in <main>'
        from ./tool/rbinstall.rb:942:in `each'
        from ./tool/rbinstall.rb:942:in `<main>'
make: *** [do-install-all] Error 1

何かで失敗
Results logged to /tmp/ruby-build.20200203095258.8069.logとのことなのでログファイルを眺める

vi /tmp/ruby-build.20200203095258.8069.log

・・・

 Could not be configured. It will not be installed.
        /tmp/ruby-build.20200203095258.8069.mCat66/ruby-2.7.0/ext/openssl/extconf.rb:97: OpenSSL library could not be found. You might want to use --with-openssl-dir=<dir> option to specify the prefix where OpenSSL is installed.
        Check ext/openssl/mkmf.log for more details.

・・・

OpenSSLライブラリが見つからないと言われているので入れる
参考

yum install -y openssl-devel

Installed ruby-2.7.0 to /root/.rbenv/versions/2.7.0

rbenvで使用するrubyバージョンを設定する

rbenv global 2.7.0

railsを入れる

gem install rails

・・・

rails after 30 seconds
40 gems installed

アプリ作る

アプリは/opt/dockerの下に作ることにした

mkdir /opt/docker
cd /opt/docker

rails new example
Could not find gem 'sqlite3 (~> 1.4)' in any of the gem sources listed in your Gemfile.
         run  bundle exec spring binstub --all
bundler: command not found: spring
Install missing gem executables with `bundle install`
       rails  webpacker:install
Could not find gem 'sqlite3 (~> 1.4)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

sqlite3がないと言われるので入れようとするが

gem install sqlite3

ERROR:  Error installing sqlite3:
        ERROR: Failed to build gem native extension.

sqlite3.h is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).

yum install sqlite-devel しろと言われるのでする

yum install -y sqlite-devel

Installed:
  sqlite-devel.x86_64 0:3.7.17-8.el7_7.1

再度gem install sqlite3

Successfully installed sqlite3-1.4.2

戻ってrails new exampleするけど

rails new example
Could not find gem 'sass-rails (>= 6)' in any of the gem sources listed in your Gemfile.

sass-railsが無いといわれるので入れようとするが

gem install sass-rails

make: g++: Command not found

g++が無いといわれるので入れる 参考

yum -y install gcc-c++

もう一回 gem install sass-rails

gem install sass-rails

4 gems installed

再びrails new example

rails new example

sh: nodejs: command not found

nodejsが無いといわれるので入れる 参考

curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum install -y nodejs

Installed:
  nodejs.x86_64 2:10.18.1-1nodesource

再びrails new example

rails new example

Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/

yarn入ってないって言われるので入れる

npm install -g yarn

再びrails new example

rails new example

Done in 163.32s.

railsを起動する

cd example
rails s

* Listening on tcp://127.0.0.1:3000

ブラウザでhttp://localhost:3000 にアクセスする
image.png

参考
railsは127.0.0.1:3000でリッスンする。
ホストでhttp://localhost3000 すると、コンテナには、コンテナのipアドレス172.17.0.1:3000としてフォワードされるので、127.0.0.1 ≠ 172.17.0.1 となり上手く動かない

127.0.0.1じゃなくてもう全部でリッスン

rails s -b 0.0.0.0

ブラウザからhttp://localhost:3000 にアクセス

image.png

sqlite3のバージョンが問題なので入れ直す

ソースコードを取ってきてlibtoolでコンパイルするらしい
参考

wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz

bash: wget: command not found

そういえばwget入れてなかったので入れて再実行する

yum install -y wget
wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz
tar xzvf sqlite-autoconf-3290000.tar.gz
cd sqlite-autoconf-3290000
./configure --prefix=/opt/sqlite/sqlite3
make

/opt/docker/example/sqlite-autoconf-3290000/missing: line 81: aclocal-1.15: command not found

aclocal-1.15がないと言われる。

makeの前にautoreconf -f -iするといいらしい

autoreconf -f -i

Can't exec "aclocal": No such file or directory at /usr/share/autoconf/Autom4te/FileUtils.pm line 326.

aclocalが無いといわれる。
automakeを入れればいいらしい。参考

yum install -y automake

Installed:
  automake.noarch 0:1.13.4-3.el7
autoreconf -f -i

configure.ac:24: error: possibly undefined macro: AC_PROG_LIBTOOL

AC_PROG_LIBTOOLが未定義とか言われる。
libtoolが足りないせいらしい

yum install -y libtool

Installed:
  libtool.x86_64 0:2.4.2-22.el7_3

もっかいautoreconf -f -iする

autoreconf -f -i

libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `.'.
libtoolize: copying file `./ltmain.sh'
libtoolize: Consider adding `AC_CONFIG_MACRO_DIR([m4])' to configure.ac and
libtoolize: rerunning libtoolize, to keep the correct libtool macros in-tree.
libtoolize: Consider adding `-I m4' to ACLOCAL_AMFLAGS in Makefile.am

もっかいmake

./configure --prefix=/opt/sqlite/sqlite3
make


libtool: link: ( cd ".libs" && rm -f "libsqlite3.la" && ln -s "../libsqlite3.la" "libsqlite3.la" )
gcc -DPACKAGE_NAME=\"sqlite\" -DPACKAGE_TARNAME=\"sqlite\" -DPACKAGE_VERSION=\"3.29.0\" -DPACKAGE_STRING=\"sqlite\ 3.29.0\" -DPACKAGE_BUGREPORT=\"http://www.sqlite.org\" -DPACKAGE_URL=\"\" -DPACKAGE=\"sqlite\" -DVERSION=\"3.29.0\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -DHAVE_FDATASYNC=1 -DHAVE_USLEEP=1 -DHAVE_LOCALTIME_R=1 -DHAVE_GMTIME_R=1 -DHAVE_DECL_STRERROR_R=1 -DHAVE_STRERROR_R=1 -DHAVE_POSIX_FALLOCATE=1 -DHAVE_ZLIB_H=1 -I.    -D_REENTRANT=1 -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_RTREE -DSQLITE_HAVE_ZLIB  -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_STMTVTAB -DSQLITE_ENABLE_DBSTAT_VTAB  -g -O2 -MT sqlite3-shell.o -MD -MP -MF .deps/sqlite3-shell.Tpo -c -o sqlite3-shell.o `test -f 'shell.c' || echo './'`shell.c
mv -f .deps/sqlite3-shell.Tpo .deps/sqlite3-shell.Po
gcc -DPACKAGE_NAME=\"sqlite\" -DPACKAGE_TARNAME=\"sqlite\" -DPACKAGE_VERSION=\"3.29.0\" -DPACKAGE_STRING=\"sqlite\ 3.29.0\" -DPACKAGE_BUGREPORT=\"http://www.sqlite.org\" -DPACKAGE_URL=\"\" -DPACKAGE=\"sqlite\" -DVERSION=\"3.29.0\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -DHAVE_FDATASYNC=1 -DHAVE_USLEEP=1 -DHAVE_LOCALTIME_R=1 -DHAVE_GMTIME_R=1 -DHAVE_DECL_STRERROR_R=1 -DHAVE_STRERROR_R=1 -DHAVE_POSIX_FALLOCATE=1 -DHAVE_ZLIB_H=1 -I.    -D_REENTRANT=1 -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_RTREE -DSQLITE_HAVE_ZLIB  -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_STMTVTAB -DSQLITE_ENABLE_DBSTAT_VTAB  -g -O2 -MT sqlite3-sqlite3.o -MD -MP -MF .deps/sqlite3-sqlite3.Tpo -c -o sqlite3-sqlite3.o `test -f 'sqlite3.c' || echo './'`sqlite3.c
mv -f .deps/sqlite3-sqlite3.Tpo .deps/sqlite3-sqlite3.Po
/bin/sh ./libtool  --tag=CC   --mode=link gcc -D_REENTRANT=1 -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_RTREE -DSQLITE_HAVE_ZLIB  -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_STMTVTAB -DSQLITE_ENABLE_DBSTAT_VTAB  -g -O2   -o sqlite3 sqlite3-shell.o sqlite3-sqlite3.o  -lz -lm -ldl -lpthread
libtool: link: gcc -D_REENTRANT=1 -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_RTREE -DSQLITE_HAVE_ZLIB -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_STMTVTAB -DSQLITE_ENABLE_DBSTAT_VTAB -g -O2 -o sqlite3 sqlite3-shell.o sqlite3-sqlite3.o  -lz -lm -ldl -lpthread

良く分からんことになるが続けて

make install

libtool: install: /usr/bin/install -c sqlite3 /opt/sqlite/sqlite3/bin/sqlite3

gem入れ直し

gem uninstall sqlite3
gem install sqlite3 -- --with-sqlite3-include=/opt/sqlite/sqlite3/include \
   --with-sqlite3-lib=/opt/sqlite/sqlite3/lib

1 gem installed

rails起動(再)

cd /opt/docker/example
rails s -b 0.0.0.0

* Listening on tcp://0.0.0.0:3000

http://localhost:3000/ にアクセス
image.png

やっぱ何も知らない奴がやると駄目だな!

9
6
1

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
9
6