Installing Hugo via package managers like apt on Linux (Debian, Ubuntu) often results in outdated versions, which can be delayed by months or even years. While installing Hugo directly from GitHub is the best way to stay current, manually downloading the .deb package each time a new version is released can be a chore. Many users find themselves putting off updates, leaving Hugo outdated for longer than they’d like.

This simple script automates both the installation and updating of Hugo Extended to the latest version directly from GitHub. It checks if Hugo is installed, fetches the latest release, and installs it, all while eliminating the need for manual downloads or worrying about missing updates. It’s designed for the Hugo Extended amd64 Linux version but can be easily modified to suit different systems or versions.

The Script

#!/bin/bash

# Check if Hugo is installed by running `hugo version`
HUGO_VERSION=$(hugo version 2>/dev/null)

if [[ $? -ne 0 ]]; then
    # If hugo is not installed, print the message
    echo "Hugo is not yet installed."
else
    # If hugo is installed, display the current version
    echo "$HUGO_VERSION"
fi

# Fetch the URL of the latest Hugo extended release for Linux (non-withdeploy version)
HUGO_URL=$(curl -s https://api.github.com/repos/gohugoio/hugo/releases/latest | \
grep "browser_download_url" | grep "hugo_extended" | grep "linux-amd64.deb" | grep -v "withdeploy" | \
sed -E 's/.*"([^"]+)".*/\1/')

# Check if the URL was successfully retrieved
if [[ -z "$HUGO_URL" ]]; then
    echo "Error: Unable to retrieve the latest Hugo release URL."
    exit 1
fi

# Extract the filename from the URL (e.g., hugo_extended_0.140.2_linux-amd64.deb)
FILE_NAME=$(basename "$HUGO_URL")

# Prompt the user before downloading, based on whether Hugo is installed or not
if [[ -z "$HUGO_VERSION" ]]; then
    # If Hugo is not installed
    echo -n "Latest Hugo Extended version is $FILE_NAME. Do you want to download it? (y/n): "
else
    # If Hugo is installed
    echo -n "Latest Hugo Extended version is $FILE_NAME. Do you want to download it? (y/n): "
fi

read -r RESPONSE
if [[ "$RESPONSE" != "y" ]]; then
    echo "Download aborted."
    exit 0
fi

# Download the latest Hugo .deb package with its original name
wget -q "$HUGO_URL" -O "$FILE_NAME" || {
    echo "Error: Failed to download Hugo package."
    exit 1
}

# Prompt the user before installing
echo -n "Do you want to install the Hugo package $FILE_NAME? (y/n): "
read -r RESPONSE
if [[ "$RESPONSE" != "y" ]]; then
    echo "Installation aborted."
    exit 0
fi

# Install the downloaded Hugo package
sudo dpkg -i "$FILE_NAME" || {
    echo "Error: Failed to install Hugo package."
    exit 1
}

# Success message
echo "Hugo package $FILE_NAME has been successfully installed."
hugo version

How to Use

  1. Save and Make the Script Executable
    Copy the script into a file (e.g., update_hugo.sh) and make it executable with:

    chmod +x update_hugo.sh
    
  2. Run the Script
    Execute the script with:

    ./update_hugo.sh
    

    The script will check if Hugo is installed, prompt you to download the latest version (which will be saved in the same directory as the script), and ask if you want to install it.

  3. Verify Installation
    Once installed, you can check the Hugo version with:

    hugo version
    

Conclusion

This script automates Hugo updates, ensuring you always have the latest version without the hassle of manual installation. Perfect for users who want to avoid outdated versions from apt and streamline their workflow, it keeps your Hugo installation current with minimal effort. Customize it for your own system and preferred version, and enjoy a seamless update process.