How to install Go in alpine linux
Asked Answered
S

9

58

I am trying to install Go inside an Alpine Docker image. For that I downloaded tar file from here inside my alpine docker image, untar it using following command:

tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz

exported PATH to have go binary as:

export PATH=$PATH:/usr/local/go/bin

However, when I say go version then it says that sh: go: not found. I am quite new to alpine. Does anyone know, what I am missing here?

Steps to reproduce-

$ docker run -it alpine sh
$ wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
$ tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz
$ export PATH=$PATH:/usr/local/go/bin
$ go version
Stylist answered 28/8, 2018 at 11:6 Comment(7)
What does echo $PATH tell you?Elbertelberta
/usr/local/go/bin # echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/binStylist
It has added PATH in it..Stylist
Not an answer to your direct question, but you could just use one of the official Golang alpine docker images. You can also use the official Dockerfile as a guide to setting up your own.Elbertelberta
You are right..but I ahve some specific requirements for my projectStylist
to execute a go binary inside a container does not require an install of the golang compiler which you show above ... to simplify I suggest you compile elsewhere (perhaps in another container) then mount the golang binary as a volume in your container ... this separation will make updates to both the golang compiler and your source code easier to maintainBanas
Not installing anything..If you look at the steps I am just extracting tar file and using Go binary.Stylist
S
27

Thanks BMitch.

I compiled the go source code and performed the below steps inside alpine image container.

echo "installing go version 1.10.3..." 
apk add --no-cache --virtual .build-deps bash gcc musl-dev openssl go 

# download go tar 
wget -O go.tgz https://dl.google.com/go/go1.10.3.src.tar.gz 
tar -C /usr/local -xzf go.tgz 
cd /usr/local/go/src/ 

# compile code
./make.bash 
export PATH="/usr/local/go/bin:$PATH"
export GOPATH=/opt/go/ 
export PATH=$PATH:$GOPATH/bin 
apk del .build-deps 
go version
Stylist answered 24/9, 2018 at 7:21 Comment(2)
when you run "apk add --no-cache --virtual .build-deps bash gcc musl-dev openssl go ", you can run go version after that. Why do you need those lines between them ?Rowdy
You can do that also. However, I focused more on installing specific version of goStylist
O
96

I just copied it over using multi stage builds, seems to be ok so far

FROM XXX
 
COPY --from=golang:1.13-alpine /usr/local/go/ /usr/local/go/
 
ENV PATH="/usr/local/go/bin:${PATH}"
Oconner answered 26/12, 2019 at 8:43 Comment(4)
+1 Love this answer! I didn't realize one could combine two Docker images like that. Previously if I wanted two main programs in a container, I thought I had to get one FROM an image, and install the other one in a RUN command. Brilliant!Citify
Great answer. Looking at a current golang Dockerfile you can see the last few lines are related to the go path. If you need the go path then also include the following after the COPY in your Dockerfile ENV GOPATH /go ENV PATH $GOPATH/bin:$PATH RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"Lafountain
getting bash: /usr/local/go/bin/go: cannot execute: required file not foundShishko
Sevy, base image may be some other linux distro, try golang:1.13Ortrud
S
44

The following Dockerfile worked for me. Simpler and more abstract.

FROM alpine:latest

RUN apk add --no-cache git make musl-dev go

# Configure Go
ENV GOROOT /usr/lib/go
ENV GOPATH /go
ENV PATH /go/bin:$PATH

RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin

# Install Glide
RUN go get -u github.com/Masterminds/glide/...

WORKDIR $GOPATH

CMD ["make"]

source: https://raw.githubusercontent.com/mickep76/alpine-golang/master/Dockerfile

Spoony answered 21/11, 2018 at 3:49 Comment(1)
Yes, this should be the better answer.Thoreau
S
27

Thanks BMitch.

I compiled the go source code and performed the below steps inside alpine image container.

echo "installing go version 1.10.3..." 
apk add --no-cache --virtual .build-deps bash gcc musl-dev openssl go 

# download go tar 
wget -O go.tgz https://dl.google.com/go/go1.10.3.src.tar.gz 
tar -C /usr/local -xzf go.tgz 
cd /usr/local/go/src/ 

# compile code
./make.bash 
export PATH="/usr/local/go/bin:$PATH"
export GOPATH=/opt/go/ 
export PATH=$PATH:$GOPATH/bin 
apk del .build-deps 
go version
Stylist answered 24/9, 2018 at 7:21 Comment(2)
when you run "apk add --no-cache --virtual .build-deps bash gcc musl-dev openssl go ", you can run go version after that. Why do you need those lines between them ?Rowdy
You can do that also. However, I focused more on installing specific version of goStylist
S
17

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:

/ # 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)

There are two options. The preferred option, and one used by docker's go images on Alpine, is to compile the go binaries on Alpine. You can see this in the Dockerfile for the golang image: https://github.com/docker-library/golang/blob/69f2d2a132565bf60afc91813801a3bdcc981526/1.10/alpine3.8/Dockerfile

The other option is to install glibc on Alpine, but once you start doing things like that, I'd question why use Alpine at all, and whether Debian or CentOS would be a more appropriate base image for you. Alpine has a wiki topic on this and there are third parties that have created glibc packages for alpine.

Saltzman answered 28/8, 2018 at 12:5 Comment(1)
This is the proper way to do it, thanks for the answer!Wharf
Q
13

I found the best way to get golang up running in alpine linux is to install it from source. This is also way followed in the official alpine go docker images.

FROM alpine:3.12

ARG GOLANG_VERSION=1.14.3

#we need the go version installed from apk to bootstrap the custom version built from source
RUN apk update && apk add go gcc bash musl-dev openssl-dev ca-certificates && update-ca-certificates

RUN wget https://dl.google.com/go/go$GOLANG_VERSION.src.tar.gz && tar -C /usr/local -xzf go$GOLANG_VERSION.src.tar.gz

RUN cd /usr/local/go/src && ./make.bash

ENV PATH=$PATH:/usr/local/go/bin

RUN rm go$GOLANG_VERSION.src.tar.gz

#we delete the apk installed version to avoid conflict
RUN apk del go

RUN go version

Quiteria answered 30/5, 2020 at 18:31 Comment(0)
B
7

If the basic requirement is to have specific go version installed inside alpine based docker image then refer these images available on official golang dockerhub account.

docker pull golang:1.12-alpine
docker pull golang:1.11-alpine
Bicycle answered 15/6, 2019 at 8:47 Comment(0)
B
4

Dockerfile:

ARG GOLANG_VERSION=1.20.4
RUN wget https://go.dev/dl/go${GOLANG_VERSION}.linux-amd64.tar.gz && \
    rm -rf /usr/local/go && tar -C /usr/local -xzf go${GOLANG_VERSION}.linux-amd64.tar.gz && \
    rm go${GOLANG_VERSION}.linux-amd64.tar.gz
ENV PATH="${PATH}:/usr/local/go/bin"
Broadus answered 5/6, 2023 at 15:45 Comment(1)
very stright forward, no magicCalceiform
Z
1

Just in case someone encounters the same issue with me.

I was able to install the golang1.17.6 ion Alpine3.15 by following @Yogesh Jilhawar 's answer.

When I ran the command apk add --no-cache --virtual .build-deps bash gcc musl-dev openssl go, I got

ERROR: unable to select packages:
  go (no such packages):
    required by: world[go]

Then I tried to install the "gcc-go", it installs. After that, I can build the golang from the source successfully.

Zumstein answered 9/2, 2022 at 9:46 Comment(0)
F
-5

There Is the Alpine package, with the latest versione of golang:

pkg --update add go

Flashboard answered 6/7, 2021 at 15:10 Comment(4)
pkg does not exist as a binary by default on AlpineCoolant
@Coolant install pkg during build and then uninstall it, it takes to be practical sometimesFlashboard
then your answer should include that, given the context of the questionCoolant
@Coolant the scope is not about how to idomatically use dockerFlashboard

© 2022 - 2024 — McMap. All rights reserved.