Fixing Hugo Source Build Errors on CentOS 7

Working around the errors that appear when building hugo from source

* This page contains promotional content

I posted a similar article before in GCC error with hugo on CentOS7 , but recently the build from source failed, so I worked on it.
Incidentally, the version of hugo I am trying to build is 0.82

Environment

# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

# git version
git version 1.8.3.1

# go version
go version go1.15.5 linux/amd64

Building hugo from source

$ vim from-gh.sh
mkdir $HOME/src
cd $HOME/src
git clone https://github.com/gohugoio/hugo.git
cd hugo
go install --tags extended

$ sh from-gh.sh

Error

Cloning into 'hugo'...
remote: Enumerating objects: 50309, done.
remote: Total 50309 (delta 0), reused 0 (delta 0), pack-reused 50309
Receiving objects: 100% (50309/50309), 93.65 MiB | 9.80 MiB/s, done.
Resolving deltas: 100% (34542/34542), done.
go: github.com/sanity-io/[email protected] requires
	github.com/pmezard/[email protected]: invalid version: git fetch --unshallow -f origin in /root/go/pkg/mod/cache/vcs/b66720d7c9f6776b1690ab6c7de6bd08fe64d74a3eaff8dbdef6603dac225370: exit status 128:
	fatal: git fetch-pack: expected shallow list

When I updated git to the latest version, this time I got the error below

$ git version 
git version 2.30.1
$ go install --tags extended
tpl/internal/go_templates/texttemplate/helper.go:11:2: package io/fs is not in GOROOT (/usr/lib/golang/src/io/fs)

A g++ error appeared as well

 go build github.com/bep/golibsass/internal/libsass: g++: exec: "g++": executable file not found in $PATH

g++ was solved with yum install -y gcc-c++

Solution (fix)

I confirmed that the build goes through once go is updated to the latest version

Incidentally, up to go 1.15 go.mod was automatically updated as needed every time you ran a command, but from 1.16 that automatic update no longer happens and the user has to specify things explicitly. So you may get an error when running a go command, but running go mod tidy either solves it or gives you a clue to the solution

go manual install

Before installing the latest version of go, remove the golang installed with yum
(If you are able to change the install location and environment variables yourself, you may be able to use both together by setting the PATH instead of removing it)

$ sudo yum -y remove golang

$ wget https://golang.org/dl/go1.16.linux-amd64.tar.gz
$ sudo tar zxf go1.16.linux-amd64.tar.gz -C /usr/local/
$ export PATH=$PATH:/usr/local/go/bin
$ go version
go version go1.16 linux/amd64
$ go env|grep GOROOT
GOROOT="/usr/local/go"

Building hugo again

Do Building hugo from source once more.

However, since it failed once, $HOME/src has probably already been created, so continue with the following operation

$ cd $HOME/src/hugo
$ go install --tags extended

If no error appears this time, hugo should have been created inside $HOME/go/bin/.
If it is not there, either the build failed or GOPATH may be set to a different location.

$ cd $HOME/go/bin
$ ./hugo version
hugo v0.82.0-DEV+extended linux/amd64 BuildDate=unknown

In my environment, building the latest version of hugo required raising go to the latest version as well, but if you still get errors, lowering the hugo version and building may succeed.

Incidentally, this time I needed the hugo extended version on CentOS7, but if you do not need the extended version, it is easier to fetch the binary from the official site

See also