0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ruby on Rails 環境構築

Posted at

背景

Apple SiliconでRuby on Railsの環境を構築する

手順

Homebrewのインストール

macOSにHomebrewをインストールする

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

.zprofileの設定

macのパッケージ管理をHomebrewに任せるため、.zprofileを編集する

# ~/.zprofile に追記
eval "$(/opt/homebrew/bin/brew shellenv)"
export LDFLAGS="-L/opt/homebrew/opt/zlib/lib"
export CPPFLAGS="-I/opt/homebrew/opt/zlib/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/zlib/lib/pkgconfig"

.zprofileとは

  • macOS(zsh)でログイン時に1回だけ読み込まれる設定ファイル
  • ターミナルを開いたとき、新しいログインシェルを起動した時に実行される
  • ここにexportを書くと、以降のすべてのコマンドやプログラムでその環境変数が有効になる

各行の意味

export LDFLAGS="-L/opt/homebrew/opt/zlib/lib"

リンカに「zlibのライブライファイルはこのディレクトリにある」と教える
-Lはライブラリ検索パスの指定

export CPPFLAGS="-I/opt/homebrew/opt/zlib/include"

コンパイラに「ヘッダーファイル(.h)はここにある」と教える
-Iはヘッダ検索パスの指定

export PKG_CONFIG_PATH="/opt/homebrew/opt/zlib/lib/pkgconfig"

pkg-configというツールが、ライブラリの情報を探すときのパスを指定

反映

$ source ~/.zprofile

rbenvとruby-buildのインストール

Rubyのバージョン管理ツールを導入

brew install rbenv ruby-build

.zprofileに追記する

# rbenv設定
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

これをすることでrbenvで設定したRubyが優先的に使用される

反映

$ source ~/.zprofile

Rubyの依存ライブラリをインストール

$ brew install openssl@3 readline libyaml zlib

Rubyのインストール

エラー対策としてopenSSLのパスを明示して、rbenvでRubyをビルド

$ export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@3)"
$ rbenv install 3.4.7

もしビルドエラーが(特にzlib関連)が出たら:

$ brew reinstall zlib
$ rm -rf /var/folders/*/*/*/ruby-build.*
$ source ~/.zprofile
$ rbenv install 3.4.7

Rubyのバージョン設定

インストールしたRubyを設定する

$ rbenv global 3.4.7
$ ruby -v
# => ruby 3.4.7 (2025-10-08 revision 7a5688e2a2) +PRISM [arm64-darwin24] と表示されていればOK!

Railsのインストール

$ gem update --system ## RubyGems(gemの管理システム)本体を最新版に更新する
$ gem install bundler ## Bundler(依存関係を管理するgem)をインストールする
$ gem install rails ## Rails本体をインストールする
$ rbenv rehash ## rbenvが新しいコマンドを認識するように再設定する
$ rails -v ## Railsのバージョン確認
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?