Skip to content

Instantly share code, notes, and snippets.

@dentarg
Last active October 26, 2022 18:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dentarg/79aae28811c290b7a6a96ab4fafd4197 to your computer and use it in GitHub Desktop.
Save dentarg/79aae28811c290b7a6a96ab4fafd4197 to your computer and use it in GitHub Desktop.
How to Ruby on Apple silicon
# Need to use ~/.zshenv, ~/.zprofile is not sourced when doing "arch -x86_64 /bin/zsh"
#
# From 'man zsh' (zsh 5.8.1 (x86_64-apple-darwin21.0))
#
# Commands are then read from $ZDOTDIR/.zshenv. If the shell is a login
# shell, commands are read from /etc/zprofile and then $ZDOTDIR/.zprofile.
# Then, if the shell is interactive, commands are read from /etc/zshrc and
# then $ZDOTDIR/.zshrc. Finally, if the shell is a login shell,
# /etc/zlogin and $ZDOTDIR/.zlogin are read.
# Adds the correct Homebrew PATHs to ENV based on arch, example
#
# $ /opt/homebrew/bin/brew shellenv
# export HOMEBREW_PREFIX="/opt/homebrew";
# export HOMEBREW_CELLAR="/opt/homebrew/Cellar";
# export HOMEBREW_REPOSITORY="/opt/homebrew";
# export PATH="/opt/homebrew/bin:/opt/homebrew/sbin${PATH+:$PATH}";
# export MANPATH="/opt/homebrew/share/man${MANPATH+:$MANPATH}:";
# export INFOPATH="/opt/homebrew/share/info:${INFOPATH:-}";
#
# Inspired by https://tenderlovemaking.com/2022/01/07/homebrew-rosetta-and-ruby.html
if [ $(arch) = "arm64" ]; then
[ -f /opt/homebrew/bin/brew ] && eval "$(/opt/homebrew/bin/brew shellenv)"
export RUBIES_DIR="${HOME}/.arm64_rubies"
else
[ -f /usr/local/bin/brew ] && eval "$(/usr/local/bin/brew shellenv)"
export RUBIES_DIR="${HOME}/.rubies"
fi
# chruby
chruby_dir="${HOME}/chruby/share/chruby"
if [ -f $chruby_dir/chruby.sh ]; then
. $chruby_dir/chruby.sh
. $chruby_dir/auto.sh
fi
# check that the directory exist and have entries except . and ..
[ -d $RUBIES_DIR ] && [ -n "$(ls -A $RUBIES_DIR)" ] && export RUBIES=($RUBIES_DIR/*)
export PATH="$HOME/bin:$PATH"
alias intel='arch -x86_64 /bin/zsh'
alias i='intel'
# pg_ctl needs this to be set
export PGDATA="${HOMEBREW_PREFIX}/var/postgres"
# For pg and sequel_pg gem installs that uses pg_config to find the include dirs
export PATH="${HOMEBREW_PREFIX}/opt/postgresql@14/bin:$PATH"

How to Ruby on Apple silicon

git clone https://gist.github.com/79aae28811c290b7a6a96ab4fafd4197.git /tmp/arm_mac
mv /tmp/arm_mac/.zshenv ~/
mkdir ~/bin
mv /tmp/arm_mac/rbuild ~/bin
chmod u+x ~/bin/rbuild

cd $HOME
git clone --branch do-no-set-gem-home https://github.com/eregon/chruby.git

source ~/.zshenv

# install ruby for aarch64
brew install ruby-build
rbuild 3.1.2

# install ruby for x86_64
intel
brew install ruby-build
rbuild 3.1.2
#!/bin/sh
set -e
set -u
if [ $# -eq 0 ];
then
echo "Usage: $0 <ruby version>"
exit 0
fi
command -v ruby-build >/dev/null 2>&1 || echo "No ruby-build, please install it: brew install ruby-build"
RUBY_TO_INSTALL=$1
RUBY_BUILD_PREFIX="${RUBIES_DIR}/${RUBY_TO_INSTALL}"
mkdir -p $RUBIES_DIR
echo "Installing ${RUBY_TO_INSTALL} to ${RUBY_BUILD_PREFIX}"
ruby-build $RUBY_TO_INSTALL $RUBY_BUILD_PREFIX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment