How to resolve the trouble occurred when I install Go into the alpine image

by Renée French CC BY 3.0

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

References

--

--

--

Site Reliablity Engineer in Tokyo. SRE/devops/Go/AWS/Terraform/Kubernetes/Docker

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
TAKASHI NARIKAWA

TAKASHI NARIKAWA

Site Reliablity Engineer in Tokyo. SRE/devops/Go/AWS/Terraform/Kubernetes/Docker

More from Medium

How to use a Cloudflare Domain with a Cloudfront distribution

Crafting a Useful & Extensible Toolchain

Building with the GitHub API

A figurine of Mona, GitHub’s mascot, on a desk.

S3 POST Policy — The hidden S3 feature you haven’t heard of