You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails中has_secure_password报错:cannot load such file -- bcrypt求助

Fixing the bcrypt LoadError in Ruby on Rails Authentication

Hey there, let's work through this bcrypt LoadError you're facing when setting up simple auth in Rails. I've run into this exact issue before, so here are the steps to get it sorted:

  • Make sure your Gemfile is set up correctly
    Instead of installing bcrypt directly from the git repo, use the official gem entry in your Gemfile. Add this line (if it's not already there):

    gem 'bcrypt'
    

    Then run bundle install in your terminal. Direct git installs can sometimes cause compatibility issues with your Rails version, so using the gem from RubyGems is the safer route.

  • Uninstall any existing git-based bcrypt installations
    If you installed bcrypt from the GitHub repo earlier, it might be conflicting with the official gem. Run this command to remove it:

    gem uninstall bcrypt
    

    When prompted, select the version that came from the git repo (it'll have a git URL listed next to it). Then re-run bundle install to get the correct version.

  • Restart your Rails Console
    The Rails Console caches your gem environment when it starts up. Even if you install bcrypt after opening the console, it won't pick up the new gem until you close the current console and open a new one with rails console.

  • Check for Spring caching issues
    Rails uses Spring to speed up development, but it can sometimes hold onto old gem data. Stop Spring with:

    spring stop
    

    Then open a new Rails Console and try User.all again.

  • Verify system dependencies (if all else fails)
    Bcrypt requires system-level compilation tools to install properly. If you're on macOS, make sure you have Xcode Command Line Tools installed (run xcode-select --install). On Linux, install build essentials (for Ubuntu/Debian: sudo apt-get install build-essential). After installing these, run bundle install again.

Once you've gone through these steps, your has_secure_password should work without the LoadError, and User.all should run smoothly.

内容的提问来源于stack exchange,提问作者Rafael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:31:56