How to Install ffi on Apple Silicon M1 in Native Mode Without Rosetta

Published

Have you been struggling to install ffi on your M1 Mac? Have you been seeing these common errors:

Or an error like this?

[capacitor] /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require’: 
dlopen(/Library/Ruby/Gems/2.6.0/gems/ffi-1.15.4/lib/ffi_c.bundle, 0x0009): tried: 
‘/Library/Ruby/Gems/2.6.0/gems/ffi-1.15.4/lib/ffi_c.bundle’ 
(mach-o file, but is an incompatible architecture (have ‘arm64’, need ‘x86_64’)), 
‘/usr/lib/ffi_c.bundle’ (no such file) - /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.4/lib/ffi_c.bundle (LoadError)

You’ve probably been seeing these ffi issues while trying to get cocoapods, Capacitor, Slate, or sassc to work. Or maybe some other tool that depends on ffi.

Unfortunately, there’s a lot of outdated and incorrect advice out there, which causes people to mess up their development environment.

The 4 main reasons people struggle to install ffi on their M1 Mac

1) They don’t have a proper Ruby development environment, and they’re trying to install gems using the system Ruby. Learn why you shouldn’t use the system Ruby to install gems on a Mac.

2) They’re trying to install an older version. If you’re trying to install ffi as part of a Ruby project with a Gemfile, try running bundle update ffi to get the latest version.

3) They followed outdated advice to use arch -x86_64 or run terminal in Rosetta mode.

4) They followed bad advice to use sudo. Learn why you should never use sudo to install gems.

So how can you get a proper Ruby development environment so you can finally install ffi?

How to install ffi on Apple Silicon M1 in native mode

The key to a proper Ruby development environment is to install a separate and newer version of Ruby with a version manager. At a high level, it involves these 6 steps:

  1. If you’ve already tried to set up a dev environment, especially if you’ve done anything with Rosetta, you’ll need to undo everything and start over with a clean slate.
  2. Install Homebrew (which also installs the prerequisite Apple command line tools, also known as the Xcode command line tools)
  3. Install a Ruby manager
  4. Configure the Ruby manager
  5. Install a specific version of Ruby
  6. Switch to that version of Ruby

You have two options for setting up a proper Ruby environment:

Have everything set up for you in 15 minutes or less with a single command

My paid script Ruby on Mac (released in February 2022) will automatically install Ruby with chruby, ruby-install, and all the other web development tools you’ll need. And the Prime and Ultimate versions come with a reset mode that will back up and clean up your dev environment in 60 seconds, so you can start over with a clean slate. Over 700 happy customers have saved so much time and frustration.

When you buy Ruby on Mac today, you’ll be supporting an independent developer, and if you need it for your job or business, you should be able to expense it.

Read what people say about Ruby on Mac, and here are more Twitter testimonials.

Spend an hour or more installing everything manually

If you haven’t yet tried to install Ruby or other development tools on your Mac, you should be able to get up and running with the basics by following my free step-by-step guide for installing Ruby on a Mac.

If you’ve already tried to set things up and you’re running into issues you can’t figure out, you can still try my step-by-step guide, but Ruby on Mac will get you unstuck much faster.

Here’s a review from someone who had ffi issues:

You (and your wonderful script) saved me from great peril today. I had developed my first Jekyll site some months ago. And I am now in the middle of developing a site using Eleventy. Earlier today, I went back to my Jekyll site to make sure that I could still build it as I had some updates to make. Sadly, the site failed to build with the horrendous “ffi” error that is going around. After numerous ill-fated attempts to get it working, trying every workaround that I could find, I ultimately came across your script. While it does take some time to get through it, I have to say that it has worked wonderfully! My site builds once again and I feel like the ruby platform that your script placed on my system is far less fragile than before. Thank you again for the great script and your service to the community. - Bob Mansour