Windows环境下Ruby on Rails项目安装mimemagic 0.3.10找不到MIME类型数据库报错
Windows环境Ruby on Rails项目安装mimemagic 0.3.10报错解决方案
我在Windows环境的Ruby on Rails项目中安装mimemagic 0.3.10时遇到如下错误,相关错误栈信息如下:
Fetching mimemagic 0.3.10 Installing mimemagic 0.3.10 with native extensions Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: C:/Ruby26/lib/ruby/gems/2.6.0/gems/mimemagic-0.3.10/ext/mimemagic C:/Ruby26/bin/ruby.exe -rrubygems C:/Ruby26/lib/ruby/gems/2.6.0/gems/rake-13.0.6/exe/rake RUBYARCHDIR\=C:/Ruby26/lib/ruby/gems/2.6.0/extensions/x86-mingw32/2.6.0/mimemagic-0.3.10 RUBYLIBDIR\=C:/Ruby26/lib/ruby/gems/2.6.0/extensions/x86-mingw32/2.6.0/mimemagic-0.3.10 rake aborted! Could not find MIME type database in the following locations: ["/usr/local/share/mime/packages/freedesktop.org.xml", "/opt/homebrew/share/mime/packages/freedesktop.org.xml", "/opt/local/share/mime/packages/freedesktop.org.xml", "/usr/share/mime/packages/freedesktop.org.xml"] Ensure you have either installed the shared-mime-info package for your distribution, or obtain a version of freedesktop.org.xml and set FREEDESKTOP_MIME_TYPES_PATH to the location of that file. This gem might be installed as a dependency of some bigger package, such as rails, activestorage, axlsx or cucumber. While most of these packages use the functionality of this gem, some gems have included this gem by accident. Set USE_FREEDESKTOP_PLACEHOLDER=true if you are certain that you do not need this gem, and wish to skip the inclusion of freedesktop.org.xml.
我已找到该问题的对应解决方案,在此分享给所有可能遇到相同问题的开发者。
可用解决方案
方案1:手动配置MIME类型数据库路径(功能完整)
报错的核心原因是Windows系统默认没有安装freedesktop的shared-mime-info库,按以下步骤操作即可:
- 准备好
freedesktop.org.xml文件,保存到本地任意无中文无空格的路径下,例如C:\shared-mime\freedesktop.org.xml - 打开Windows系统环境变量设置,新建系统变量,变量名为
FREEDESKTOP_MIME_TYPES_PATH,变量值填写刚才xml文件的完整路径 - 重启你正在使用的CMD/PowerShell/Ruby终端,重新执行
bundle install即可正常安装
方案2:不需要mimemagic功能时快速跳过
如果你确认项目不需要用到MIME类型识别的相关功能,可以临时设置环境变量跳过检查:
- CMD终端执行:
set USE_FREEDESKTOP_PLACEHOLDER=true - PowerShell终端执行:
$env:USE_FREEDESKTOP_PLACEHOLDER = "true" - 执行完上述命令后再运行
bundle install即可完成安装
方案3:升级gem版本规避问题
该安装问题是mimemagic 0.3.10版本的已知问题,你可以直接升级gem版本修复:
- 修改项目Gemfile,将mimemagic的版本指定为修复后的版本:
gem 'mimemagic', '~> 0.4.3' - 执行
bundle update mimemagic即可直接安装无问题的版本
内容的提问来源于stack exchange,提问作者flyingfishcattle
相关产品推荐
相关产品推荐

