# Cross Compile Your Go Programs

- Published: October 2, 2013
- Author: William Kennedy
- URL: https://www.ardanlabs.com/blog/2013/10/cross-compile-your-go-programs.html

<b><span style="font-size: large;">Introduction</span></b><br />
In my post about building and running programs in <a href="https://www.ardanlabs.com/blog/2013/09/running-go-programs-in-ironworker.html" target="_blank">Iron.Io</a>, I needed to switched over to my Ubuntu VM to build linux versions of my test programs locally. I love the ability to have Ubuntu available to me for building and testing my code. However, if I can stay on the Mac side it is better.<br />
<br />
I have wanted to learn how to cross compile my Go programs for the two platforms I use, darwin/amd64 and linux/amd64. This way I could create final builds of my programs and publish everything from my Mac OS. After a couple of hours I am finally able to do this.<br />
<br />
If you don't have the need to cross compile your code then I recommend you stick with the traditional distribution packages and installs for Go. If this is something you need, then it all starts with downloading the current release of the Go source code.<br />
<br />
<span style="font-size: large;"><b>Installing Git</b></span><br />
The Go source code is stored in a DVCS called Github and is located on <a href="http://github.com/golang/go" target="_blank">github.com/golang/go</a>. The first thing you need to do is install Git if you don't already have it.<br />
<br />
Go to the download page on the Git website: &nbsp;[http://git-scm.com/downloads](http://git-scm.com/downloads)<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<b><span style="font-size: large;">Cloning Go Source Code</span></b><br />
<a href="http://golang.org/doc/install/source" target="_blank"><span class="sr-only">External resource on golang.org: </span>http&#58;//golang.org/doc/install/source</a><br />
<br />
Open up a Terminal session and go to your $HOME directory and clone the current release of the Go source code. This will create a folder named Go, make sure this does not already exist:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
cd $HOME<br />
git clone https://go.googlesource.com/go
</div>
<br />
If everything works correctly you should see the following output or something similar:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
Cloning into 'go'...
<br />
remote: Sending approximately 57.30 MiB ...<br />
remote: Counting objects: 6825, done<br />
remote: Finding sources: 100% (1345/1345)<br />
remote: Total 194730 (delta 160700), reused 194473 (delta 160700)<br />
Receiving objects: 100% (194730/194730), 55.07 MiB | 2.92 MiB/s, done.<br />
Resolving deltas: 100% (161032/161032), done.<br />
Checking connectivity... done.
</div>
<br />
Once that is done you now have a folder called&nbsp;<b>go</b>&nbsp;inside of the $HOME directory with the source code under&nbsp;<b>src</b>.&nbsp;Now change your directory to the&nbsp;<b>go</b>&nbsp;folder and checkout the latest version:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
cd go<br />
git checkout go1.4.1</div>
<br />
If everything works correctly you should see the following output or something similar:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
Note: checking out 'go1.4.1'.
<br />
<br />
You are in 'detached HEAD' state. You can look around, make experimental<br />
changes and commit them, and you can discard any commits you make in this<br />
state without impacting any branches by performing another checkout.<br />
<br />
If you want to create a new branch to retain commits you create, you may<br />
do so (now or later) by using -b with the checkout command again. Example:<br />
<br />
&nbsp; git checkout -b new_branch_name<br />
<br />
HEAD is now at 886b02d... [release-branch.go1.4] go1.4.1
</div>
<br />
<b><span style="font-size: large;">Building Go For Each Target</span></b><br />
Now you need to build the Go code for the targets you need. For now just build darwin/amd64 and linux/amd64.<br />
<br />
First build the Go code for the darwin/amd64 target which is for the host machine:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
cd src<br />
GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 ./make.bash --no-clean</div>
<br />
If everything builds correctly you should see the build end like this:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
---<br />
Installed Go for darwin/amd64 in /Users/name/go<br />
Installed commands in /Users/name/go/bin
</div>
<br />
If you get the following error on your Mac there is a fix:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
 crypto/x509 root_darwin.go:9:43: error: CoreFoundation/CoreFoundation.h: No such file or directory root_darwin.go:10:31: error: Security/Security.h: No such file or directory mime/multipart net/mail</div>
<br />
This means you don't have the Command Line Tools for Xcode installed on your machine. To install the command line tools on 10.9 follow these instructions:<br />
<br />
[http://www.computersnyou.com/2025/2013/06/install-command-line-tools-in-osx-10-9-mavericks-how-to/](/broken-link)<br />
<br />
Click the Install button for the Command Line Tools. Once that is done try building the Go code again.<br />
<br />
Once the build is successful, open the Go folder. You should see everything you need for building Go programs on the Mac 64 bit environment:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">

<a href="../../../images/goinggo/Screen+Shot+2013-10-02+at+5.22.33+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-02+at+5.22.33+PM.png)</a>

<a href="../../../images/goinggo/Screen+Shot+2013-10-02+at+5.37.46+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-02+at+5.37.46+PM.png)</a>

</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
You have the go, godoc and gofmt tools and all the package related libraries and tools. Next you need to fix your PATH to point to the bin folder so you can start using the tools.<br />
<div style="clear: both;">
</div>
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
export PATH=$HOME/go/bin:$PATH</div>
<br />
You may want to set that in .bashrc or .bash_profile file from inside the $HOME directory.<br />
<br />
With the path set, check that Go is working. Check the version and the environment:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
<b>go version</b><br />
go version go1.4.1 darwin/amd64<br />
<br />
<b>go env</b><br />
GOARCH=&quot;amd64&quot;<br />
GOBIN=&quot;&quot;<br />
GOCHAR=&quot;6&quot;<br />
GOEXE=&quot;&quot;<br />
GOHOSTARCH=&quot;amd64&quot;<br />
GOHOSTOS=&quot;darwin&quot;<br />
GOOS=&quot;linux&quot;<br />
GOPATH=&quot;&quot;<br />
GORACE=&quot;&quot;<br />
GOROOT=&quot;/Users/name/go&quot;<br />
GOTOOLDIR=&quot;/Users/name/go/pkg/tool/darwin_amd64&quot;<br />
CC=&quot;gcc&quot;<br />
GOGCCFLAGS=&quot;-g -O2 -fPIC -m64&quot;<br />
CGO_ENABLED=&quot;1&quot;
</div>
<br />
If everything looks good, now build a version of Go that will let you build linux/amd64 binaries:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
GOOS=<b>linux</b> GOARCH=amd64 CGO_ENABLED=<b>0</b> ./make.bash --no-clean</div>
<br />
You can't use CGO when cross compiling. Make sure CGO_ENABLED is set to 0<br />
<br />
If everything builds correctly you should see the build end like this:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
---<br />
Installed Go for <b>linux</b>/amd64 in /Users/name/go<br />
Installed commands in /Users/name/go/bin
</div>
<br />
If you look at the Go folder again you should see some new folders for linux/amd64:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">

<a href="../../../images/goinggo/Screen+Shot+2013-10-02+at+5.43.28+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-02+at+5.43.28+PM.png)</a>

<a href="../../../images/goinggo/Screen+Shot+2013-10-02+at+5.43.39+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-02+at+5.43.39+PM.png)</a>

</div>
<br />
Now it is time to test if you can build Go programs for both the Mac and Linux operating systems. Set up a quick GOPATH and Go program. In Terminal run the following commands:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
cd $HOME<br />
mkdir example<br />
mkdir src<br />
mkdir simple<br />
export GOPATH=$HOME/example
<br />
cd example/src/simple</div>
<br />
Create a file called main.go inside of the simple folder with this code:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
package main<br />
<br />
import (<br />
&nbsp; &nbsp; &quot;fmt&quot;<br />
)<br />
<br />
func main() {<br />
&nbsp; &nbsp; fmt.Printf(&quot;Hello Gophers\n&quot;)<br />
}
</div>
<br />
First build the Mac version and make sure it is a Mac executable using the file command:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
go build<br />
file simple<br />
<br />
simple: Mach-O 64-bit executable x86_64
</div>
<br />
The file command tells us what type of file our program is. It is certainly a Mac executable file.<br />
<br />
Now build the code for linux/amd64:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
<b>export GOARCH=&quot;amd64&quot;</b><br />
<b>export GOOS=&quot;linux&quot;</b><br />
<br />
go build<br />
file simple<br />
<br />
Simple: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
</div>
<br />
You need to change either one or both of the OS/ARCH environment variables to point to the target platform and architecture. Then you can build the code. After the build you can see the file command is reporting the program is a linux executable.<br />
<br />
As a reference, here are the different platforms and architectures you can build for cross compilation:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
$GOOS &nbsp; &nbsp; $GOARCH<br />
darwin &nbsp; &nbsp;386 &nbsp; &nbsp; &nbsp;-- 32 bit MacOSX<br />
darwin &nbsp; &nbsp;amd64 &nbsp; &nbsp;-- 64 bit MacOSX<br />
freebsd &nbsp; 386<br />
freebsd &nbsp; amd64<br />
linux &nbsp; &nbsp; 386 &nbsp; &nbsp; &nbsp;-- 32 bit Linux<br />
linux &nbsp; &nbsp; amd64 &nbsp; &nbsp;-- 64 bit Linux<br />
linux &nbsp; &nbsp; arm &nbsp; &nbsp; &nbsp;-- RISC Linux<br />
netbsd &nbsp; &nbsp;386<br />
netbsd &nbsp; &nbsp;amd64<br />
openbsd &nbsp; 386<br />
openbsd &nbsp; amd64<br />
plan9 &nbsp; &nbsp; 386<br />
windows &nbsp; 386 &nbsp; &nbsp; &nbsp;-- 32 bit Windows<br />
windows &nbsp; amd64 &nbsp; &nbsp;-- 64 bit Windows</div>
<br />
<b><span style="font-size: large;">Installing Go Tooling</span></b><br />
Once you finish building Go for the targets you want, you will want to install these Go tools for the default target.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
go get golang.org/x/tools/cmd/godoc<br />
go get golang.org/x/tools/cmd/vet<br />
go get golang.org/x/tools/cmd/goimports<br />
go get golang.org/x/tools/cmd/gorename<br />
go get golang.org/x/tools/cmd/oracle<br />
go get golang.org/x/tools/cmd/gotype<br />
go get github.com/golang/lint/golint</div>
<br />
<b><span style="font-size: large;">Release Updates</span></b><br />
If you are performing an update be sure to remove the pkg folder from your GOPATH. These archive files will no longer be valid.<br />
<br />
Then you will want to update the source code to the current release:<br />
<br />
&#42;&#42;NOTE: Read This First: http://golang.org/s/go14nopkg<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
cd $HOME/go/src<br />
git fetch<br />
git checkout go&lt;tag&gt;</div>
<br />
Then you will want to build your targets again:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 ./make.bash --no-clean<br />
GOOS=<b>linux</b> GOARCH=amd64 CGO_ENABLED=<b>0</b> ./make.bash --no-clean</div>
<br />
Finally re-build the tools. Before doing so, remove it is a good idea to remove the pkg folder from your GOPATH.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; padding: 10px;">
go get -u golang.org/x/tools/cmd/godoc<br />
go get -u golang.org/x/tools/cmd/vet<br />
go get -u golang.org/x/tools/cmd/goimports<br />
go get -u golang.org/x/tools/cmd/gorename<br />
go get -u golang.org/x/tools/cmd/oracle<br />
go get -u golang.org/x/tools/cmd/gotype<br />
go get -u github.com/golang/lint/golint</div>
<br />
You may want to remove everything from the bin folder as well and<b> go get </b>them to re-build and install.<br />
<br />
<b><span style="font-size: large;">Conclusion</span></b><br />
This is the documentation for building Go from the source:<br />
<br />
<a href="http://golang.org/doc/install/source" target="_blank"><span class="sr-only">External resource on golang.org: </span>http&#58;//golang.org/doc/install/source</a><br />
<br />
This is a document written by Dave Cheney about cross compilation. He has build a script that you can download. It makes all of this real simple to perform:<br />
<br />
<a href="http://dave.cheney.net/2013/07/09/an-introduction-to-cross-compilation-with-go-1-1" target="_blank"><span class="sr-only">External resource on dave.cheney.net: </span>http&#58;//dave.cheney.net/2013/07/09/an-introduction-to-cross-compilation-with-go-1-1</a><br />
<br />
Mitchell Hashimoto built this great tool called <a href="https://github.com/mitchellh/gox" target="_blank">gox</a>. This tool makes it real easy to build your program for all the different targets without the need to manually change the GOARCH and GOOS environment variables.
