Search code examples
ruby-on-railsrubyopensslmacos-sonoma

Trouble Installing Specific Version of Ruby (OpenSSL Issue)


I have a 2020 Intel Mac running Sonoma. I'm working on a web app project that is running on Rails. For the last few months, I've had the same issue no matter what I try. I follow the Rails instructions on their website but I keep running into OpenSSL errors. I've tried asdf, rvm, and rbenv for package managers and am trying to install Ruby 3.0.4, which is what the app runs on.

I keep getting a error that looks something like this:

*** Following extensions are not compiled:
openssl:
    Could not be configured. It will not be installed.

I've tried downloading and redownloading OpenSSL, trying different versions, and changing my paths accordingly, as well as changing flags and other things. However, I can't seem to make this work on my machine. I eventually got it working on a Oracle VirtualBox, but I'd prefer to have it just running on my Mac.

Anyone have any idea or solutions. I've seen a lot of posts regarding this on different websites, so it seems to be an issue with a lot of people, but nothing is working for me! Thank you!


Solution

  • Ruby 3.0 requires OpenSSL version 1.1, which is quite old now and unsupported on a lot of environments. It is advised that you upgrade the app to run on at least Ruby 3.1, which uses modern OpenSSL version.

    That said on OSX you can still get this running, using homebrew, rbenv and ruby-build. Note, that the guide will use rbenv as the ruby manager, if you use something else (like rvm) then you'll need to change some of the guide to cater for that. (Or uninstall that and swap to rbenv)

    Do note that the guide below will install ruby 3.0.6 and not 3.0.4. You should probably move tho this version as this is the latest of the 3.0.x branches, which should still not break your existing app.

    Also note that the steps below should also work with ruby 2.7 if needed. and we had some success with 2.6, but there were some other steps needed to get it running that I don't remember.

    1. Make sure you have homebrew installed. Should be standard if you do development on a Mac though.

    2. Assuming you don't have a ruby manager installed yet install and set up rbenv:

    brew install rbenv
    
    if [ -n "$ZSH_VERSION" ]; then
      SHELL_CONFIG_FILE_PATH=~/.zshrc
    elif [ -n "$BASH_VERSION" ]; then
      SHELL_CONFIG_FILE_PATH=~/.bashrc
    else
      SHELL_CONFIG_FILE_PATH=~/.profile
    fi
    
    echo "eval '$(rbenv init -)'" >> $SHELL_CONFIG_FILE_PATH
    echo "export PATH='$HOME/.rbenv/bin:$PATH'" >> $SHELL_CONFIG_FILE_PATH
    source $SHELL_CONFIG_FILE_PATH
    

    If you use something else like rvm or chruby then you can skip the above, although note that ruby-build works best with rbenv.

    1. Install ruby-build:
    brew install ruby-build
    
    1. Install openssl 1.1:
    brew install [email protected]
    
    1. Install ruby with the proper openssl version:
    export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix [email protected])"
    rbenv install 3.0.6
    

    Note, if you don't use rbenv then you have to use ruby-build natively:

    export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix [email protected])"
    ruby-build 3.0.6 /opt/rubies/3.0.6
    

    and then configure your other ruby manager to point to this version of ruby.

    1. Set up some symlinks (Optional, but needed by some gems. Only do this if you encounter issues otherwise as it can break apps running on newer openssl versions):

    For Intel mac or Homebrew running under Rosetta:

    ln -sf /usr/local/opt/[email protected]/lib/libcrypto.dylib /usr/local/lib/libcrypto.dylib
    ln -sf /usr/local/opt/[email protected]/lib/libssl.dylib /usr/local/lib/libssl.dylib
    

    For ARM mac:

    ln -sf /opt/homebrew/opt/[email protected]/lib/libcrypto.dylib /usr/local/lib/libcrypto.dylib
    ln -sf /opt/homebrew/opt/[email protected]/lib/libssl.dylib /usr/local/lib/libssl.dylib
    
    1. Manually install the openssl gem for your ruby:
    rbenv local 3.0.6
    
    gem install openssl -v '2.2.3' -- --with-openssl-dir=$(brew --prefix [email protected])
    

    (do this for every openssl gem version that's in your bundle file)

    1. Issues with gems.

    Some gems might need to be linked to a different openssl, if that's the case you might use the below. For example we had issues with puma and eventmachine:

    brew install openssl@3 openssl
    
    bundle config set --global build.eventmachine --with-ssl-dir=$(brew --prefix openssl@3)
    bundle config set --global build.puma --with-openssl-dir=$(brew --prefix openssl@3)
    

    These steps should get you through, and still work as of January 2024, on Sonoma for both Intel and ARM macs.

    Also if you need to install a newer ruby while keeping support for 3.0.6 you should then use:

    export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@3)"
    
    rbenv install 3.1.4
    # works with 3.2 and 3.3 as well