Script to install latest hugo release on macos and ubuntu


#1

Made a script to install the latest hugo release (non-extended version) on macOS. It works on Ubuntu as well, just replace occurrences of macOS with Linux.

Edit: This assumes $HOME/bin/ is already on your PATH

Edit 2: It now does all work in /tmp/, then moves the hugo binary to /usr/local/bin/, so this script will likely need need to be run with sudo

# Change to temporary directory
pushd /tmp/

# Get JSON response of latest releases, find the one we want, 
# pretty-up the URL, then download it
curl -s https://api.github.com/repos/gohugoio/hugo/releases/latest \
| grep "browser_download_url.*hugo_[^extended].*_macOS-64bit\.tar\.gz" \
| cut -d ":" -f 2,3 \
| tr -d \" \
| wget -qi -

# Unzip hugo binary
tarball="$(find . -name "*macOS-64bit.tar.gz")"
tar -xzf $tarball

# Give hugo binary executable permissions
chmod +x hugo

# Move hugo binary to a location that is already on your PATH
mv hugo /usr/local/bin/

# Go back to previous directory
popd

# Display hugo binary location and version
location="$(which hugo)"
echo "Hugo binary location: $location"
version="$(hugo version)"
echo "Hugo binary version: $version"

My inspiration: https://gist.github.com/steinwaywhw/a4cd19cda655b8249d908261a62687f8


#2

Also note: https://www.bryanklein.com/blog/hugo-update-script-and-binaries-in-repository/


#3

Nice. It’s like he took the Gradle Wrapper concept and applied it to Hugo


#4

Also, mostly because I need to explain things to myself lol, did a small write-up:
https://zwbetz.com/script-to-install-latest-hugo-release-on-ubuntu-and-mac/


#5

I do it this way, with a zsh function (I’m using macOS):

Yours is more agnostic I think @zwbetz, but I just really like jq.

https://stedolan.github.io/jq/


#6

Nice. Btw, I’m digging your .zshrc file


#7

beware, probably a lot of detritus in there! :blush:


#8

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

#9

@dkebler – Just took it for a spin on macOS, nice improvements :+1:


#10

Ok here is my “super improved” version as a downloadable link
https://download.kebler.net/hugo-update
(latest update 8:26pst Nov 9,2018)
I’ll make any corrections/improvements to the copy at this link.

Easiest way to use it is download to ~/bin or another directory in your path. Do a chmod +x and then you’ll be able to run it from anywhere. Then create a daily cron job and bob’s your uncle.

Will only work on generic linux including ubuntu/debian as it only downloads those tarballs.

  • Will determine if you are trying to install on an arm32, arm64, or amd64 machine and download the correct tarball.
  • Does some better error checking, aborts if there is nothing to update
  • You can set the default install directory in the script and/or install to an alternate directory from command line.
  • Uses sudo only if need be.
  • will use github user and token variables you can set in your enviroment or in script because as I discovered if you do too many anonymous curls to the github API it locks you out

Fair warning I did do some good testing/debugging but my effort was maybe 1/2 hour. Find anything amiss why then do share your corrections.


#11

Link not working (at least for me):


#12

Concur, not working for me either.


#13

sorry that’s apparently a private thread and discourse won’t let me duplicate it
See above or below post where I fixed it with a direct downloadable link


#14

There was request to post source here. It’s below but will likely become outdated as changes are made so to see/download the latest use this link https://download.kebler.net/hugo-update

#!/bin/bash
# Version 0.1.0  8:26 PST, Nov 9, 2018
# see https://discourse.gohugo.io/t/script-to-install-latest-hugo-release-on-macos-and-ubuntu/14774/10
# if you have run into github api anonymous access limits then add user and token here or in environment
# GITHUB_USER=""
# GITHUB_TOKEN=""

DEFAULT_BIN_DIR="/usr/local/bin"
# Single optional argument is directory in which to install hugo
BIN_DIR=${1:-"$DEFAULT_BIN_DIR"}
BIN_PATH="$(which hugo)"
declare -A ARCHES
ARCHES=( ["arm64"]="ARM64" ["aarch64"]="ARM64"  ["x86_64"]="64bit" ["arm32"]="ARM"  ["armhf"]="ARM" )
ARCH=$(arch)

if [ -z "${ARCHES[$ARCH]}" ]; then
  echo  Your machine kernel architecture $ARCH is not supported by this script, aborting
  exit 1
fi


INSTALLED="$(hugo version 2>/dev/null | cut -d'v' -f2 | cut -c 3-5)"
CUR_VERSION=${INSTALLED:-"None"}
NEW_VERSION="$(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest  \
             | grep tag_name \
             | cut -d'.' -f2 | cut -d'"' -f1)"

echo "Hugo:  Current Version: $CUR_VERSION => New Version: $NEW_VERSION"

if [ -z "$NEW_VERSION" ]; then
  echo  Unable to retrieve new version number - Likely you have reached github anonymous limit
  echo  set environment variable `$GITHUB_USER` and `$GITHUB_TOKEN` and try again
  exit 1
fi

if ! [ $NEW_VERSION = $CUR_VERSION ]; then

  echo "Installing version $NEW_VERSION"
  echo "This machine's architecture is $ARCH"
  echo "Downloading Tarball Linux-${ARCHES[$ARCH]}"

  pushd /tmp/ > /dev/null

  curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest \
  | grep "browser_download_url.*hugo_[^extended].*_Linux-${ARCHES[$ARCH]}\.tar\.gz" \
  | cut -d ":" -f 2,3 \
  | tr -d \" \
  | wget --user=-u $GITHUB_USER --password=$GITHUB_TOKEN -qi -

  TARBALL="$(find . -name "*Linux-${ARCHES[$ARCH]}.tar.gz" 2>/dev/null)"
  echo Expanding Tarball
  tar -xzf $TARBALL

  chmod +x hugo

if [ -w $BIN_DIR ]; then
  echo "Installing hugo to $BIN_DIR"
  mv hugo -f $BIN_DIR
else
    echo "sudo Installing hugo to $BIN_DIR"
    sudo mv -f hugo $BIN_DIR
fi

  popd > /dev/null

  BIN_PATH="$(which hugo)"
  if [ -z "$BIN_PATH" ]; then
  printf "WARNING: Installed Hugo Binary in $BIN_DIR is not in your environment path\nPATH=$PATH\n"
else
  if [ "$BIN_DIR/hugo" != "$BIN_PATH" ]; then
  echo "WARNING: Just installed Hugo binary, $BIN_DIR , conflicts with existing Hugo in $BIN_PATH"
  echo "add $BIN_DIR to path and delete $BIN_PATH"
else
  echo "--- Confirmation ---"
  printf "New Hugo binary version in $BIN_DIR is\n $($BIN_DIR/hugo version)\n"
  fi
fi

else
  echo Latest version already installed in $BIN_PATH
fi

#15

Just for any getting here. To be clear the “improved” script not only installs hugo but will update an existing install. One script to rule all but updates on windows


#16

Hi @dkebler, been happily using this script on Ubuntu but have been playing with Manjaro Linux recently I found the script doesn’t work out of the box, it can’t find the kernel architecture and aborts. Seems that ‘arch’ command doesn’t work on arch based distros =D

Got it working by using uname -m instead, but not sure how portable that is. So comment out original line and use uname like so:

#ARCH=$(arch)
ARCH=$(uname -m)