How to fix Ruby error : OpenSSL is not available (Mac)

The Error

This is the error that you may get while installing a gem using gem install or bundle install.

ERROR:  While executing gem ... (Gem::Exception)
    OpenSSL is not available. Install OpenSSL and rebuild Ruby (preferred) or use non-HTTPS sources

I have seen this error in M1 Macs and the issue is due to ruby not having OpenSSL support compiled into it

The Fix

There are two parts to it, it works only if you reinstall ruby with openssl 1.1 support. This is very important to keep in mind, it will not work if you are using openssl 3 instead.

Step 0 : Ensure that we are dealing with the correct ruby version

You might be using rvm, or rbenv or something else to manage multiple versions of ruby, it is also possible that you are using system ruby. In any case, it is important to keep in mind that we are dealing with the correct ruby version you want to use in your application. That means to use rvm or rbenv to switch to the appropriate version before proceeding

Step 1 : Identify that the issue is due to OpenSSL itself

Run

ruby -ropenssl -e 'puts OpenSSL::OPENSSL_VERSION'

on a terminal and see if it is printing the OpenSSL version. If it errors, then we know that the issue is due to OpenSSL. Then you can proceed with the following steps. If ruby says that OpenSSL is present, like below

➜  ~ ruby -ropenssl -e 'puts OpenSSL::OPENSSL_VERSION'
OpenSSL 1.1.1u  30 May 2023

This means that this is not the issue, I would suggest you research more

Step 2 : Install OpenSSL 1.1 if not already

Assuming you have home brew installed, you can install OpenSSL 1.1 using

brew install [email protected]

Step 3 : Re-install ruby with openssl 1.1

Note : replace the version 3.0.0 with your preferred version

If using rvm

RVM allows to reinstall. So if you have the ruby version installed, you can run

rvm reinstall 3.0.0 --with-openssl-dir=`brew --prefix [email protected]`

If you do not have it installed, you can run rvm install instead of rvm reinstall

Set it as the default

rvm alias create default 3.0.0

If using rbenv

First, you need to uninstall the ruby. Assuming you want ruby 3.0.0, uninstall it first using

rbenv uninstall 3.0.0

And Once that is done, you can install ruby again with openssl support.

Note: Openssl 3 does not work (tested on ruby 3.0.0), you need openssl 1.1
RUBY_CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix [email protected]`" rbenv install 3.0.0

And set it as the default

rbenv global 3.0.0

Verify

➜  ~ ruby -ropenssl -e 'puts OpenSSL::OPENSSL_VERSION'
OpenSSL 1.1.1u  30 May 2023

That should be it. You are good to go.