July 30th, 2024

Building static binaries with Go on Linux

The article explains how to build static binaries with Go on Linux, noting that while possible, it requires specific configurations and tools, especially when using C code via cgo.

Read original articleLink Icon
CuriosityInterestCaution
Building static binaries with Go on Linux

The article discusses the process of building static binaries with Go on Linux, highlighting that while Go can produce statically-linked binaries, it does not do so by default in all cases. The author conducts experiments with simple Go programs to determine whether the resulting binaries are statically or dynamically linked. For instance, a basic "hello, world" program is confirmed to be statically linked, while programs that utilize the Go standard library for DNS and user lookups are dynamically linked unless specific build tags or environment variables are set to enforce static linking.

The article explains that when C code is included in a Go program via cgo, the resulting binary will typically be dynamically linked due to dependencies on the C runtime library (libc). To create a statically linked binary in such cases, the author suggests using an alternative libc implementation, such as musl, or employing the Zig toolchain, which simplifies the process of static linking.

The author notes that achieving static linking requires additional effort and mentions a proposal for a `-static` flag in the Go build process to streamline this. The article concludes by stating that while building static binaries in Go on Linux can be complex, it is feasible with the right tools and configurations. The code for the experiments is available on GitHub for further exploration.

AI: What people are saying
The comments discuss various aspects of building static binaries with Go, particularly in relation to SQLite and performance issues.
  • Using specific build tags can optimize SQLite for static binaries, as noted by a user.
  • There are alternative methods to use SQLite without cgo, such as through WASM, which some users find more efficient.
  • Concerns about performance when using static binaries with SQLite, particularly with different compilers like Zig.
  • Potential for integrating Go with Cosmopolitan libc to create cross-platform static binaries.
  • Discussion on including the Go runtime within the program itself, rather than embedding or downloading it.
Link Icon 10 comments
By @acatton - 4 months
The secret with sqlite is to use "-tags sqlite_omit_load_extension", if you don't use any extension. (which is 99% of the users)

This is explained in https://www.arp242.net/static-go.html

By @daenney - 4 months
In the specific case of SQLite, you can use it through WASM now [1]. It uses the dependency and Cgo-free Wazero runtime.

Performance so far has been better than the modernc transpile and it’s probably sufficient for a lot of use cases.

[1] https://github.com/ncruces/go-sqlite3

By @bradfitz - 4 months
Careful. We (Tailscale) tried to use static Go binaries a year or two ago built with Zig (zig cc) and the SQLite performance was atrocious. It passed all our tests but it didn't survive deploying to prod. It was a very quick (and uneventful) rollback at least.

(Needless to say, we have better load testing tooling now)

I forget the details, but something about the libc allocator used by SQLite-with-Zig-libc being ... not good.

By @nrvn - 4 months
Producing static PIE binaries is a bigger challenge still.

  $ go build -ldflags '-linkmode external -s -w -extldflags "--static-pie"' -buildmode=pie -tags 'osusergo,netgo,static_build' main.go
For anyone curious to delve into what is this and why: https://www.leviathansecurity.com/blog/aslr-protection-for-s...
By @SpecialistK - 4 months
Very interesting and well described! If I were to have one nitpick, it would be the use of "Unix" when it's more specific to Linux. Ex. "The libc typically used on Unix systems is glibc" However I'm sure all of the concepts still apply on BSD, Solaris, etc.
By @wwarner - 4 months
There’s also Filippo Valsorda linking directly to Rust via .a files. https://words.filippo.io/rustgo/
By @sbstp - 4 months
I feel like there's a lot of potential between Go and Cosmopolitan libc. Go itself does not use libc much, as shown in the blog, but some great libraries like SQLite3 need it (unless you use https://pkg.go.dev/modernc.org/sqlite).

The ability to build a single static binary that works on Linux, Mac and Windows using Go would be life changing for the internal tools I develop at work.

By @moondev - 4 months
What's the best way to include the go runtime itself, as in ability to invoke the "go" program from the program itself . I'm not talking about embedding it or downloading it. I want it included within the program.