Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build application for Ubuntu 18 from Ubuntu 20

I build my Go Application in my Ubuntu 20.04 OS with go build -o myApp.

When I run this app on Ubuntu Server 18.04, this error occurs:

/lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by ./myApp)

As I searched in stackoverflow, someone mentioned installing glibc 2.29 on the server. But someone else replied that this way is risky and may break OS.

Someone else suggested building App with glibc 2.27.

How can I do this?

like image 941
DrMorteza Avatar asked Nov 15 '25 19:11

DrMorteza


1 Answers

How to make a static binary depends mostly on whether your code (or libraries) uses cgo.

If cgo is not needed, it's fairly easy: just disable it using the environment variable: CGO_ENABLED. This will automatically switch some cgo-based features to use pure go implementations (netgo, osusergo):

CGO_ENABLED=0 go build -o myApp

If cgo is required, you can tell the linker to statically link C code as such:

go build -ldflags="-extldflags=-static" -o myApp

Note that not all cgo code will be happy about this. How to solve it depends on the libraries used.

like image 55
Marc Avatar answered Nov 18 '25 20:11

Marc