
Build and Optimize Hugo Binary
Contents
Let’s see how to optimize Hugo size binary as small as possible.
About Go Optimization
Optimizing the sizes of Hugo binary may useful for saving your storage, especially if you keep this on a remote server. There are several articles mentioned that go binary can be optimized as small as possible. According to the filippo.io, we can use the -s and -w linker flags to strip the debugging and save almost 28% of sizes. Petr Jahoda via itnext.io also mentioned that using UPX with --best --lzma parameter will reduce about 21% of the original size.
Based on those articles, we can apply it to optimize Hugo binary and save more sizes. Take a look into my tweet below:
Update Jun 2025
The tweet has been removed as the original post is no longer available.
Install Required Packages
Info
I wrote this only for
linux/amd64 environment- First of all, these are required packages to build Hugo binary:
Install required packages
a. Setup GoLang
1wget https://go.dev/dl/go1.24.0.linux-amd64.tar.gz && sudo tar -C /usr/local -xzf go1.24.0.linux-amd64.tar.gzb. Setup UPX
1wget https://github.com/upx/upx/releases/download/v5.0.0/upx-5.0.0-amd64_linux.tar.xz && sudo tar -C /usr/local -xf upx-5.0.0-amd64_linux.tar.xz --transform 's/upx-5.0.0-amd64_linux/upx/'Add all environment into
.profile1 2 3export PATH=$PATH:/usr/local/go/bin export PATH=$PATH:$HOME/go/bin export PATH=$PATH:/usr/local/upx
Build Hugo Binary
Download latest Hugo source
1git clone https://github.com/gohugoio/hugo -b master && cd hugoExport all required go environment
1export CGO_ENABLED="1"Build go binary with
-sand-wlinker flags1go install -ldflags="-s -w" --tags extendedOptimize Hugo binary with upx
1upx --best --lzma "$HOME/go/bin/hugo"
Tips
If you want instant build, you can use my script below:
| |
The output file will be in the bin/$(go env GOARCH)/hugo.
Messages from UPX
False positive antivirus alerts of official UPX releases (Windows only). See #437




