Running and testing your Jekyll site locally with custom options
Developing with Jekyll often requires running your site locally to test changes before deploying them live. Here is a handy yet useful one-line command that I usually use to run my Jekyll site locally with custom options.
My main go-to command for running my Jekyll site locally is:
bundle exec jekyll serve --port 8000 --livereload --future
Let’s go through each part of the command to understand its purpose
bundle exec
- Function: Launches a Ruby program in the environment of the current Gem bundle.
- Why It’s Important: Ensures that all gems listed in your Jekyll site’s Gemfile are correctly loaded and used. This prevents version conflicts between different Jekyll projects and ensures a consistent development environment.
jekyll serve
- Function: Builds your Jekyll site and starts a local web server.
- Why It’s Important: Allows you to view your site in a web browser by navigating to
localhost:4000
(or another specified port). It’s essential for the development and testing of your site, providing a real-time preview of your work.
--port 8000
- Function: Changes the default port on which the server runs from 4000 to 8000.
- Why It’s Important: Useful if port 4000 is already in use or if you want to run multiple Jekyll instances simultaneously. Access your site by entering
localhost:8000
in your browser.
--livereload
- Function: Enables the LiveReload feature.
- Why It’s Important: Automatically reloads the website in the browser whenever you make changes to the code. This saves time as you don’t need to manually refresh to see updates.
--future
- Function: Includes posts with a future date in the build.
- Why It’s Important: Useful for previewing scheduled posts or posts with a future date. Without this option, Jekyll will not include posts with a future date in the build. In my next post, I further discuss how to deal with future posts in Jekyll.
Summary
For my workflow, this command is a powerful tool and may be for other Jekyll developers as well. It ensures your development environment is correctly set up, allows testing your website on a specific port, and boosts your productivity with automatic reloading upon code changes.
comments