GitHub Pages Locally On Windows

[ blog  ]
Written on January 3, 2023

My blog runs on GitHub Pages, which uses Jekyll. Usually I just push new posts to GitHub and check them once they’re active on the site, but sometimes it’s nice to be able to preview things locally.

Install Jekyll

Instructions for installing Jekyll on Windows are available here, but I wanted to see if I could simplify it a bit using winget - my new goto for installing things on Windows.

I’m running on Windows 11, using PowerShell.

winget install -e --id RubyInstallerTeam.RubyWithDevKit.3.1

Note that you must restart the shell after installing so binaries installed can be found in the path.

Next step is to run this:

ridk install

I got the following error:

ridk : File C:\Ruby31-x64\bin\ridk.ps1 cannot be loaded because running scripts is disabled on this system. For more in
formation, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ ridk install
+ ~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

To get around this I set the execution policy for PowerShell scripts to unrestricted:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process

Setting the execution policy to unrestricted may seem risky, but it’s only for the current process - it’s not changing any setting permanently.

Trying this again I got further:

ridk install
 _____       _           _____           _        _ _         ___
|  __ \     | |         |_   _|         | |      | | |       |__ \
| |__) |   _| |__  _   _  | |  _ __  ___| |_ __ _| | | ___ _ __ ) |
|  _  / | | | '_ \| | | | | | | '_ \/ __| __/ _` | | |/ _ \ '__/ /
| | \ \ |_| | |_) | |_| |_| |_| | | \__ \ || (_| | | |  __/ | / /_
|_|  \_\__,_|_.__/ \__, |_____|_| |_|___/\__\__,_|_|_|\___|_||____|
                    __/ |           _
                   |___/          _|_ _  __   | | o __  _| _     _
                                   | (_) |    |^| | | |(_|(_)\^/_>

   1 - MSYS2 base installation
   2 - MSYS2 system update (optional)
   3 - MSYS2 and MINGW development toolchain

Which components shall be installed? If unsure press ENTER [1,3]

I selected 3, as per the instructions from the original Jekyll installation instructions.

Next is to install Jekyll itself (so far I’ve just installed Ruby).

gem install jekyll bundler

To check if Jekyll has been installed properly I can run this:

jekyll -v

Previewing locally

I found I needed to add a Gemfile to the project:

source "https://rubygems.org"
gem 'github-pages'

Run bundle install:

bundle install

I also needed to add webrick:

bundle add webrick

Then start serving:

bundle exec jekyll serve

It tells you where to point your browser:

PS C:\snorristurluson.github.io> bundle exec jekyll serve
Configuration file: C:/snorristurluson.github.io/_config.yml
            Source: C:/snorristurluson.github.io
       Destination: C:/snorristurluson.github.io/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
       Jekyll Feed: Generating feed for posts
                    done in 0.553 seconds.
 Auto-regeneration: enabled for 'C:/snorristurluson.github.io'
    Server address: http://127.0.0.1:4000/
  Server running... press ctrl-c to stop.

And what do you know, it works:

Screenshot

Note that I did find I had to add a layout directive to the top of a post:

---
layout: post
title: GitHub Pages Locally On Windows
tags: blog
---

I haven’t needed that so far - it seems that when GitHub generates the pages it uses the correct layout by default.