LoginSignup
27
20

More than 3 years have passed since last update.

Mac M1 Big Sur にRuby / Railsをインストール 2021-01

Last updated at Posted at 2021-01-02

M1のMac miniを買ったので、RubyとRailsのインストールを試しました。結論から言うと、2021年1月現在、Homebrewとrbenvを使えば問題なくインストールできます。ただし、Rails(開発環境)の動作にちょっとへんなところがあります。また、2.6.6のコンパイルでエラーが出ることがあります。

環境: Apple M1, macOS Big Sur, Ruby 2.6.6/2.7.2/3.0.0, Rails 6.1.0

コマンドライン・デベロッパツールのインストール

次のコマンドを実行します。「"clang"コマンドを実行するには、コマンドライン・デベロッパツールが必要です。ツールを今すぐインストールしますか?」というダイアログが出るので「インストール」を押します。

% clang

次のコマンドを実行しても同じです。なお、Xcodeのインストールは必要ありません。

% xcode-select --install

関連記事: コマンドライン・デベロッパツールについてメモ

Homebrew、rbenvのインストール

Homebrewのサイト からコマンドをコピペしてHomebrewをインストールします。

% /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

IntelのMacとは違い、インストール先のディレクトリは /opt/homebrew になりますので、環境変数PATHにHomebrewのbinディレクトリを追加します。.zshrcを編集してターミナルを開き直します。

.zshrc
export PATH="/opt/homebrew/bin:$PATH"

ruby-buildとrbenvをインストールします。

% brew install ruby-build rbenv

OpenSSL 1.1 をインストールします。

% brew install openssl

.zshrcにOpenSSLとrbenvの設定を書きます。これ以外の環境変数は、記述しなくても必要なものはインストールできます。ターミナルを開き直します。

.zshrc
export PATH="/opt/homebrew/bin:$PATH"
export PATH="$(brew --prefix openssl@1.1)/bin:$PATH"

export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"

eval "$(rbenv init -)"

rbenvでRubyをインストールします。

% rbenv install 2.7.2

rbenvでRubyのバージョンを2.7.2にします。

% rbenv local 2.7.2

Rubyのバージョンを確認します。「arm64」という表示を見てニンマリします。

システムのrubyコマンド(/usr/bin/ruby)のままになっているときは、ターミナルを開き直してください。

% ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [arm64-darwin20]

Ruby 3.0.0も入れておきます。

% rbenv install 3.0.0

2.6.6でのコンパイル失敗

2021-01-12追記:rbenvで2.6.6をインストールするときに、Rubyのコンパイルが失敗しました。ログには次のエラーが出ています。ext/fiddle/closure.c で失敗しています。

closure.c:264:14: error: implicit declaration of function 'ffi_prep_closure' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    result = ffi_prep_closure(pcl, cif, callback, (void *)self);

これを防ぐには、まずHomebrewでlibffiをインストールします。

% brew install libffi

環境変数PKG_CONFIG_PATHを設定し、Homebrewでインストールしたlibffiを使うようにします。

.zshrc
export PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig"

参照: https://github.com/rvm/rvm/issues/4968

Railsのインストール

Bundlerのバージョンを上げておきます。

% gem install bundler -N

Ruby 3.0.0の場合は、bundle installでnokogiriのインストールに失敗するので、先にオプションを指定してインストールしておきます(2020/01/02現在)。

% gem install nokogiri -- --use-system-libraries

Railsの最新版をインストールします。-N オプションはriドキュメントを省いてインストールを速くするものです。

% gem install rails -N

バージョンを確認します。システムのrailsコマンド(/usr/bin/rails)のままになっているときは、ターミナルを開き直します。

% rails --version
Rails 6.1.0

yarn をインストールします。

% brew install node yarn

新しいアプリケーションを作ってみます。

% rails new generic

アプリケーションを起動します。

% cd generic
% bin/rails s

http://localhost:3000/ でページにアクセスはできますが、次のエラーが出続けます(2020/01/02現在)。

E, [2021-01-02T20:15:05.838500 #91997] ERROR -- : Exception rescued in listen-worker_thread:
Errno::EBADARCH: Bad CPU type in executable - /Users/kazuto/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rb-fsevent-0.10.4/bin/fsevent_watch
/Users/kazuto/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rb-fsevent-0.10.4/lib/rb-fsevent/fsevent.rb:140:in `popen'

これを防ぐには、開発環境の設定で、config.file_watcher を変更します。

config/environments/development.rb
# config.file_watcher = ActiveSupport::EventedFileUpdateChecker
  config.file_watcher = ActiveSupport::FileUpdateChecker

おまけ: PostgreSQL/MySQLインストール

PostgreSQLのバージョン11をインストールします。

% brew install postgresql@11

.zshrcでバージョン11のパスを追加します。これだけで、gemのpgはコンパイルできます。

.zshrc
export PATH="/opt/homebrew/bin:$PATH"
export PATH="$(brew --prefix openssl@1.1)/bin:$PATH"
export PATH="$(brew --prefix postgresql@11)/bin:$PATH"
# 以下略

PostgreSQLを起動します。

% brew services start postgresql@11

MySQL 5.7をインストールします。

% brew install mysql@5.7

.zshrcでパスを追加します。

.zshrc
export PATH="/opt/homebrew/bin:$PATH"
export PATH="$(brew --prefix openssl@1.1)/bin:$PATH"
export PATH="$(brew --prefix postgresql@11)/bin:$PATH"
export PATH="$(brew --prefix mysql@5.7)/bin:$PATH"
# 以下略

MySQLを起動します。

% brew services start mysql@5.7

bundlerでgemのmysql2をインストールすると、ld: library not found for -lssl というエラーが出ます(2021/01/02現在)。先にオプション付きでmysql2をインストールしておきます。

% gem install mysql2 -- --with-opt-dir="$(brew --prefix openssl@1.1)"

MySQL 5.7はパスワード必須のはずですが、Homebrewでインストールすると、rootのパスワードなしで使えます。パスワード必須にするには、次のコマンドで設定します。

% mysql_secure_installation
27
20
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
27
20