How to resolve the trouble occurred when I install Go into the alpine image
At First
Hi, everyone.
My name is Takashi Narikawa.(twitter:@fukubaka0825)
I work as a software engineer at Wano Co., Ltd.
This is my first post on medium.
This time, I would like to leave a memorandum about the trouble occurred when I install Go into the alpine image.
TL;DR
- If you want to install Go on an alpine-based image, use apk instead of wget or curl command
What I wanted to do
- I want to auto-deploy lambda with apex command on codebuild (CI) with hashicorp / terraform image
- because I wrote lambda script in Go language, it is necessary to install Go in addition to the hashicorp / terraform image .
What Trouble
hashicorp / terraform image is based on alpine. I tried to install Go with buildspec.yml of AWS Codebuild as follows
export GO_VERSION=1.12.4
wget https://storage.googleapis.com/golang/go{GO_VERSION}.linux-amd64.tar.gz
tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
go version
Then, when go version command was executed, the following error appeared
sh: go: not found
How to resolve the trouble
docker-How to install Go in alpine linux-Stack Overflow had a similar case So it was helpful
With Alpine, you have libmusl instead of glibc. Alpine’s libmusl is not a 1 for 1 replacement. Code linked against glibc will show a not found error which is actually from the dynamic linker. You can see what libraries are linked to the binary with ldd:
Apparently, the behavior of libmusl (linux standard C library) included instead of glibc seems to be different.
# ldd /usr/local/go/bin/go
/lib64/ld-linux-x86-64.so.2 (0x7f63ceed1000)
libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7f63ceed1000)
libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f63ceed1000)
I see, / usr / local / go / bin / go has a dynamic link to / lib64 / ld-linux-x86–64.so.2 and returned not found error.
This guy’s suggestion is whether to run Go binaries (do not download go itself) or glibc (if you use CentOS or Debian), but I didn’t use either solution.
I solved it by downloading go with apk as follows.
apk add --update --no-cache vim git make musl-dev go curl# Configure Go
export GOPATH=/root/go
export PATH=${GOPATH}/bin:/usr/local/go/bin:$PATH
export GOBIN=$GOROOT/bin
mkdir -p ${GOPATH}/src ${GOPATH}/bin
export GO111MODULE=on
go version