How to install Ruby on Apple Sillicon M1/M2 architecture?

Apple Sillicon M1/M2

Follow these steps:

  1. Install xcode using sudo xcodebuild -license agree
  2. Edit ~/.zshrc, adding the following export statements and aliases:
export PKG_CONFIG_PATH="/opt/homebrew/opt/readline/lib/pkgconfig"
export PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"
export LDFLAGS="-L/opt/homebrew/opt/zlib/lib"
export CPPFLAGS="-I/opt/homebrew/opt/zlib/include"
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"
export PATH="/opt/homebrew/opt/sqlite/bin:$PATH"
alias abrew='/opt/homebrew/bin/brew'
alias ibrew='arch -x86_64 /usr/local/bin/brew'
  1. Run source ~/.zshrc to apply changes.
  2. Install required dependencies using abrew install openssl libffi zlib rbenv readline ruby-build
  3. Install Ruby using RUBY_CONFIGURE_OPTS=--with-readline-dir="$(abrew --prefix readline)" rbenv install x.y.z
  4. If it does not work, try using LDFLAGS="-L/opt/homebrew/opt/libffi/lib" CPPFLAGS="-I/opt/homebrew/opt/libffi/include" RUBY_CONFIGURE_OPTS=--with-readline-dir="$(abrew --prefix readline)" rbenv install x.y.z

Credit for the solution goes to @otavioschwanck - thank you!

How to install gem mysql2 on Apple Sillicon M1/M2 architecture?

Apple Sillicon M1/M2Database

In your CLI, run the following commands:

brew install mysql
brew install openssl@3
bundle config build.mysql2 \
  --with-mysql-lib=$(brew --prefix mysql)/lib \
  --with-mysql-dir=$(brew --prefix mysql)\
  --with-mysql-config=$(brew --prefix mysql)/bin/mysql_config \
  --with-mysql-include=$(brew --prefix mysql)/include\
  --with-ldflags=-L/$(brew --prefix openssl@3)/lib\
  --with-cppflags=-I/$(brew --prefix openssl@3)/include
gem install mysql2

Credit for the solution goes to @newx - thank you!

How to install gem pg on Apple Sillicon M1/M2 architecture?

Apple Sillicon M1/M2Database

In your CLI, run the following commands:

brew install libpq
# add to .zshrc
export PATH="$(brew --prefix libpq)/bin:$PATH"
gem install pg

Credit for the solution goes to @wbharding - thank you!

How to install gem sqlite3 on Apple Sillicon M1/M2 architecture?

Apple Sillicon M1/M2Database

Follow these steps:

  1. Install sqlite on your system using brew install sqlite
  2. In your project, run bundle add sqlite3
  3. If the installation fails, edit the sqlite3 reference in your Gemfile to include following installation flags:
gem "sqlite3", "~> x.y", platforms: [:ruby]

This also applies to running SQLite3 with Ruby in the alpine-based docker containers on Apple M1/M2 architecture.

Hix logo

Try hix.dev now

Do not repeat any of the project configuration ever again.

Automate your projects initialization.

Try Hix