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 post where I fixed it with a direct downloadable link