ruby on railsの開発環境をmacで構築しようとしたのですが、結局rbenvを使ったruby 2.6.3のインストールがうまくいかなかったので、 docker-composeを使って環境構築した話になります。

構築したrubyなどのバージョン

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
$ rails -v
Rails 5.2.3
$ docker-compose -v
docker-compose version 1.23.2, build 1110ad01
$ docker -v
Docker version 18.09.2, build 6247962
$ mysql -V
mysql  Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using  EditLine wrapper

まずはmacで環境構築頑張った記録から

rbenvのインストール

homebrewをつかってrbenvをインストール

$ brew install rbenv

これは普通にインストールできました。

ruby 2.6.3のインストール

$ rbenv install 2.6.3

エラーが発生(一回目)

$ rbenv install 2.6.3
ruby-build: use openssl from homebrew
Downloading ruby-2.6.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.3.tar.bz2
Installing ruby-2.6.3...
ruby-build: use readline from homebrew

BUILD FAILED (OS X 10.14.5 using ruby-build 20190423)

Inspect or clean up the working tree at /var/folders/s_/fmyljq3s0nv4hmb7d8ftkzcm0000gn/T/ruby-build.20190709152027.5604
Results logged to /var/folders/s_/fmyljq3s0nv4hmb7d8ftkzcm0000gn/T/ruby-build.20190709152027.5604.log

Last 10 log lines:
checking whether -pie is accepted as LDFLAGS... no
checking whether -Wl,-pie is accepted as LDFLAGS... no
checking whether wrapper for DYLD_FALLBACK_LIBRARY_PATH is needed... yes
checking whether dtrace USDT is available... no
checking for __builtin_setjmp... (cached) no
checking for _setjmpex as a macro or function... no
checking for _setjmp as a macro or function... (cached) no
checking for sigsetjmp as a macro or function... no
checking for setjmp type... configure: error: sigsetjmp is not available
make: *** No targets specified and no makefile found.  Stop.

対応したこと

参考

Unable to install Ruby 2.5.1 on macOS 10.14 Mojave rbenvでrubyをインストールできなかったのでメモ

xcode

$ xcode-select -p
/Applications/Xcode.app/Contents/Developer
$ sudo xcode-select --switch /Library/Developer/CommandLineTools
Password:
$ xcode-select -p
/Library/Developer/CommandLineTools

アップデート

$ brew update
$ brew upgrade

再インストール

$ brew uninstall rbenv ruby-build
$ rm -rf ~/.rbenv

ruby 2.6.3のインストール(二回目)

$ rbenv install 2.6.3

またもエラーが発生

$ rbenv install 2.6.3
ruby-build: use openssl from homebrew
Downloading ruby-2.6.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.3.tar.bz2
Installing ruby-2.6.3...
ruby-build: use readline from homebrew

BUILD FAILED (OS X 10.14.5 using ruby-build 20190423)

Inspect or clean up the working tree at /var/folders/s_/fmyljq3s0nv4hmb7d8ftkzcm0000gn/T/ruby-build.20190709173501.41831
Results logged to /var/folders/s_/fmyljq3s0nv4hmb7d8ftkzcm0000gn/T/ruby-build.20190709173501.41831.log

Last 10 log lines:
checking size of long... 8
checking size of long long... 8
checking size of __int64... 0
checking size of __int128... ./configure: line 2390: /bin/cat: Permission denied
0
checking size of off_t... ./configure: line 2390: /bin/cat: Permission denied
configure: error: in `/var/folders/s_/fmyljq3s0nv4hmb7d8ftkzcm0000gn/T/ruby-build.20190709173501.41831/ruby-2.6.3':
configure: error: cannot compute sizeof (off_t)
See `config.log' for more details
make: *** No targets specified and no makefile found.  Stop

対応

キャッシュを削除してみた

$ rm -rf /var/folders/s_/fmyljq3s0nv4hmb7d8ftkzcm0000gn/T/ruby-build.201907091*

ruby 2.6.3のインストール(3回目)

$ rbenv install 2.6.3

エラーが発生・・・

$ rbenv install 2.6.2
ruby-build: use openssl from homebrew
Downloading ruby-2.6.2.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.2.tar.bz2
Installing ruby-2.6.2...
ruby-build: use readline from homebrew

BUILD FAILED (OS X 10.14.5 using ruby-build 20190423)

Inspect or clean up the working tree at /var/folders/s_/fmyljq3s0nv4hmb7d8ftkzcm0000gn/T/ruby-build.20190709175744.49715
Results logged to /var/folders/s_/fmyljq3s0nv4hmb7d8ftkzcm0000gn/T/ruby-build.20190709175744.49715.log

Last 10 log lines:
checking for strip... strip
checking whether -fPIE is accepted as CFLAGS... no
checking whether wrapper for DYLD_FALLBACK_LIBRARY_PATH is needed... yes
checking whether dtrace USDT is available... no
checking for __builtin_setjmp... (cached) no
checking for _setjmpex as a macro or function... no
checking for _setjmp as a macro or function... (cached) no
checking for sigsetjmp as a macro or function... no
checking for setjmp type... configure: error: sigsetjmp is not available
make: *** No targets specified and no makefile found.  Stop.

エラーを見る限り、gcc周りでエラーが起きているように見える。 もしかしてbrewの環境が悪いのかな?

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and which additional flags to use when
compiling and linking.

Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew-provided
script of the same name. We found the following "config" scripts:
  ~/.pyenv/shims/python2-config
  ~/.pyenv/shims/icu-config
  ~/.pyenv/shims/libpng16-config
  ~/.pyenv/shims/python3.7-config
  ~/.pyenv/shims/python2.7-config
  ~/.pyenv/shims/python3.7m-config
  ~/.pyenv/shims/python-config
  ~/.pyenv/shims/python3-config
  ~/.pyenv/shims/ncursesw6-config
  ~/.pyenv/shims/pcre-config

Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  node

なんか時間がかかりそうなので、諦めてdocker-compose使うことにしました。

docker-composeを使用した開発環境構築手順

DockerでRuby on Railsの開発をしよう Quickstart: Compose and Rails

ディレクトリ構成

最終的にはこうなります。

$ tree -L 1
.
├── Dockerfile
├── Gemfile
├── Gemfile.lock
├── LICENSE
├── README.md
├── Rakefile
├── app
├── bin
├── config
├── config.ru
├── db
├── docker-compose.yml
├── entrypoint.sh
├── lib
├── log
├── package.json
├── public
├── storage
├── test
├── tmp
└── vendor

Dockerfile

FROM ruby:2.6.3
ENV LANG C.UTF-8
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

version: '3'

services:
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      # MYSQL_ALLOW_EMPTY_PASSWORD: "yes"  パスワードなしにしたい場合はこれ
    ports:
      - '3306:3306'
    volumes:
      - mysql_data:/var/lib/mysql
volumes:
  bundle:
  mysql_data:

endpoint.sh

#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

プロジェクト作成

$ docker-compose run web rails new . --force --database=mysql --skip-bundle

このコマンドでプロジェエクトの新規作成ができます。

ビルド、起動

$ docker-compose build
$ docker-compose up -d

データベース作成

$ docker-compose run web rake db:create

アクセス

http://localhost:3000/

成果物

https://github.com/hirasaki1985/rubyonrails_sample git に置いておきました。