Bundler - Multiple Platforms

Bundler - Multiple Platforms
Photo by Charles Forerunner / Unsplash

As a Rails engineer, you inevitably have run across an issue with Nokogiri when running bundle install.

Most of the issues are caused by compilation issues.

Thankfully those have been fixed with Nokogiri now provides pre-compiled native gems. This means that we don't need to compile Nokogiri on our local machines.

The Nokogiri urges users to not try to

"We expect it will be a much better experience for you and allow us to focus our efforts on improving functionality rather than diagnosing installation issues."

That's all great but when you run bundle install on your Mac, bundler only installs the version for Darwin (the OS for Macs).

There's a high chance that your staging and production servers are running Linux. So when it's time to deploy, it will fail because it's missing the Linux version of the Nokogiri gem.

Configuring Bundler to accept multiple platforms

  1. Ensure that you are running on Bundler 2.2.3+

2. If you are have just updated Bundler, then you'll need to delete your Gemfile.lock file and re-run bundle install.

Near the end of the Gemfile.lock file, you'll see the PLATFORMS section. If you're running MacOS Big Sur, you'll see this.

PLATFORMS
  x86_64-darwin-20

You can add the Linux to the list of platforms by running this command.

bundle lock --add-platform x86_64-linux

NOTE: If your local machine is Linux and you need to add Darwin for the other developers on your team, just run:

bundle lock --add-platform x86_64-darwin-20

You should see this change in your Gemfile

PLATFORMS
   x86_64-darwin-20
+  x86_64-linux

Then you will successfully have both Linux and Mac versions of Nokogiri!!!