Francium Tech

Francium's core purpose is to create technology solutions for progressive and forward-thinking…

Follow publication

Installing EventMachine Gem from Source on Apple Silicon Macs

Braga J
Francium Tech
Published in
3 min readNov 20, 2024

--

EventMachine is a popular Ruby gem that provides event-driven I/O using the Reactor pattern. While it’s typically installed via a simple gem install eventmachine, Apple Silicon (M1/M2) Mac users often encounter compilation issues when installing from source. This guide walks through the common problems and provides a detailed solution.

Apple Silicon Macs use ARM64 architecture, which requires specific compiler configurations for native extensions. EventMachine’s native extensions were originally written with x86 architectures in mind, and its build system needs adjustments for proper ARM64 compilation.

Common Installation Failure

When attempting to install EventMachine from source on an M1/M2 Mac, you might encounter this error:

➜ git clone https://github.com/eventmachine/eventmachine.git
cd eventmachine
➜ gem build eventmachine.gemspec && \
ARCHFLAGS="-arch arm64" \
gem install eventmachine-1.2.7.gem -- \
--with-openssl-dir=$(brew --prefix openssl@3)

This typically fails with:

ERROR: Failed to build gem native extension.
extconf.rb:3:in `require_relative': cannot load such file -- [...]/em_config (LoadError)

And even if you get past this error, you might hit another one in the fastfilereader extension:

compiling mapper.cpp
make: *** [mapper.o] Error 1

The issues stem from two main factors:

  1. The main EventMachine extension needs proper C++ compiler configuration for ARM64
  2. The FastFileReader sub-extension also requires similar ARM64-specific settings
  3. The default compiler flags aren’t compatible with Apple’s ARM64 architecture

The Solution

1. Modify the Main Extension Configuration

First, edit ext/extconf.rb with these contents:

require 'fileutils'
require 'mkmf'

# Configure compiler for ARM64 Mac directly in extconf.rb
if RUBY_PLATFORM =~ /darwin/
CONFIG['CXX'] = 'clang++'
CONFIG['CC'] = 'clang'

# Set proper include paths for C++
sdk_path = `xcrun --show-sdk-path`.chomp
cxx_include = "#{sdk_path}/usr/include/c++/v1"

$CXXFLAGS = " -std=c++11 -stdlib=libc++ -I#{cxx_include}"
$LDFLAGS << " -stdlib=libc++"

# Reset any problematic flags
RbConfig::CONFIG['CXXFLAGS'] = RbConfig::CONFIG['CXXFLAGS'].to_s.gsub(/-fdeclspec/, '')
CONFIG['CXXFLAGS'] = CONFIG['CXXFLAGS'].to_s.gsub(/-fdeclspec/, '')
end

2. Modify the FastFileReader Extension

Then, edit ext/fastfilereader/extconf.rb:

require 'mkmf'

# Configure compiler for ARM64 Mac
if RUBY_PLATFORM =~ /darwin/
CONFIG['CXX'] = 'clang++'
CONFIG['CC'] = 'clang'

# Set proper include paths for C++
sdk_path = `xcrun --show-sdk-path`.chomp
cxx_include = "#{sdk_path}/usr/include/c++/v1"

$CXXFLAGS = " -std=c++11 -stdlib=libc++ -I#{cxx_include}"
$LDFLAGS << " -stdlib=libc++"

# Reset any problematic flags
RbConfig::CONFIG['CXXFLAGS'] = RbConfig::CONFIG['CXXFLAGS'].to_s.gsub(/-fdeclspec/, '')
CONFIG['CXXFLAGS'] = CONFIG['CXXFLAGS'].to_s.gsub(/-fdeclspec/, '')
end

dir_config("fastfilereader")

# Force g++ instead of gcc for linking
if CONFIG['CXX'] == 'g++'
CONFIG['LDSHARED'] = "$(CXX) -shared"
CONFIG['LDSHAREDXX'] = "$(CXX) -shared"
end

have_func('rb_thread_blocking_region')
have_func('rb_thread_call_without_gvl')

create_makefile("fastfilereader")

3. Install the Modified Gem

Now you can install the gem with:

gem build eventmachine.gemspec && \
ARCHFLAGS="-arch arm64" \
gem install eventmachine-1.2.7.gem -- \
--with-openssl-dir=$(brew --prefix openssl@3)

What Changed and Why It Works

The key modifications we made:

  1. Compiler Selection: We explicitly set clang++ as the C++ compiler, which is optimized for Apple Silicon.
  2. C++ Standard: We specify C++11 standard with -std=c++11.
  3. Standard Library: We use libc++ instead of libstdc++ with -stdlib=libc++.
  4. Include Paths: We properly set up C++ include paths using the Mac SDK.
  5. Flag Cleanup: We remove problematic flags that could interfere with compilation.

These changes ensure that:

  • The code compiles with the correct ARM64 architecture settings
  • The proper C++ standard library is used
  • All necessary headers are found
  • The linking process uses the correct flags

Requirements

  • macOS on Apple Silicon (M1/M2)
  • Homebrew installed
  • OpenSSL installed via Homebrew (brew install openssl@3)
  • Ruby installed (preferably via rbenv or RVM)
  • Xcode Command Line Tools installed

Troubleshooting Tips

If you still encounter issues:

  1. Ensure Xcode Command Line Tools are installed: xcode-select --install
  2. Verify OpenSSL installation: brew info openssl@3
  3. Check your Ruby installation: ruby -v
  4. Make sure all changes to extconf.rb files are saved properly

While installing EventMachine from source on Apple Silicon Macs requires some additional configuration, the process becomes straightforward once you understand the necessary compiler settings. The modifications we’ve made ensure proper compilation of both native extensions while maintaining compatibility with the ARM64 architecture.

This solution should work for EventMachine version 1.2.7 and similar versions, though future versions might require adjustments as the codebase evolves.

Francium Tech is a technology company laser-focused on delivering top quality software at scale and at extreme speeds. The numbers and size of the data don’t scare us. If you have any requirements or want a free health check of your systems or architecture, feel free to shoot an email to contact@francium.tech, we will get in touch with you!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Francium Tech

Francium's core purpose is to create technology solutions for progressive and forward-thinking organizations to empower their ascendancy and to magnify their impact.

No responses yet

Write a response