Script to install latest hugo release on macos and ubuntu

Here’s my improved version. Checks if update is needed first. Avoids having to remember to preface with sudo. Checking local architecture and downloading the correct tarball is next improvement.

#!/bin/bash

CUR_VERSION="$(hugo version | cut -d'v' -f2 | cut -c 3-5)"
NEW_VERSION="$(curl -s https://api.github.com/repos/gohugoio/hugo/releases/latest | grep tag_name | cut -d'.' -f2 | cut -d'"' -f1)"
echo "Current Version: $CUR_VERSION => New Version: $NEW_VERSION"

if [ "$NEW_VERSION" -ne "$CUR_VERSION" ]; then

  echo "Installing version $NEW_VERSION"

  pushd /tmp/

  curl -s https://api.github.com/repos/gohugoio/hugo/releases/latest \
  | grep "browser_download_url.*hugo_[^extended].*_Linux-64bit\.tar\.gz" \
  | cut -d ":" -f 2,3 \
  | tr -d \" \
  | wget -qi -

  tarball="$(find . -name "*Linux-64bit.tar.gz" 2>/dev/null)"
  tar -xzf $tarball

  chmod +x hugo

  sudo mv hugo /usr/local/bin/

  popd

  location="$(which hugo)"
  echo "Hugo binary location: $location"

  version="$(hugo version)"
  echo "New Hugo binary version installed!: $version"

else
  echo Latest version already installed
fi
2 Likes