1. 问题描述
安装 jekyll 时,执行完 gem install jekyll
命令后报错:
1
2
3
Fetching: public_suffix-4.0.6.gem (100%)
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.
这是因为 macOS 不允许您对 Mac 自带的 Ruby 版本进行任何更改。但是,像 bundler
这种通过使用独立版本的 Ruby(该版本不会干扰 Apple 提供的版本)来安装 gem 是可行的。
注意:强烈建议不要使用 sudo
命令来安装 gems
或更改系统文件和目录的权限,即使您知道自己在做什么。
2. 解决方案
如果您确定您不需要同时使用多个 Ruby 版本(除了 macOS 附带的版本),可以按照以下方法:
首先,重新单独安装一个 Ruby 版本:
1
brew install ruby
检查当前使用的 Ruby 路径:
1
which ruby
结果:
1
/usr/local/opt/ruby/bin/ruby
检查当前使用的 Ruby 版本:
1
ruby -v
结果:
1
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-darwin18]
然后,更新 shell 配置文件中的 PATH 路径:
1
echo 'export PATH="/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/3.0.0/bin:$PATH"' >> ~/.zshrc
注意:上面的 3.0.0
与当前 Ruby 版本有关,如果您安装的 Ruby 是其他版本,请将这里的 3.0
替换为您的 Ruby 版本的前两位。
刷新 shell 配置文件,使其生效:
1
source ~/.zshrc
注意:如果您使用的是 bash shell,请将上面的 .zshrc
替换为 .bash_profile
。
安装 jekyll
和 jekyll bundler
:
1
2
gem install jekyll
gem install jekyll bundler
安装完成后,切换目录到本地 github pages 项目所在文件夹,然后尝试在本地启动 Jekyll 服务器:
1
2
cd /Users/andy/Documents/GitHub/YEY11.github.io
jekyll s
报错:
1
2
3
4
5
6
7
Bundler::GemNotFound: Could not find rake-10.3.2 in any of the sources
~/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.6.2/lib/bundler/spec_set.rb:92:in `block in materialize'
~/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.6.2/lib/bundler/spec_set.rb:85:in `map!'
~/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.6.2/lib/bundler/spec_set.rb:85:in `materialize'
~/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.6.2/lib/bundler/definition.rb:133:in `specs'
~/.rvm/gems/ruby-2.0.0-p451/gems/bundler-1.6.2/lib/bundler/definition.rb:178:in `specs_for'
Show 28 more lines
尝试修复,首先运行以下命令,修复捆绑程序路径配置:
1
bundle config set path 'vendor/cache'
然后尝试运行以下命令:
1
bundle exec jekyll serve
按照提示要求,运行以下命令,安装缺失的 gem(这里是 rake
):
1
bundle install
更新 bundle,确保 Gemfile.lock
引用的是最新的 gem:
1
bundle update
再次尝试在本地启动 Jekyll 服务器:
1
jekyll s
仍然报错:
1
Jekyll 4.2.0 Please append `--trace` to the `serve` command for any additional information or backtrace.
这是因为 webrick
不再是 Ruby 3.0 中的捆绑 gem,我们需要手动添加 webrick
到 Gemfile 中作为依赖:
1
bundle add webrick
再次尝试在本地启动 Jekyll 服务器:
1
jekyll s
解决,服务器可以正常启动:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/usr/local/lib/ruby/gems/3.0.0/gems/bundler-1.17.2/lib/bundler/shared_helpers.rb:29: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/lib/ruby/gems/3.0.0/gems/bundler-1.17.2/lib/bundler/shared_helpers.rb:118: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/lib/ruby/gems/3.0.0/gems/bundler-1.17.2/lib/bundler/shared_helpers.rb:118: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/lib/ruby/gems/3.0.0/gems/bundler-1.17.2/lib/bundler/shared_helpers.rb:35: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/lib/ruby/gems/3.0.0/gems/bundler-1.17.2/lib/bundler/shared_helpers.rb:35: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/lib/ruby/gems/3.0.0/gems/bundler-1.17.2/lib/bundler/shared_helpers.rb:44: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/lib/ruby/gems/3.0.0/gems/bundler-1.17.2/lib/bundler/shared_helpers.rb:118: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
Configuration file: /Users/andy/Documents/GitHub/YEY11.github.io/_config.yml
Source: /Users/andy/Documents/GitHub/YEY11.github.io
Destination: /Users/andy/Documents/GitHub/YEY11.github.io/_site
Incremental build: disabled. Enable with --incremental
Generating...
done in 6.922 seconds.
Auto-regeneration: enabled for '/Users/andy/Documents/GitHub/YEY11.github.io'
Server address: http://127.0.0.1:4000/
Server running... press ctrl-c to stop.
参考资料:
- https://github.com/jekyll/jekyll/issues/6892
- https://stackoverflow.com/questions/51664716/you-dont-have-write-permissions-for-the-library-ruby-gems-2-3-0-directory/54874320
- https://stackoverflow.com/questions/51126403/you-dont-have-write-permissions-for-the-library-ruby-gems-2-3-0-directory-ma/54873916#54873916
- https://stackoverflow.com/questions/56911682/bundlergemnotfound-could-not-find-rake-12-3-2-in-any-of-the-sources
- https://stackoverflow.com/questions/23801899/bundlergemnotfound-could-not-find-rake-10-3-2-in-any-of-the-sources
- https://talk.jekyllrb.com/t/jekyll-4-2-0-please-append-trace-to-the-serve-command-error/5661
- https://github.com/jekyll/jekyll/issues/8523
- https://ruby-china.org/topics/26655
- https://github.com/rapid7/metasploit-framework/issues/14666
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。 欢迎转载,并请注明来自:YEY 的博客 同时保持文章内容的完整和以上声明信息!