Building a totally static go binary.

647 views
Skip to first unread message

Hongyi Zhao

unread,
Jan 20, 2021, 11:28:41 AM1/20/21
to golang-nuts

On Ubuntu 20.10, I use the following steps to build the go binary:

$ sudo apt-get build-dep -y golang
$ cd go.git/src
$ ./make.bash
$ cd ../
$ ldd bin/go
linux-vdso.so.1 (0x00007ffef27ee000) libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0
(0x00007f540e41d000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f540e233000) /lib64/ld-linux-x86-64.so.2 (0x00007f540e475000)

It seems that it needs some distribution relevant dependencies for working correctly. Thus, I want to know whether I can build a totally static go binary so that it can run under other Linux distributions.

Regards,
HY

Axel Wagner

unread,
Jan 20, 2021, 11:38:30 AM1/20/21
to Hongyi Zhao, golang-nuts
I just tried building using

mero@hix ~/go/src$ CGO_ENABLED=0 ./make.bash
mero@hix ~/go/src$ file ../bin/go
../bin/go: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=vuwQ_gWzFYzm9u1fSb6J/teB52WVS50es5tAa1xez/9HCuFMUtERd7wPEheIgP/i3RObo0pxXnfqkowogAH, not stripped

I'm honestly quite surprised this works.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d496885c-63ad-4efe-901c-fb4b198bdf84n%40googlegroups.com.

Ian Lance Taylor

unread,
Jan 20, 2021, 7:27:02 PM1/20/21
to Hongyi Zhao, golang-nuts
Related discussion at https://golang.org/issue/26492 .

Ian

Hongyi Zhao

unread,
Jan 20, 2021, 8:34:06 PM1/20/21
to golang-nuts
I looked through the above issue, but it seems so chaos. As a result, I still can't draw any conclusion based the discussion there.

Regards,
HY

Hongyi Zhao

unread,
Jan 20, 2021, 8:37:12 PM1/20/21
to golang-nuts
On Thursday, January 21, 2021 at 12:38:30 AM UTC+8 axel.wa...@googlemail.com wrote:
I just tried building using

mero@hix ~/go/src$ CGO_ENABLED=0 ./make.bash
mero@hix ~/go/src$ file ../bin/go
../bin/go: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=vuwQ_gWzFYzm9u1fSb6J/teB52WVS50es5tAa1xez/9HCuFMUtERd7wPEheIgP/i3RObo0pxXnfqkowogAH, not stripped

I'm honestly quite surprised this works.

Really, it also does the trick for my case:

$ file go
go: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=5jq4NVhOUQBZ9qoSzZZP/ZGpiMeW1vV69o1mL-B9A/6YaUKXNOw58uUviv4EeO/4sUw0l9CinylaK_Pov59, not stripped

Regards,
HY

Brian Candler

unread,
Jan 22, 2021, 9:05:58 AM1/22/21
to golang-nuts
Building statically with cgo seems to be possible, although over time I found the required flags have changed.
Reply all
Reply to author
Forward
0 new messages