This is a straight-forward guide on setting up a simple and beautiful website using Hugo + PaperMod theme and GitHub pages. This is basically what I did to set up this very website!
There are two steps. We first install Hugo and create our website locally. Then we use GitHub pages to publish our website on the Internet. Once this is done, changing our website is as simple as a git commit
! (Admittedly, a git commit can get very complicated at times!🙂)
Let’s begin!
Installing Hugo Locally
-
Follow the first two steps here. This will install Hugo on your local machine and create a website.
- Note: When creating the site, use
hugo new site <name of site> -f yml
to create ayml
config file.
- Note: When creating the site, use
-
From step 3, follow this guide to add the PaperMod theme.
- To update the theme, go to
themes/PaperMod
directory and rungit pull
.
- To update the theme, go to
-
Serving Locally: We have now successfully installed Hugo along with a theme. To see how things look, open the command line at the project location and run
hugo server
. -
To add some content run
hugo new <category>/<file>.md
. This will create a new post in thecontent
folder. You can then edit it.- Change
draft: true
todraft: false
in the header section of your posts to have them published. - We almost always use
category = posts
. Although you can add other categories as well.
- Change
-
Adding math and Latex support: Navigate to the
layouts
directory. There create a new directory namedpartials
and inside it create an html file namedextend_head.html
. Put the following code inside it:<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css"> <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.js"></script> <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/auto-render.min.js" onload="renderMathInElement(document.body);"></script> <script> document.addEventListener("DOMContentLoaded", function() { renderMathInElement(document.body, { delimiters: [ {left: "$$", right: "$$", display: true}, {left: "$", right: "$", display: false} ] }); }); </script>
-
Config file: Inside the main directory there is a
config.yml
file that stores the configuration of the website. For a decent starting point, you can copy my configurations. With a few tweaks, it should work fine for you.- Put your images in the
static/
directory
- Put your images in the
-
To set up the archives and the search page, you should add an
archives.md
and asearch.md
file to yourcontents
directory. They should contain the following codes:--- title: "Archive" layout: "archives" # url: "/archives" summary: "archives" ---
--- title: "Search" # in any language you want layout: "search" # is necessary # url: "/archive" # description: "Description for Search" summary: "search" placeholder: "placeholder text in search input box" ---
-
For adding additional features, such as comments, check out this page.
GitHub Pages
-
In your GitHub account, create a repo with the same name as your username.
-
In the
config.yml
file of your website, changebaseURL
tohttps://YourGitHubUsername.github.io/
. -
From the command line, run the
hugo
command in your main directory. This will create apublic
directory which is what we will publish. -
Go to the newly created
public
directory. There, we will create a repo and connect it with the remote repo that we just created:git init git add . git remote add origin https://github.com/username/username.github.io.git git commit -m "first commit" git push origin master
-
Once the content of
public
directory was pushed to your GitHub, go to your repo’s page on GitHub. Go toSettings -> Pages
. Under the source section, select your master branch and the/(root)
directory and create your website. -
Done! You can see your website at
YourGitHubUsername.github.io
. (This may take a few minutes) -
Issue: After doing all this, my website didn’t display correctly (it was raw html without formatting). I had to disable fingerprinting by adding the following code to the config file:
params: assets: disableFingerprinting: true
This solved the issue.
Updating Your Webpage
Each time you want to update your page you must do so in your local machine, run the hugo
command and commit the changes that are made to the public
directory. Any changes that you make will automatically take effect after you commit them.
So let’s say we want to add a new post. We will
-
hugo new posts/MyNewPost.md
-
Go to the
content/posts
directory and editMyNewPost.md
. (You can also change the meta-data at the top of this file to add tags, etc. Take a look at the source code for this post!) -
hugo cd public git add . git commit -m "Added a new post" git push origin master