Set up Jekyll on AlmaLinux 8: Free Static Site Generator

Posted on

Set up Jekyll on AlmaLinux 8: Free Static Site Generator

Set up Jekyll on AlmaLinux 8: Free Static Site Generator

This tutorial guides you through the process of setting up Jekyll on AlmaLinux 8. Jekyll is a powerful, free, and open-source static site generator. It simplifies website creation by allowing you to build dynamic, navigable sites similar to content management systems (CMS) like Drupal and WordPress. However, unlike those CMS platforms that generate content dynamically upon each page request, Jekyll on AlmaLinux 8 pre-renders all content at build time, resulting in faster load times and improved security. Let’s explore how to install and configure Jekyll on AlmaLinux 8.

To effectively follow this guide, you’ll need a server running AlmaLinux 8. Ensure you’re logged in as a non-root user with sudo privileges and that a basic firewall is configured. If you haven’t already done so, refer to our guide on Initial Server Setup with AlmaLinux 8 to get your server ready.

Now, let’s dive into the steps required to complete your Jekyll installation on AlmaLinux 8.

1. Install Ruby on AlmaLinux 8

Jekyll is built using Ruby, so the first step is to install Ruby on your AlmaLinux server.

Begin by updating your system’s package index:

sudo dnf update -y

Next, install the necessary dependencies and required packages for Ruby:

# sudo dnf install git make gcc curl openssl-devel zlib-devel libffi-devel readline-devel sqlite-devel -y
# sudo dnf group install "Development Tools" -y

Now, install Ruby using the DNF package manager:

# sudo dnf module reset ruby -y
# sudo dnf install @ruby:3.1 -y

Confirm the Ruby installation by checking its version:

ruby -v
**Output**
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]

Finally, install the ruby-devel package:

sudo dnf install ruby-devel -y

2. Jekyll Configuration on AlmaLinux 8

With Ruby installed, the next step is to configure the Ruby Gems path and then install Jekyll itself.

Configure Ruby Gems Path

To configure the Ruby Gems path, open your .bashrc file using your preferred text editor (e.g., vi):

vi ~/.bashrc

Add the following lines to the end of the file:

# Install Ruby Gems to ~/.gems
export GEM_HOME=$HOME/gems
export PATH=$HOME/gems/bin:$PATH

Save the changes and close the file.

Apply the changes to your current shell session by sourcing the .bashrc file:

source ~/.bashrc

Install Jekyll Site Generator on AlmaLinux 8

Now, use the gem command to install Jekyll and Bundler:

gem install jekyll bundler
**Output**
Successfully installed bundler-2.4.10
Parsing documentation for bundler-2.4.10
Installing ri documentation for bundler-2.4.10
Done installing documentation for bundler after 0 seconds
31 gems installed

Keep your Ruby gems up to date by running:

gem update --system

3. Create a Demo Site with Jekyll

Let’s create a new demo site to verify the Jekyll installation.

Navigate to your home directory and create a new Jekyll site:

# cd ~
# jekyll new demo-site

You can inspect the files and directories created within the demo-site directory using the tree command (if tree is not installed, use sudo dnf install tree -y):

# sudo dnf install tree -y
# tree demo-site
**Output**
demo-site
├── 404.html
├── about.markdown
├── _config.yml
├── Gemfile
├── Gemfile.lock
├── index.markdown
└── _posts
    └── 2023-04-04-welcome-to-jekyll.markdown

1 directory, 7 files

Run Web Server with Jekyll

Jekyll can now monitor the files in the created directory and automatically generate a static site. It also regenerates the site whenever changes are made.

To start the built-in web server, use these commands:

# cd ~/demo-site
# bundle add webrick
# jekyll serve --host=your-host-ip-address

Note: If no host is specified, Jekyll defaults to serving the site on localhost. Replace your-host-ip-address with the actual IP address you want Jekyll to bind to.

**Output**
Auto-regeneration: enabled for '/root/demo-site'
    Server address: http://ip-address:4000/
  Server running... press ctrl-c to stop.

This command uses the configurations and content in the _site directory to serve the website.

tree ~/demo-site
**Output**
/root/demo-site
├── 404.html
├── about.markdown
├── _config.yml
├── Gemfile
├── Gemfile.lock
├── index.markdown
├── _posts
│   └── 2023-04-04-welcome-to-jekyll.markdown
└── _site
    ├── 404.html
    ├── about
    │   └── index.html
    ├── assets
    │   ├── main.css
    │   ├── main.css.map
    │   └── minima-social-icons.svg
    ├── feed.xml
    ├── index.html
    └── jekyll
        └── update
            └── 2023
                └── 04
                    └── 04
                        └── welcome-to-jekyll.html

9 directories, 15 files

To run the Jekyll server in the background, use:

jekyll serve --host=host-ip > /dev/null 2>&1 &

Access Jekyll Site on AlmaLinux 8

You can now access your new Jekyll site by opening the following URL in your web browser:

http://IP_Address:4000

You should see the default Jekyll welcome page.

[Image of Jekyll Welcome Page]

Now, let’s add a new post to your Jekyll site.

Create a Sample Post on Jekyll

Posts are stored in the _posts directory using the following naming convention:

YEAR-MONTH-DAY-title.MARKUP

Where:

  • YEAR is the year of the post.
  • MONTH is the month of the post.
  • DAY is the day of the post.
  • title is the title of the post.
  • MARKUP is the file extension (e.g., .md for Markdown).

For example, to create a "hello world" post, use the following command:

vi ~/demo-site/_posts/2023-01-07-hello-world-blog.md

Add the following content to the file:

---
layout: post
title:  "Hello World!"
---

# Welcome

**Hello world**, this is my first Jekyll blog post.

I hope you like it!@OrcaCoreTeam

Save the file and close it.

Refresh your Jekyll site page, and you should see your new post listed:

[Image of Jekyll Post]

Click on the post to view its content:

[Image of Jekyll Post Content]

Conclusion

You have successfully installed and configured the Jekyll static site generator on AlmaLinux 8. You also learned to create a basic blog post.

Hopefully, you enjoyed it. You may also like these articles:

Alternative Solutions for Setting up Jekyll on AlmaLinux 8

While the guide above provides a solid method for setting up Jekyll using Ruby and Gems directly, here are two alternative approaches that offer different advantages:

1. Using Docker:

Docker provides a containerized environment, ensuring consistent behavior across different systems. This eliminates potential dependency conflicts and simplifies deployment.

  • Explanation: Instead of installing Ruby and Jekyll directly on your AlmaLinux 8 system, you can use a pre-built Docker image containing everything needed to run Jekyll. This isolates Jekyll and its dependencies, preventing interference with other software on your server.

  • Steps:

    1. Install Docker: Follow the official Docker documentation to install Docker and Docker Compose on your AlmaLinux 8 server.

    2. Create a docker-compose.yml file: In your project directory, create a docker-compose.yml file with the following content:

    version: "3.9"
    services:
      jekyll:
        image: jekyll/jekyll:latest
        command: jekyll serve --watch --host 0.0.0.0
        ports:
          - "4000:4000"
        volumes:
          - .:/srv/jekyll
    • image: jekyll/jekyll:latest: Specifies the official Jekyll Docker image.
    • command: jekyll serve --watch --host 0.0.0.0: Runs the Jekyll server, watches for changes, and makes the server accessible from outside the container.
    • ports: - "4000:4000": Maps port 4000 on your host machine to port 4000 inside the container.
    • volumes: - .:/srv/jekyll: Mounts your project directory into the container, allowing Jekyll to access your site files.
    1. Run Docker Compose: Navigate to your project directory in the terminal and run:
    docker-compose up

    This will download the Jekyll image (if it’s not already present), build the container, and start the Jekyll server.

    1. Access your site: Open your web browser and navigate to http://your-server-ip:4000.
  • Advantages:

    • Isolation: Prevents dependency conflicts.
    • Consistency: Ensures the same environment across different systems.
    • Easy Deployment: Simplifies deploying your Jekyll site to different environments.

2. Using rbenv or RVM (Ruby Version Manager):

Managing multiple Ruby versions and gemsets can become complex, especially when working on different projects with varying requirements. rbenv and RVM are Ruby version managers that allow you to easily switch between Ruby versions and create isolated gem environments for each project.

  • Explanation: These tools help you manage Ruby versions and gemsets, preventing conflicts and ensuring each project uses the correct dependencies.

  • Steps (using rbenv as an example):

    1. Install rbenv: Follow the instructions on the rbenv GitHub page (https://github.com/rbenv/rbenv) to install rbenv. This typically involves downloading the rbenv installer script and adding some lines to your .bashrc file.

    2. Install a Ruby version:

    rbenv install 3.1.2 # Or your desired Ruby version
    1. Set the global Ruby version:
    rbenv global 3.1.2
    1. Create a .ruby-version file: In your project directory, create a file named .ruby-version containing the Ruby version you want to use (e.g., 3.1.2).

    2. Install Jekyll and Bundler (within the rbenv environment): Navigate to your project directory and run:

    gem install jekyll bundler
    1. Run Jekyll:
    bundle exec jekyll serve --host your-host-ip-address
  • Advantages:

    • Ruby Version Management: Allows you to easily switch between different Ruby versions.
    • Gemset Isolation: Prevents gem conflicts between projects.
    • Cleaner System: Keeps your system Ruby installation clean and uncluttered.

These alternative methods offer different approaches to setting up and managing Jekyll on AlmaLinux 8. Choose the one that best suits your needs and technical preferences.