Introduction
GitHub Pages is a fantastic way to host your personal website from your GitHub repository – it is free, convenient while provides much richness in customizing your own style. In this post I will first guide you how to render your repository into a website. Then I will discuss a bit more on how to use Jekyll to preview posts locally before pushing the changes to GitHub.
:pencil: For complete beginners, I would recommend a training course on GitLab, a great resource to get to know Pages.
Jekyll and GitHub Pages
Jekyll is a built-in engine behind GitHub Pages. It is a popular static site generator written in Ruby, which can transform plain text (e.g. Markdown files) into static websites.
All we need to begin with in our repository are a _config.yml file and an index.md file.
The file structure of the hosting repository would look like:
|--
|-- _config.yml
|-- index.md
|-- _posts/
|- yyyy-mm-dd-<title>.md
The index.md file is the content shown on the homepage.
The _config.yml file (example) describes the Jekyll configuration.
With these being set, we are all good to post our blog.
Getting Started - Initializing GitHub Pages with Jekyll
- First create a GitHub repository and enable Github Pages in Settings following this guide.
- Create an
index.mdfile with the content to be shown on the homepage - Create a
_config.ymlfile with the following content:title: Your awesome title author: GitHub User email: your-email@domain.com description: > # this means to ignore newlines until "show_excerpts:" Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description. show_excerpts: true # set to false to remove excerpts on the homepage # Build settings markdown: kramdown theme: minima # Plugins plugins: - jekyll-feed - Commit the changes to
masterand that’s it. Now we can visit our website!
Note here we use the minima theme, which is one of the default themes supported by GitHub Pages.
We can change the theme in Settings.
However, if we want to use other themes, we may need to install it or use remote_theme.
Let’s say we want to use the minimal-mistakes theme.
Since we just want the changes to be effective on Pages, we will use the remote_theme way.
All that needs to be modified is the _config.yml file:
- Comment out all other themes and add
remote_theme: mmistakes/minimal-mistakes - Add plugin
jekyll-include-cacheto thepluginsarray - Commit changes and refresh the website
Installing Jekyll on Ubuntu
Enabling Jekyll on GitHub is as simple as mentioned above. However, if we want more advanced features, like previewing our post locally and troubleshooting possible failed builds before pushing the changes, we need to install Jekyll on our local machine.
bundler and gem
So how do we install Jekyll?
Jekyll is Ruby based and Ruby uses RubyGems to install and manage packages.
gem is the command-line tool.
A gem is also referred to a Ruby library and gemfile is used to describe gem dependencies for Ruby applications.
Bundler is the most common tool to manage gem dependencies.
We can analogize these concepts with Python as follows. It is not exactly the same, but close:
| Python | Ruby |
|---|---|
pip |
gem |
anaconda |
bundler |
requirements.txt |
gemfile |
Installation
- Installing Ruby
Ubuntu 16.04 should have pre-installed Ruby, but my experience told me it would not be able to compile Ruby extension modules. There will be error messages like"Failed to build gem native extension". Therefore, we need to installruby-full:sudo apt-get install ruby-full - Installing Bundler & Jekyll
At this stage, we know Bundler, or Jekyll is actually agem. Bundler is the gem to manage gems. It is best to avoid installing Ruby gems under root directories. We can set up the installation path under$HOME. First append the following to~/.bashrc:# Install Ruby Gems to ~/gems export GEM_HOME="$HOME/gems" export PATH="$PATH:$HOME/gems/bin"Then run the following in Terminal
source ~/.bashrc gem install bundler jekyll
Setting up Jekyll Locally
Once we have installed Jekyll, we can set up Jekyll configuration in our repository. Clone the above github repository locally (find my git tutorial here). Then
Create Gemfile
- Create a
Gemfilefile under the repository’s root directory with the following content:source "https://rubygems.org” gem 'github-pages', group: :jekyll_plugins - Install
github-pagesgem by running:bundle installGem dependencies will be installed at
~/gems. AGemfile.lockfile will also be generated under the current path.
Installing other gems (if needed)
- Whenever we need to install other necessary gems
- add them to
Gemfilegem 'jekyll-include-cache' # if we use the `minimal-mistakes` theme - execute
bundle
- add them to
Build local Jekyll site
- Build project
bundle exec jekyll serveThe
jekyll servecommand builds the site and run a local web server at http://localhost:4000. Thebundle execprefix says to run commands under the environment set inGemfile. - Preview the local site at http://localhost:4000.
- Press
Ctrl+Cto stop the process.
:pencil: After built, a folder _site/ will be generated.
This is where Jekyll outputs HTML files converted from our posts etc.
Therefore, it should not be staged in git and needs to be added to .gitignore.