Package a Go Program into an RPM Quickly with go-bin-rpm
This guide walks through installing rpmbuild dependencies, using the open‑source go‑bin‑rpm tool to create a minimal rpm.json configuration, adding optional systemd service definitions, and automating the build and packaging process with a Makefile to produce a ready‑to‑install RPM for a Go binary.
First, install the rpmbuild toolchain required for any language that needs to be packaged as an RPM:
yum install -y gcc make rpm-build redhat-rpm-configThe go-bin-rpm utility simplifies the packaging process. Install it with the following one‑liner:
wget -O - https://raw.githubusercontent.com/mh-cbon/latest/master/bintray.sh \
| GH=mh-cbon/go-bin-rpm sh -xeVerify the installation:
$ go-bin-rpm --version
go-bin-rpm version 1.0.0Create a rpm.json file (the default name can be used so you don’t need to pass it explicitly). A minimal example looks like this:
{
"name": "you-service-name",
"version": "0.0.1",
"release": "20221110",
"arch": "x86_64",
"summary": "",
"description": "",
"license": "iswbm.com",
"url": "https://gtihub.com/iswbm/!name!",
"files": [
{
"from": "./bin/!name!",
"to": "/usr/local/!name!/",
"base": "",
"type": ""
},
{
"from": "./!name!.service",
"to": "/usr/lib/systemd/system/",
"base": "",
"type": ""
}
]
}If the RPM should install a systemd service, the files array must contain the binary, the service unit file, and any configuration files needed.
An example .service template:
[Unit]
Description
After=syslog.target network.target
[Service]
Environment=key=value
Type=simple
NotifyAccess=all
TimeoutStartSec=0
Restart=always
User=root
ExecStart=/usr/local/xxxx/bin/xxxx
[Install]
WantedBy=multi-user.targetAfter the configuration is ready, generate the RPM (replace VERSION and RELEASE with the appropriate values):
# VERSION and RELEASE should be substituted
go-bin-rpm generate -o rpms/xxxx-$(VERSION)-$(RELEASE).rpmIf you need different binaries for different platforms, create separate rpm.json files and specify the one to use with the -f rpm.json flag.
To avoid typing the long commands each time, a Makefile can automate the workflow:
VERSION = 1.0.0
RELEASE = $(shell date +"%Y%m%d")
.PHONY: build-go
build-go:
go build -o ./bin/hello .
.PHONY: build
build: build-go
sed -i "s/VERSION/$(VERSION)/g" rpm.json
sed -i "s/RELEASE/$(RELEASE)/g" rpm.json
mkdir -p rpms
go-bin-rpm generate -o rpms/hellp-$(VERSION)-$(RELEASE).rpmRunning make build compiles the Go binary, updates the version fields in rpm.json, creates the output directory, and produces the final RPM in a single step.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Golang Shines
We share daily the latest Golang technical articles, practical resources, language news, tutorials, and real-world projects to help everyone learn and improve.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
