|
| 1 | +# Running Jekyll locally |
| 2 | + |
| 3 | +Follow the [GitHub: Testing your GitHub Pages site locally with |
| 4 | +Jekyll](https://docs.github.com/en/pages/setting-up-a-github-pages-site-with-jekyll/testing-your-github-pages-site-locally-with-jekyll) instructions |
| 5 | + |
| 6 | +Two possible approaches: Either install `ruby` (>= 2.7) and |
| 7 | +[jekyll](https://jekyllrb.com/) locally or use a Docker image with a |
| 8 | +jekyll installation. The local ruby/jekyll is more flexible if you can |
| 9 | +get it to work. |
| 10 | + |
| 11 | +## Local jekyll installation |
| 12 | + |
| 13 | +Basically follow [GitHub: Testing your GitHub Pages site locally with |
| 14 | +Jekyll](https://docs.github.com/en/pages/setting-up-a-github-pages-site-with-jekyll/testing-your-github-pages-site-locally-with-jekyll) |
| 15 | +and in particular [Jekyll's installation |
| 16 | +instructions](https://jekyllrb.com/docs/installation/). |
| 17 | + |
| 18 | + |
| 19 | +### macOS |
| 20 | + |
| 21 | +Do not use the system ruby (i.e., not `/usr/bin/ruby`) but install |
| 22 | +your own version. The [Jekyll macOS |
| 23 | +instructions](https://jekyllrb.com/docs/installation/macos/) use |
| 24 | +[homebrew](https://brew.sh/) but this works equally well with |
| 25 | +[macports](https://www.macports.org/). |
| 26 | + |
| 27 | +#### Overview |
| 28 | + |
| 29 | +1. Use `chruby` and `ruby-install` to manage different ruby versions. |
| 30 | +2. Build ruby 3.4.1 (this was the recommended version in the Jekyll |
| 31 | + instructions at the time) |
| 32 | +3. Install all necessary gems from [Gemfile][] with `bundle`. |
| 33 | + |
| 34 | + |
| 35 | +#### ruby installation with macports |
| 36 | + |
| 37 | +Install `chruby` and `ruby-install`: On macos with macports |
| 38 | + |
| 39 | + sudo port install chruby ruby-install |
| 40 | + |
| 41 | +Update `~/.bash_profile` (or your `~/.zshrc`) with |
| 42 | + |
| 43 | + # chruby (macports) |
| 44 | + source /opt/local/share/chruby/chruby.sh |
| 45 | + |
| 46 | + # To enable auto-switching of Rubies specified by .ruby-version files, add the |
| 47 | + # following to ~/.bash_profile or ~/.zshrc: |
| 48 | + source /opt/local/share/chruby/auto.sh |
| 49 | + |
| 50 | +Install ruby (compile from source, this requires Xcode and development |
| 51 | +tools on macOS and will take a while): |
| 52 | + |
| 53 | + ruby-install ruby 3.4.1 |
| 54 | + |
| 55 | +Switch to using it (can also be added to `~/.bash_profile`) |
| 56 | + |
| 57 | + chruby ruby-3.4.1 |
| 58 | + |
| 59 | +To always activate this version, add a `.ruby-version` file in the |
| 60 | +directory with the `Gemfile` (see [How to install different versions |
| 61 | +of ruby and automatically switch between them](https://www.moncefbelyamani.com/how-to-install-xcode-homebrew-git-rvm-ruby-on-mac/#how-to-install-different-versions-of-ruby-and-switch-between-them) |
| 62 | +-- also needs chruby to be active): |
| 63 | + |
| 64 | + echo "3.4.1" > .ruby-version |
| 65 | + |
| 66 | +(May need to open a new terminal to let `.bash_profile` changes to take effect). |
| 67 | + |
| 68 | + |
| 69 | +### All OS: install gems (including jekyll) |
| 70 | + |
| 71 | +Ensure that the correct version of ruby is use (`ruby --version`) and |
| 72 | +then |
| 73 | + |
| 74 | + bundle install |
| 75 | + |
| 76 | +should download and build the appropriate gems. |
| 77 | + |
| 78 | +The `Gemfile.lock` should show something like |
| 79 | +```ruby |
| 80 | +GEM |
| 81 | + remote: https://rubygems.org/ |
| 82 | + specs: |
| 83 | + ... |
| 84 | + github-pages (232) |
| 85 | + github-pages-health-check (= 1.18.2) |
| 86 | + jekyll (= 3.10.0) |
| 87 | + ... |
| 88 | +``` |
| 89 | + |
| 90 | +### Run jekyll locally |
| 91 | + |
| 92 | +Start jekyll and have it serve the site at http://127.0.0.1:4000 |
| 93 | +```bash |
| 94 | +rm -r _site |
| 95 | +bundle exec jekyll serve --watch --livereload --future --incremental |
| 96 | +``` |
| 97 | + |
| 98 | +(See the [serve commandline |
| 99 | +options](https://jekyllrb.com/docs/configuration/options/#serve-command-options) |
| 100 | +for more details.) |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +## Docker image |
| 105 | + |
| 106 | +### macOS docker installation |
| 107 | + |
| 108 | +- install Docker Desktop https://docs.docker.com/desktop/install/mac-install/ |
| 109 | +- start docker |
| 110 | +- follow the solution from https://stackoverflow.com/a/58850151/ as |
| 111 | + described next |
| 112 | + |
| 113 | + |
| 114 | +### Build the site |
| 115 | + |
| 116 | +To build the static site, run `jekyll build` inside docker: |
| 117 | +```bash |
| 118 | +export JEKYLL_VERSION=4.0 |
| 119 | +docker run --rm \ |
| 120 | + --volume="$PWD:/srv/jekyll" \ |
| 121 | + -it jekyll/jekyll:$JEKYLL_VERSION \ |
| 122 | + jekyll build |
| 123 | +``` |
| 124 | +The static site files are stored in the `_site` directory. |
| 125 | + |
| 126 | +### Serve the static site |
| 127 | + |
| 128 | +Then you must *serve* the site: |
| 129 | +```bash |
| 130 | +cd _site |
| 131 | +python -m http.serve |
| 132 | +``` |
| 133 | + |
| 134 | +Point your browser to http://localhost:8000 |
| 135 | + |
| 136 | +When you make changes, you need to re-build the site. |
| 137 | + |
| 138 | + |
| 139 | + |
0 commit comments