Skip to content

Instantly share code, notes, and snippets.

@MarceloCajueiro
Last active February 12, 2025 13:45
Show Gist options
  • Save MarceloCajueiro/0ed04474005d4860008cac397f3f6a5e to your computer and use it in GitHub Desktop.
Save MarceloCajueiro/0ed04474005d4860008cac397f3f6a5e to your computer and use it in GitHub Desktop.
Installing Ruby 2.5.X on Mac M1/M2 Without Rosetta

Ruby 2.5.9 is an old version that does not officially support Apple Silicon (ARM architecture). Additionally, it depends on OpenSSL 1.1, which has been disabled in Homebrew since October 2024. This means that installing Ruby 2.5.9 on a Mac M1/M2 requires a manual installation of OpenSSL 1.1, since it’s no longer available via Homebrew.

This guide will walk you through installing Ruby 2.5.9 on an Apple Silicon Mac without using Rosetta, covering OpenSSL 1.1 installation, Ruby installation using asdf, and general compatibility notes.


Why OpenSSL 1.1?

Ruby 2.5.9 requires OpenSSL 1.1, but Homebrew has disabled it due to security concerns. Since newer OpenSSL versions (such as 3.x) are not compatible with Ruby 2.5.9, we must manually download, compile, and install OpenSSL 1.1.


Step 1: Install OpenSSL 1.1 Manually

Since Homebrew no longer provides OpenSSL 1.1, you need to download it manually from the official website.

1.1 Download OpenSSL 1.1

Downloading via curl may not work due to SSL/TLS issues. Instead, download it directly from your browser:

🔗 Download OpenSSL 1.1.1whttps://www.openssl.org/source/openssl-1.1.1w.tar.gz

Move the downloaded file to your terminal’s working directory.

1.2 Extract and Compile OpenSSL 1.1

Run the following commands in the terminal:

tar -xvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w

Now, compile and install OpenSSL 1.1:

./config --prefix=/opt/openssl-1.1
make -j$(sysctl -n hw.ncpu)
sudo make install

This will install OpenSSL 1.1 in /opt/openssl-1.1.


Step 2: Configure OpenSSL 1.1 for Ruby

To ensure Ruby uses the correct OpenSSL version, update your environment variables:

export PATH="/opt/openssl-1.1/bin:$PATH"
export LDFLAGS="-L/opt/openssl-1.1/lib"
export CPPFLAGS="-I/opt/openssl-1.1/include"
export PKG_CONFIG_PATH="/opt/openssl-1.1/lib/pkgconfig"

To make these changes permanent, add them to your ~/.zshrc or ~/.zshenv:

echo 'export PATH="/opt/openssl-1.1/bin:$PATH"' >> ~/.zshrc
echo 'export LDFLAGS="-L/opt/openssl-1.1/lib"' >> ~/.zshrc
echo 'export CPPFLAGS="-I/opt/openssl-1.1/include"' >> ~/.zshrc
echo 'export PKG_CONFIG_PATH="/opt/openssl-1.1/lib/pkgconfig"' >> ~/.zshrc
source ~/.zshrc

Step 3: Install Ruby 2.5.9

Each user may have their own preferred Ruby version manager. This tutorial demonstrates using asdf, but you can adapt it to rbenv or ruby-install.

3.1 Install asdf and Dependencies

If you haven't installed asdf, do it via Homebrew:

brew install asdf

Then, ensure asdf is loaded:

echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ~/.zshrc
source ~/.zshrc

3.2 Install Ruby 2.5.9 with asdf

Now, install Ruby 2.5.9, ensuring it links to OpenSSL 1.1:

RUBY_CFLAGS=-DUSE_FFI_CLOSURE_ALLOC RUBY_CONFIGURE_OPTS="--with-openssl-dir=/opt/openssl-1.1" asdf install ruby 2.5.9

Alternative: Installing with rbenv

If you prefer rbenv, use:

brew install rbenv
eval "$(rbenv init -)"
RUBY_CFLAGS=-DUSE_FFI_CLOSURE_ALLOC RUBY_CONFIGURE_OPTS="--with-openssl-dir=/opt/openssl-1.1" rbenv install 2.5.9
rbenv global 2.5.9

Step 4: Verify Installation

After installing Ruby, check if the correct version is active:

ruby --version

It should output:

ruby 2.5.9p229 (2021-04-05 revision 67939) [arm64-darwin24]

To confirm OpenSSL is correctly linked:

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

Expected output:

OpenSSL 1.1.1w  11 Sep 2023

Step 5: Install Bundler (Fix for RubyGems Issues)

Since RubyGems versions newer than 3.3.x may not be fully compatible with Ruby 2.5.9, you need to downgrade RubyGems before installing Bundler.

5.1 Update RubyGems to a Compatible Version

Run the following command:

gem install rubygems-update -v 3.2.3
update_rubygems

This ensures that RubyGems works correctly with Ruby 2.5.9.

5.2 Install Bundler 2.3.26

Now, install the Bundler version compatible with Ruby 2.5.9:

gem install bundler -v 2.3.26

If you get a timeout error, retry the installation.


Final Notes

Now, Ruby 2.5.9 is installed on your Mac M1/M2 without Rosetta! 🎉

If you face any issues:

  • Ensure OpenSSL 1.1 is correctly installed and referenced.
  • Restart your terminal and re-run source ~/.zshrc if necessary.
  • If you still face dependency issues, consider using Docker or a newer Ruby version for better support.

This guide ensures a legacy-compatible Ruby 2.5.9 installation on Apple Silicon Macs while avoiding Rosetta.

Happy coding! 🚀

@MarceloCajueiro
Copy link
Author

If you're experiencing timeouts when installing Ruby gems on macOS, the issue may be related to IPv6 connectivity. Disabling IPv6 has been reported as an effective solution. Here's how you can do it:

  1. Open Terminal: You can find it in Applications > Utilities > Terminal.

  2. List All Network Services: Run the following command to see all network services on your system:

    networksetup -listallnetworkservices

    This will display a list of network services, such as Wi-Fi, Ethernet, etc.

  3. Disable IPv6: For each network service, disable IPv6 by running:

    sudo networksetup -setv6off "SERVICE_NAME"

    Replace "SERVICE_NAME" with the name of the network service, e.g., "Wi-Fi" or "Ethernet". If the service name contains spaces, ensure you enclose it in quotes.

  4. Re-enable IPv6 (Optional): If you wish to re-enable IPv6 later, use:

    sudo networksetup -setv6automatic "SERVICE_NAME"

By disabling IPv6, your system will default to using IPv4, which can resolve timeout issues during gem installations. Remember to re-enable IPv6 if you encounter any network issues or require IPv6 connectivity in the future.

@MarceloCajueiro
Copy link
Author

When installing the sidekiq-pro gem, you might encounter SSL certificate verification errors, such as:

Retrying fetcher due to error (2/4): Bundler::Fetcher::CertificateFailureError Could not verify the SSL certificate for https://gems.contribsys.com/.
There is a chance you are experiencing a man-in-the-middle attack, but most likely your system doesn't have the CA certificates needed for verification.

This issue often arises due to outdated or missing CA certificates on your system, which are necessary to verify SSL connections.

Solution:

To bypass this SSL verification issue on your local machine, you can configure Bundler to skip SSL certificate verification for the specific source. This approach is generally safe for local development but should be used cautiously in production environments.

Configure Bundler to Skip SSL Verification:

Run the following command in your terminal:

bundle config ssl_verify_mode 0

This command sets the SSL verification mode to '0', which disables SSL certificate verification for Bundler.

Caution:

Disabling SSL certificate verification can expose your system to security risks, such as man-in-the-middle attacks. It is recommended to use this approach only in controlled development environments. For production systems, ensure that your CA certificates are up-to-date and that SSL verification is enabled to maintain secure communications.

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