[Ruby] Resolve LoadError (cannot load such file –bcrypt) that occurs in rails test (Rails tutorial Chapter 6)

2 minute read

Keep track of any errors that occur as you progress through the rails tutorial.
The hardware uses MacBook Air, and the development environment uses VScode.

Rails Tutorial Chapter 6 6.3 Adding a Secure Password

LoadError (cannot load such file –bcrypt) Synopsis until the error occurs

Generate a migration file called add_password_digest_to_users and perform the migration on the database.

$ rails generate migration add_password_digest_to_users password_digest:string
$ rails db:migrate

Write bcrypt in the Gemfile and run $ bundle install.

Gemfile


gem 'rails',          '5.1.6'
gem 'bcrypt',         '3.1.12'
$ bundle install

Next, add has_secure_password in the User model.

app/models/user.rb


class User < ApplicationRecord
  ~abridgement~
  has_secure_password
end

here

$ rails test

When you run, the validation added by has_secure_password should cause an error.

However, I got the following error:

$ rails test
LoadError: cannot load such file -- bcrypt
~abridgement~

As a test, when I run rails console and execute User.new, …

$ rails console

Running via Spring preloader in process 18909
Loading development environment (Rails 5.1.7)

WARNING: This version of ruby is included in macOS for compatibility with legacy software. 
In future versions of macOS the ruby runtime will not be available by 
default, and may require you to install an additional package.

>User.new
You don't have bcrypt installed in your application. Please add it to your Gemfile and run bundle install
Traceback (most recent call last):
        3: from (irb):1
        2: from app/models/user.rb:1:in `<top (required)>'
        1: from app/models/user.rb:10:in `<class:User>'
LoadError (cannot load such file -- bcrypt)

The bcrypt that should have been installed is not loaded at all … (crying)

[Rails] “cannot load such file –bcrypt_ext” error support when installing bcrypt [Windows]

If this solution seems to be the best, but it doesn’t solve it at all …, try the following solution.

Solution Update Ruby

First,

$ rbenv versions

Check the ruby version with.
Then uninstall that ruby simasu

$rbenv uninstall Confirmed version

Then specify the new version and install it. (Here 2.6.5)

$ rbenv install 2.6.5

After the installation is complete, do the following:

$ rbenv global 2.6.5
$ rbenv rehash

This should change the version of ruby. (You can check with $ ruby -v)

Reference: How to use and how rbenv works

Finally, change / check the contents of the Gemfile.

source 'https://rubygems.org'

ruby '2.6.5'← Addendum
gem 'bcrypt'← You do not have to specify the version (bcrypt)-I don't need ruby or macOS)

▲ Here, specify the version of ruby and describe so that the latest version of bcrypt can be installed without specifying the version.

$ gem list bcrypt
And if bcrypt is included,
$ gem uninstall bcrypt)
To delete it.

Do bundle install to install bcrypt.

$ bundle install

Then, when you run $ rails console, WARNING will be resolved and ** the error will be resolved. ** **


If it helps ** I would be grateful if you could click the LGTM button. ** **
Let’s do our best to learn Rails together! : raised_hand_tone1: