Search code examples
ruby-on-railsrubygemsruby-on-rails-5bundlermysql2

Rails 5 - LoadError: Error loading the 'mysql2' Active Record adapter. mysql2 is not part of the bundle


I have a long-term-stable Rails 5.1 app deployed to Ubuntu 18.04.3 using MySQL.

I came to deploy a mundane update yesterday, and for some reason I've started getting the error,

LoadError: Error loading the 'mysql2' Active Record adapter. Missing a gem it depends on? mysql2 is not part of the bundle. Add it to your Gemfile.

Of course, the gem is already in my Gemfile (and Gemfile.lock), and its installed on the deployment machine. I had made some unrelated Gemfile changes (switched to mini_racer and added bcrypt), which required me to update to Bundler 2.0.2 over 2.0.1, but that's it.

Things I've tried:

  1. I've tried going back to the code base before any gem changes and switched back to bundler 2.0.1, but I still hit the same problem on deployment - now it's broken, it seems to be staying broken.

  2. I tried deploying to a entirely independent server I have set up (still on Ubuntu 18.04) and now see the same problem.

  3. I found these apparent duplicate questions, and there the fix was to go back to an earlier version of the mysql2 gem (I tried 0.3.20 as I saw mention that 0.3.21 doesn't work), but that doesn't fix it in my case. I've tried uninstalling all other versions on the server side.

  4. The comments around the above linked question say this will be fixed in Rails 5 (I was already using 5.1) but I went through the exercise of updating my Rails app to 5.2 to get the very latest gems, and specified mysql 0.5.2 to see if I could leap frog the versions of mysql2 with the issue, but still I hit the same problem.

My 'current' Gemfile.lock file is pasted below, but this issue started with Rails 5.1.7 and Bundler 2.0.1 and I've tried all permutations of mysql2 0.3.18, 0.3.20 and 0.5.2.

Without a fix I am unable to deploy a Rails app update, so any suggestions as to how to overcome this deployment problem much appreciated.

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (5.2.3)
      actionpack (= 5.2.3)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailer (5.2.3)
      actionpack (= 5.2.3)
      actionview (= 5.2.3)
      activejob (= 5.2.3)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (5.2.3)
      actionview (= 5.2.3)
      activesupport (= 5.2.3)
      rack (~> 2.0)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    actionview (5.2.3)
      activesupport (= 5.2.3)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.3)
    activejob (5.2.3)
      activesupport (= 5.2.3)
      globalid (>= 0.3.6)
    activemodel (5.2.3)
      activesupport (= 5.2.3)
    activerecord (5.2.3)
      activemodel (= 5.2.3)
      activesupport (= 5.2.3)
      arel (>= 9.0)
    activestorage (5.2.3)
      actionpack (= 5.2.3)
      activerecord (= 5.2.3)
      marcel (~> 0.3.1)
    activesupport (5.2.3)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
    addressable (2.7.0)
      public_suffix (>= 2.0.2, < 5.0)
    airbrussh (1.4.0)
      sshkit (>= 1.6.1, != 1.7.0)
    arel (9.0.0)
    autoprefixer-rails (9.7.1)
      execjs
    bcrypt (3.1.13)
    bindex (0.8.1)
    bootstrap-sass (3.3.7)
      autoprefixer-rails (>= 5.2.1)
      sass (>= 3.3.4)
    builder (3.2.3)
    byebug (11.0.1)
    capistrano (3.11.2)
      airbrussh (>= 1.0.0)
      i18n
      rake (>= 10.0.0)
      sshkit (>= 1.9.0)
    capistrano-bundler (1.6.0)
      capistrano (~> 3.1)
    capistrano-rails (1.4.0)
      capistrano (~> 3.1)
      capistrano-bundler (~> 1.1)
    capybara (2.18.0)
      addressable
      mini_mime (>= 0.1.3)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      xpath (>= 2.0, < 4.0)
    childprocess (3.0.0)
    coffee-rails (4.2.2)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0)
    coffee-script (2.4.1)
      coffee-script-source
      execjs
    coffee-script-source (1.12.2)
    concurrent-ruby (1.1.5)
    crass (1.0.5)
    erubi (1.9.0)
    execjs (2.7.0)
    ffi (1.11.1)
    font-awesome-rails (4.7.0.5)
      railties (>= 3.2, < 6.1)
    globalid (0.4.2)
      activesupport (>= 4.2.0)
    httparty (0.17.1)
      mime-types (~> 3.0)
      multi_xml (>= 0.5.2)
    i18n (1.7.0)
      concurrent-ruby (~> 1.0)
    jbuilder (2.9.1)
      activesupport (>= 4.2.0)
    jquery-rails (4.3.5)
      rails-dom-testing (>= 1, < 3)
      railties (>= 4.2.0)
      thor (>= 0.14, < 2.0)
    libv8 (7.3.492.27.1)
    loofah (2.3.1)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.1)
      mini_mime (>= 0.1.1)
    marcel (0.3.3)
      mimemagic (~> 0.3.2)
    method_source (0.9.2)
    mime-types (3.3)
      mime-types-data (~> 3.2015)
    mime-types-data (3.2019.1009)
    mimemagic (0.3.3)
    mini_mime (1.0.2)
    mini_portile2 (2.4.0)
    mini_racer (0.2.6)
      libv8 (>= 6.9.411)
    minitest (5.13.0)
    multi_xml (0.6.0)
    mysql2 (0.3.20)
    net-scp (2.0.0)
      net-ssh (>= 2.6.5, < 6.0.0)
    net-ssh (5.2.0)
    nio4r (2.5.2)
    nokogiri (1.10.5)
      mini_portile2 (~> 2.4.0)
    public_suffix (4.0.1)
    rack (2.0.7)
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rails (5.2.3)
      actioncable (= 5.2.3)
      actionmailer (= 5.2.3)
      actionpack (= 5.2.3)
      actionview (= 5.2.3)
      activejob (= 5.2.3)
      activemodel (= 5.2.3)
      activerecord (= 5.2.3)
      activestorage (= 5.2.3)
      activesupport (= 5.2.3)
      bundler (>= 1.3.0)
      railties (= 5.2.3)
      sprockets-rails (>= 2.0.0)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.3.0)
      loofah (~> 2.3)
    railties (5.2.3)
      actionpack (= 5.2.3)
      activesupport (= 5.2.3)
      method_source
      rake (>= 0.8.7)
      thor (>= 0.19.0, < 2.0)
    rake (13.0.0)
    rb-fsevent (0.10.3)
    rb-inotify (0.10.0)
      ffi (~> 1.0)
    rubyzip (2.0.0)
    sass (3.7.4)
      sass-listen (~> 4.0.0)
    sass-listen (4.0.0)
      rb-fsevent (~> 0.9, >= 0.9.4)
      rb-inotify (~> 0.9, >= 0.9.7)
    sass-rails (5.1.0)
      railties (>= 5.2.0)
      sass (~> 3.1)
      sprockets (>= 2.8, < 4.0)
      sprockets-rails (>= 2.0, < 4.0)
      tilt (>= 1.1, < 3)
    selenium-webdriver (3.142.6)
      childprocess (>= 0.5, < 4.0)
      rubyzip (>= 1.2.2)
    sprockets (3.7.2)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.2.1)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    sqlite3 (1.4.1)
    sshkit (1.20.0)
      net-scp (>= 1.1.2)
      net-ssh (>= 2.8.0)
    thor (0.20.3)
    thread_safe (0.3.6)
    tilt (2.0.10)
    turbolinks (5.2.1)
      turbolinks-source (~> 5.2)
    turbolinks-source (5.2.0)
    tzinfo (1.2.5)
      thread_safe (~> 0.1)
    uglifier (4.2.0)
      execjs (>= 0.3.0, < 3)
    web-console (3.7.0)
      actionview (>= 5.0)
      activemodel (>= 5.0)
      bindex (>= 0.4.0)
      railties (>= 5.0)
    websocket-driver (0.7.1)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.4)
    will_paginate (3.2.1)
    xpath (3.2.0)
      nokogiri (~> 1.8)

PLATFORMS
  ruby

DEPENDENCIES
  bcrypt (~> 3.1.13)
  bootstrap-sass (~> 3.3.7)
  byebug
  capistrano-rails
  capybara (~> 2.13)
  coffee-rails (~> 4.2)
  font-awesome-rails
  httparty
  jbuilder (~> 2.5)
  jquery-rails
  mini_racer
  mysql2 (= 0.3.20)
  rails (~> 5.2)
  sass-rails (~> 5.0)
  selenium-webdriver
  sqlite3
  turbolinks (~> 5)
  uglifier (>= 1.3.0)
  web-console (>= 3.3.0)
  will_paginate

BUNDLED WITH
   2.0.2

Solution

  • Part of the problem here that mysql2 would not build on Ubuntu.

    It turned out that upgrading to bundle 2.0.2, introduced a new config file in my Rails project,

    .bundle/config
    

    Which contained,

    BUNDLE_BUILD__MYSQL2: "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
    BUNDLE_WITHOUT: "production"
    BUNDLE_WITH: "development"
    

    The specific BUNDLE_BUILD__MYSQL2 declaration is weird, and it turns out breaks mysql2 build on my Ubuntu machines.

    To fix this, I had to make two changes,

    BUNDLE_WITHOUT: ""
    

    ...which started including mysql2 gem again (not sure why).

    But this generated an error when building the native mysql2 extensions as the openssl headers / libs could not be found, which was fixed by, clear the value,

    BUNDLE_BUILD__MYSQL2: ""
    

    I can now run Rails 5.2 with mysql2 (installed using Bundler 2.0.2)

    Hope this helps someone else!