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

Ubuntu 16.04下gem安装bcrypt遇gcc错误求助

问题描述

我在Ubuntu 16.04系统中尝试执行gem install bcrypt、gem install bcrypt-ruby命令均失败,报错信息一致,导致无法在Ruby on Rails中完成bundle install。具体报错内容如下:

sylviachoong@sylviachoong:~/Desktop/postco$ gem install bcrypt-ruby
Building native extensions. This could take a while...
ERROR: Error installing bcrypt-ruby:
	ERROR: Failed to build gem native extension.

current directory: /home/sylviachoong/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bcrypt-3.1.12/ext/mri
/home/sylviachoong/.rbenv/versions/2.5.0/bin/ruby -r ./siteconf20180526-3491-4idkgq.rb extconf.rb
creating Makefile

current directory: /home/sylviachoong/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bcrypt-3.1.12/ext/mri
make "DESTDIR=" clean

current directory: /home/sylviachoong/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bcrypt-3.1.12/ext/mri
make "DESTDIR="
compiling bcrypt_ext.c
gcc: 错误:无法识别的命令行选项'-Wmisleading-indentation'
gcc: 错误:无法识别的命令行选项'-Wduplicated-cond'
Makefile:241: recipe for target 'bcrypt_ext.o' failed
make: *** [bcrypt_ext.o] Error 1

make failed, exit code 2

Gem files will remain installed in /home/sylviachoong/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bcrypt-3.1.12 for inspection.
Results logged to /home/sylviachoong/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/bcrypt-3.1.12/gem_make.out
解决方案

这个问题的核心原因很明确:Ubuntu 16.04默认自带的gcc版本是5.x,而Ruby 2.5在编译原生扩展时会添加两个gcc 6及以上版本才支持的警告选项(-Wmisleading-indentation和-Wduplicated-cond),两者不兼容导致编译失败。下面给你几个可行的解决办法:

方法一:升级gcc到6+版本(推荐)

这是最彻底的解决方案,后续其他需要编译原生扩展的gem也不会再遇到类似问题:

  1. 先更新软件源并安装gcc-6:
    sudo apt-get update
    sudo apt-get install gcc-6 g++-6
    
  2. 将gcc-6设置为系统默认编译器:
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6
    
  3. 验证gcc版本是否切换成功:
    gcc --version
    
    看到输出显示gcc 6.x版本后,重新执行你的安装命令即可:
    gem install bcrypt-ruby
    # 或者直接运行bundle install
    bundle install
    

方法二:临时禁用Ruby的额外编译警告

如果你不想升级gcc,可以通过环境变量关闭这两个不兼容的警告选项,临时绕过问题:

  • 直接安装gem时:
    gem install bcrypt-ruby -- --with-cflags="-Wno-misleading-indentation -Wno-duplicated-cond"
    
  • 使用bundle安装时,先设置环境变量再执行:
    export CFLAGS="-Wno-misleading-indentation -Wno-duplicated-cond"
    bundle install
    

方法三:降级Ruby版本(适合项目允许的情况)

Ruby 2.4及更早的版本不会添加这两个gcc 6专属的编译选项,如果你的项目不需要Ruby 2.5的特性,可以用rbenv降级到2.4.x版本:

  1. 安装Ruby 2.4.10(稳定版):
    rbenv install 2.4.10
    
  2. 设置当前目录使用这个Ruby版本:
    rbenv local 2.4.10
    
  3. 重新安装gem或执行bundle:
    gem install bcrypt-ruby
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:58:37