Windows安装Ruby和Jekyll

国内安装jekyll构建博客系统

Posted by Procon on September 19, 2022

Windows安装Ruby和Jekyll

安装包下载和安装 打开 https://rubyinstaller.org/downloads/ 选择WITH DEVKIT下面的安装包

  • 推荐使用最新版本

  • 如果电脑是 32位系统选择 x86 的包,64位系统选择 x64

  • Ruby + Devkit 是集成了 gem 的安装

现在完后打开安装包,除了第一个界面选择同意条款外,后面都直接都是用默认选项,点下一步。

安装完后会引导运行 ridk install,默认是勾选上的,点击完成自动运行 CMD终端

在 CMD 终端界面输入3 并按 Enter 回车键,选择安装 ` 3 - MSYS2 and MINGW development toolchain`

执行完后会再次提示,为保证万无一失,再次输入3 并按 Enter 回车键。再次提示按 Enter 回车键会自动关闭 cmd终端。

在开始菜单点击Start Command Prompt with Ruby 打开并使用Ruby。 这一步不能少。

  • 检查安装是否成功,使用查看版本命令如果有返回版本号就是安装好了
  • 查看 Ruby 版本:ruby -v
  • 查看 gem 版本:gem -v
  • 然后 替换源镜像,再更新 Ruby gem

修改 gem 源为国内镜像

默认的 gem 源安装可能会特别慢,几个小时都没反应,所以先替换 gem 源

gem sources --remove https://rubygems.org/
gem sources -a https://gems.ruby-china.com/

替换 gem 源后把 bundle 源也换一下

bundle config mirror.https://rubygems.org https://gems.ruby-china.com

替换后检查是否替换成功,如果返回的只有 gems.ruby-china.com 表示替换成功

gem sources -l

更新 Ruby gem

gem update

安装 Jekyll 组件

1
gem install jekyll bundler

查看 jekyll 当前版本

1
jekyll -v

推荐环境安装后执行下面命令避免一些报错 安装 webrick

1
bundle add webrick

全新安装

  • 新建项目:jekyll new site1px
    • site1px 是项目名可随意修改
  • 进入项目文件夹:cd site1px
    • site1px 是项目名称,要和之前保持一致
  • 运行服务:bundle exec jekyll serve
    • 如果提示Run bundle install to install missing gems.,执行下bundle install

git 旧项目直接使用

先安装缺失的 gems

1
bundle install

如果执行 bundle install 太慢了,可能是bundle源没有改成国内的,可以先终止,再执行下面命令后重新执行bundle install

1
bundle config mirror.https://rubygems.org https://gems.ruby-china.com

如果只使用 gem source --add --remove修改 gem 镜像,对 bundle install 并不起作用

安装完后,运行服务

1
bundle exec jekyll serve

常用命令 运行服务 bundle exec jekyll serve

生成静态文件 文件会生成到项目目录下的 _site 文件夹中

bundle exec jekyll build -d _site

常见报错解决方案 `require’: cannot load such file – webrick (LoadError) 运行服务时,出现以下类似报错:

1
2
3
4
<internal:C:/Ruby32-x64/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require': cannot load such file -- webrick (LoadError)
from <internal:C:/Ruby32-x64/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
from C:/Users/ll/.local/share/gem/ruby/3.2.0/gems/jekyll-4.2.2/lib/jekyll/commands/serve/servlet.rb:3:in `<top (required)>'
from C:/Users/ll/.local/share/gem/ruby/3.2.0/gems/jekyll-4.2.2/lib/jekyll/commands/serve.rb:179:in `require_relative'

因为未安装 webrick,执行以下命令安装

1
bundle add webrick