Skip to content

Instantly share code, notes, and snippets.

@AndrewDongminYoo
Last active March 14, 2023 01:48
Show Gist options
  • Select an option

  • Save AndrewDongminYoo/ead7033edfb062f20762e3af3c8a9eb8 to your computer and use it in GitHub Desktop.

Select an option

Save AndrewDongminYoo/ead7033edfb062f20762e3af3c8a9eb8 to your computer and use it in GitHub Desktop.
MacOS에서의 루비버전 셋팅. rbenv와 homebrew를 설치하지 않았다면 다운받은 후에, 이 스크립트를 ".bash_profile", ".zshrc", ".config/fish/config.fish" 등에 불여넣으시면 됩니다. CLI 커맨드로는 `tail -n +3 ruby-version.sh >> ~/.zshrc` 하면 끝!
#!/bin/bash
# this function will check if .ruby-version file exists
function set_ruby() {
if [ -f ".ruby-version" ]; then
version=$(cat .ruby-version)
if [[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
brew_ruby=$("$HOMEBREW_PREFIX/opt/ruby/bin/ruby" --version)
if [[ $brew_ruby =~ $version ]]; then
echo "💎 now using homebrew version ($version) of Ruby."
export RUBY_PATH="$HOMEBREW_PREFIX/opt/ruby/bin"
return 0
else
if [ -d "$HOME/.rbenv/versions/$version" ]; then
echo "💎 $version version of ruby was already installed."
else
rbenv install "$version" && rbenv local "$version" && echo "💎 $version version of ruby is ready."
return 0
fi
export RUBY_PATH="$HOME/.rbenv/versions/$version/bin"
fi
else
echo "💎 .ruby-version file does not contain a valid version number."
fi
fi
}
# so, we can use this function in every opened terminal
set_ruby
# set environment variables for ruby
export PATH="$RUBY_PATH/bin:$PATH"
export LDFLAGS="-L$RUBY_PATH/lib"
export CPPFLAGS="-I$RUBY_PATH/include"
export PKG_CONFIG_PATH="$LDFLAGS/pkgconfig"
OPEN_SSL_DIR=$(brew --prefix openssl@1.1)
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$OPEN_SSL_DIR"
@AndrewDongminYoo
Copy link
Author

Installation.

@rbenv : https://github.com/rbenv/rbenv
@Homebrew : https://github.com/Homebrew/brew

# install brew first (maybe you already have brew)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# next, install rbenv (with ruby-build)
brew install rbenv ruby-build
# initialize rbenv (at first-time once)
rbenv init
# to run rbenv every shell, (zsh)
echo 'eval "$(~/.rbenv/bin/rbenv init - zsh)"' >> ~/.zshrc
# or if you don't use zsh,
echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bash_profile
# and copy this scripts on you profile!!
tail -n +3 ruby-version.sh >> ~/.zshrc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment