71

I upgraded to Rails 7 and Ruby 3.1. While trying to run tests with rspec I got the error below. How can I fix it?

An error occurred while loading rails_helper.
Failure/Error: require File.expand_path('../config/environment', __dir__)

LoadError:
  cannot load such file -- net/smtp
# .../gems/mail-2.7.1/lib/mail.rb:9:in `<module:Mail>'
# .../gems/mail-2.7.1/lib/mail.rb:3:in `<main>'
# .../bundler/gems/rails-6a0f6c4d70b1/actionmailbox/lib/action_mailbox/mail_ext.rb:3:in `<main>'
# .../bundler/gems/rails-6a0f6c4d70b1/actionmailbox/lib/action_mailbox.rb:3:in `<main>'
# .../bundler/gems/rails-6a0f6c4d70b1/actionmailbox/lib/action_mailbox/engine.rb:9:in `<main>'
# ./config/application.rb:11:in `<top (required)>'
# ./config/environment.rb:2:in `require_relative'
# ./config/environment.rb:2:in `<top (required)>'
# ./spec/rails_helper.rb:4:in `require'
# ./spec/rails_helper.rb:4:in `<top (required)>'

(bootsnap and zeitwerk is excluded from backtrace)

2 Answers 2

146

For Rails 6

Add gem 'net-smtp', require: false to your Gemfile and run bundle.

Similarly I assume you may have problems with net-imap and net-pop and so have to add them until a new mail gem version is released.

gem 'net-smtp', require: false
gem 'net-imap', require: false
gem 'net-pop', require: false

UPD: The mail gem version 2.8.0 was released on December 3rd, 2022 which includes the fix. Just upgrade to >= 2.8.0.

For Rails 7

On January 6th, 2022 Rails 7.0.1 was released:

The focus of this release is bring support to Ruby 3.1

Amongh other Ruby 3.1-related issues it brought a fix for this problem. So upgrade to Rails >= 7.0.1.


Related pull requests and issues:

4
  • 1
    I got this just by upgrading Ruby and Puma and the above fix worked.
    – tschumann
    Feb 19, 2022 at 3:25
  • 1
    I had this problem with the "pony" gem, and this fixed it for me.
    – Julien
    Apr 2, 2022 at 17:41
  • 4
    Upgrading from 6.0.x to rails 6.1.x and had a problem I added all three gems as require false and my production site worked. Thank you!
    – Nezir
    Apr 13, 2022 at 2:51
  • 1
    I had to also include net-ftp
    – abax
    Jan 4 at 21:42
12

I met the same problem when running Rails in production mode. ( Ruby 3, Rails 6.1 )

copy and paste this lines of code to Gemfile:

gem 'net-smtp', require: false
gem 'net-imap', require: false
gem 'net-pop', require: false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.