1

I would like to switch Ruby versions on my Mac M1. My understanding is that Mac comes with a preinstalled version of Ruby (in my case, when I run ruby -v I get:

ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin23]

I have installed ruby with brew:

ruby 3.3.1 is already installed and up-to-date.

How can I switch to this version? Nothing I have found online and tried has helped me so far.

I feel like something in one of my configuration files may be causing a conflict somewhere that doesn't allow me to switch versions.

Here is my .zshrc:

if [ -d "/opt/homebrew/opt/ruby/bin" ]; then
  export PATH=/opt/homebrew/opt/ruby/bin:$PATH
  export PATH=⁠ gem environment gemdir ⁠/bin:$PATH
fi

#export PATH="/opt/homebrew/opt/ruby/bin:$PATH"

#[ -f "/Users/xxx/.ghcup/env" ] && source "/Users/xxx/.ghcup/env" # ghcup-env
export PATH=/usr/local/bin:$PATH
export PATH="$PATH:/Users/xxx/Library/Application Support/Coursier/bin"


export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/xxx/opt/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/xxx/opt/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/Users/xxx/opt/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/Users/xxx/opt/anaconda3/bin:$PATH"
    fi
fi

unset __conda_setup
# <<< conda initialize <<<

[ -f "/Users/xxx/.ghcup/env" ] && source "/Users/xxx/.ghcup/env" # ghcup-env

if [ "$ARCH" = '' ]
then
  ARCH=⁠ arch ⁠
fi
export TOOLDIR=$HOME/c-tools
export PATH="$TOOLDIR/bin:$TOOLDIR/bin/$ARCH:$PATH"
if [ "$MANPATH" = '' ]
then
  export MANPATH=$TOOLDIR/man
else
  export MANPATH=${MANPATH}:$TOOLDIR/man
fi

export PATH="$PATH:/Users/xxx/flutter/flutter-2/bin"

# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/xxx/google-cloud-sdk/path.zsh.inc' ]; then . '/Users/xxx/google-cloud-sdk/path.zsh.inc'; fi

# The next line enables shell command completion for gcloud.
if [ -f '/Users/xxx/google-cloud-sdk/completion.zsh.inc' ]; then . '/Users/xxx/google-cloud-sdk/completion.zsh.inc'; fi

export JAVA_HOME=$(/usr/bin/java)

#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="$HOME/.sdkman"
[[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh"

Here is my .bash_profile:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/xxx/opt/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/xxx/opt/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/Users/xxx/opt/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/Users/xxx/opt/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

. "$HOME/.cargo/env"

# >>> coursier install directory >>>
export PATH="$PATH:/Users/xxx/Library/Application Support/Coursier/bin"
# <<< coursier install directory <<<

#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="$HOME/.sdkman"
[[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh"

export PATH=${PATH}:/Users/xxx/prolog/bin/sicstus-4.7.0

Here is my .profile:

export PATH=$PATH:/usr/local/go/bin

# >>> coursier install directory >>>
export PATH="$PATH:/Users/xxx/Library/Application Support/Coursier/bin"
# <<< coursier install directory <<<

I understand that most of the above is irrelevant but I have given these as I feel like there may be a conflict somewhere that is causing my issue.

3
  • You don't really want to do this. Instead use a ruby version manager like rvm, asdf, rbenv, etc. Commented May 9 at 16:56
  • All three of these fail at the build stage. I think the problem is to do with openssl and psych not being configured, but they are both already installed on my machine.
    – user985216
    Commented May 9 at 17:31
  • 3
    OK I managed to get it working with rvm by running rvm install 3.3.1 --with-openssl-dir=/usr/local/opt/[email protected]. Thank you so much!
    – user985216
    Commented May 9 at 17:39

1 Answer 1

0

Personally, I use ruby-install to download my ruby versions and chruby for switching. Other popular version managers are RVM and RBenv, which both have functions for installing and switching rubies all-in-one. I prefer the lightweightness of chruby and ruby-install though.

For ruby-install/chruby, it'd look something like this:

# first install via homebrew
brew install ruby-install 
brew install chruby 

After installing chruby, you want to add a couple lines to your .zshrc file:

source /opt/homebrew/opt/chruby/share/chruby/chruby.sh
source /opt/homebrew/opt/chruby/share/chruby/auto.sh

Then in command line, just do:

# download ruby 
ruby-install ruby-3.3.1 # or whichever version

# Switch ruby version
chruby ruby-3.3.1
2
  • But when I add the lines to .zshrc, I get: /Users/xxx/.zshrc:source:77: no such file or directory: /opt/homebrew/opt/chruby/share/chruby/chruby.sh /Users/xxx/.zshrc:source:78: no such file or directory: /opt/homebrew/opt/chruby/share/chruby/auto.sh Why would this be happening?
    – user985216
    Commented May 9 at 17:16
  • Probably depends on your local env and version of chruby. Those are the references I am using, but might be different for you. I would follow the chruby installation directions on their github. Also, I believe the post-install directions for chruby, will tell you the exact references you need in your .zshrc
    – MicahBrown
    Commented May 9 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.