blob: e71fff710f408b681ea091b535a152fc68777b4e [file] [log] [blame]
Colin Cross7bb052a2015-02-03 12:59:37 -08001<!--{
2 "Title": "Installing Go from source",
3 "Path": "/doc/install/source"
4}-->
5
6<h2 id="introduction">Introduction</h2>
7
8<p>
9Go is an open source project, distributed under a
10<a href="/LICENSE">BSD-style license</a>.
11This document explains how to check out the sources,
12build them on your own machine, and run them.
13</p>
14
15<p>
16Most users don't need to do this, and will instead install
17from precompiled binary packages as described in
18<a href="/doc/install">Getting Started</a>,
19a much simpler process.
20If you want to help develop what goes into those precompiled
21packages, though, read on.
22</p>
23
24<div class="detail">
25
26<p>
27There are two official Go compiler tool chains.
28This document focuses on the <code>gc</code> Go
Dan Willemsen6ff23252015-09-15 13:49:18 -070029compiler and tools.
Colin Cross7bb052a2015-02-03 12:59:37 -080030For information on how to work on <code>gccgo</code>, a more traditional
31compiler using the GCC back end, see
32<a href="/doc/install/gccgo">Setting up and using gccgo</a>.
33</p>
34
35<p>
Dan Willemsen6ff23252015-09-15 13:49:18 -070036The Go compilers support five instruction sets.
Colin Cross7bb052a2015-02-03 12:59:37 -080037There are important differences in the quality of the compilers for the different
38architectures.
39</p>
40
41<dl>
42<dt>
Dan Willemsen6ff23252015-09-15 13:49:18 -070043 <code>amd64</code> (also known as <code>x86-64</code>)
Colin Cross7bb052a2015-02-03 12:59:37 -080044</dt>
45<dd>
46 A mature implementation. The compiler has an effective
47 optimizer (registerizer) and generates good code (although
48 <code>gccgo</code> can do noticeably better sometimes).
49</dd>
50<dt>
Dan Willemsen6ff23252015-09-15 13:49:18 -070051 <code>386</code> (<code>x86</code> or <code>x86-32</code>)
Colin Cross7bb052a2015-02-03 12:59:37 -080052</dt>
53<dd>
54 Comparable to the <code>amd64</code> port.
55</dd>
56<dt>
Dan Willemsen6ff23252015-09-15 13:49:18 -070057 <code>arm</code> (<code>ARM</code>)
Colin Cross7bb052a2015-02-03 12:59:37 -080058</dt>
59<dd>
Dan Willemsen6ff23252015-09-15 13:49:18 -070060 Supports Linux, FreeBSD, NetBSD and Darwin binaries. Less widely used than the other ports.
61</dd>
62<dt>
63 <code>arm64</code> (<code>AArch64</code>)
64</dt>
65<dd>
66 Supports Linux and Darwin binaries. New in 1.5 and not as well excercised as other ports.
67</dd>
68<dt>
69 <code>ppc64, ppc64le</code> (64-bit PowerPC big- and little-endian)
70</dt>
71<dd>
72 Supports Linux binaries. New in 1.5 and not as well excercised as other ports.
Colin Cross7bb052a2015-02-03 12:59:37 -080073</dd>
74</dl>
75
76<p>
77Except for things like low-level operating system interface code, the run-time
78support is the same in all ports and includes a mark-and-sweep garbage
79collector, efficient array and string slicing, and support for efficient
80goroutines, such as stacks that grow and shrink on demand.
81</p>
82
83<p>
84The compilers can target the DragonFly BSD, FreeBSD, Linux, NetBSD, OpenBSD,
85OS X (Darwin), Plan 9, Solaris and Windows operating systems.
86The full set of supported combinations is listed in the discussion of
87<a href="#environment">environment variables</a> below.
88</p>
89
90</div>
91
Dan Willemsen6ff23252015-09-15 13:49:18 -070092<h2 id="go14">Install Go compiler binaries</h2>
Colin Cross7bb052a2015-02-03 12:59:37 -080093
94<p>
Dan Willemsen6ff23252015-09-15 13:49:18 -070095The Go tool chain is written in Go. To build it, you need a Go compiler installed.
96The scripts that do the initial build of the tools look for an existing Go tool
97chain in <code>$HOME/go1.4</code>.
98(This path may be overridden by setting the <code>GOROOT_BOOTSTRAP</code>
99environment variable.)
Colin Cross7bb052a2015-02-03 12:59:37 -0800100</p>
101
Dan Willemsen6ff23252015-09-15 13:49:18 -0700102<p>
103Build the tools with Go version 1.4 or a point release (1.4.1, 1.4.2 etc.).
104Go 1.4 binaries can be found at <a href="/dl/">the downloads page</a>.
105</p>
106
107<p>
108Download the zip or tarball of Go 1.4 for your platform and extract it to
109<code>$HOME/go1.4</code> (or your nominated <code>GOROOT_BOOTSTRAP</code>
110location).
111</p>
112
113<p>
114If you want to install Go 1.5 on a system that is not supported by Go 1.4 (such
115as <code>linux/ppc64</code>) you can either use
116<a href="/src/bootstrap.bash">bootstrap.bash</a> on a system that can bootstrap Go
1171.5 normally, or bootstrap with gccgo 5.
118</p>
119
120<p>
121When run as (for example)
122</p>
123
124<pre>
125$ GOOS=linux GOARCH=ppc64 ./bootstrap.bash
126</pre>
127
128<p>
129<code>bootstrap.bash</code> cross-compiles a toolchain for that <code>GOOS/GOARCH</code>
130combination, leaving the resulting tree in <code>../../go-${GOOS}-${GOARCH}-bootstrap</code>.
131That tree can be copied to a machine of the given target type
132and used as <code>GOROOT_BOOTSTRAP</code> to bootstrap a local build.
133</p>
134
135<p>
136To use gccgo, you need to arrange for <code>$GOROOT_BOOSTRAP/bin/go</code> to be
137the go tool that comes as part of gccgo 5. For example on Ubuntu Vivid:
138</p>
139
140<pre>
141$ sudo apt-get install gccgo-5
142$ sudo update-alternatives --set go /usr/bin/go-5
143$ GOROOT_BOOTSTRAP=/usr ./make.bash
144</pre>
145
Colin Cross7bb052a2015-02-03 12:59:37 -0800146<h2 id="git">Install Git, if needed</h2>
147
148<p>
149To perform the next step you must have Git installed. (Check that you
150have a <code>git</code> command before proceeding.)
151</p>
152
153<p>
154If you do not have a working Git installation,
155follow the instructions on the
156<a href="http://git-scm.com/downloads">Git downloads</a> page.
157</p>
158
159
160<h2 id="fetch">Fetch the repository</h2>
161
162<p>Go will install to a directory named <code>go</code>.
163Change to the directory that will be its parent
164and make sure the <code>go</code> directory does not exist.
165Then clone the repository and check out the latest release tag:</p>
166
167<pre>
168$ git clone https://go.googlesource.com/go
169$ cd go
Dan Willemsen6ff23252015-09-15 13:49:18 -0700170$ git checkout go1.5
Colin Cross7bb052a2015-02-03 12:59:37 -0800171</pre>
172
173<h2 id="head">(Optional) Switch to the master branch</h2>
174
175<p>If you intend to modify the go source code, and
176<a href="/doc/contribute.html">contribute your changes</a>
177to the project, then move your repository
178off the release branch, and onto the master (development) branch.
179Otherwise, skip this step.</p>
180
181<pre>
182$ git checkout master
183</pre>
184
185<h2 id="install">Install Go</h2>
186
187<p>
188To build the Go distribution, run
189</p>
190
191<pre>
Dan Willemsen6ff23252015-09-15 13:49:18 -0700192$ cd src
Colin Cross7bb052a2015-02-03 12:59:37 -0800193$ ./all.bash
194</pre>
195
196<p>
197(To build under Windows use <code>all.bat</code>.)
198</p>
199
200<p>
201If all goes well, it will finish by printing output like:
202</p>
203
204<pre>
205ALL TESTS PASSED
206
207---
208Installed Go for linux/amd64 in /home/you/go.
209Installed commands in /home/you/go/bin.
210*** You need to add /home/you/go/bin to your $PATH. ***
211</pre>
212
213<p>
214where the details on the last few lines reflect the operating system,
215architecture, and root directory used during the install.
216</p>
217
218<div class="detail">
219<p>
220For more information about ways to control the build, see the discussion of
221<a href="#environment">environment variables</a> below.
222<code>all.bash</code> (or <code>all.bat</code>) runs important tests for Go,
223which can take more time than simply building Go. If you do not want to run
224the test suite use <code>make.bash</code> (or <code>make.bat</code>)
225instead.
226</p>
227</div>
228
229
230<h2 id="testing">Testing your installation</h2>
231
232<p>
233Check that Go is installed correctly by building a simple program.
234</p>
235
236<p>
237Create a file named <code>hello.go</code> and put the following program in it:
238</p>
239
240<pre>
241package main
242
243import "fmt"
244
245func main() {
246 fmt.Printf("hello, world\n")
247}
248</pre>
249
250<p>
251Then run it with the <code>go</code> tool:
252</p>
253
254<pre>
255$ go run hello.go
256hello, world
257</pre>
258
259<p>
260If you see the "hello, world" message then Go is installed correctly.
261</p>
262
263<h2 id="gopath">Set up your work environment</h2>
264
265<p>
266You're almost done.
267You just need to do a little more setup.
268</p>
269
270<p>
271<a href="/doc/code.html" class="download" id="start">
272<span class="big">How to Write Go Code</span>
273<span class="desc">Learn how to set up and use the Go tools</span>
274</a>
275</p>
276
277<p>
278The <a href="/doc/code.html">How to Write Go Code</a> document
279provides <b>essential setup instructions</b> for using the Go tools.
280</p>
281
282
283<h2 id="tools">Install additional tools</h2>
284
285<p>
286The source code for several Go tools (including <a href="/cmd/godoc/">godoc</a>)
287is kept in <a href="https://golang.org/x/tools">the go.tools repository</a>.
288To install all of them, run the <code>go</code> <code>get</code> command:
289</p>
290
291<pre>
292$ go get golang.org/x/tools/cmd/...
293</pre>
294
295<p>
296Or if you just want to install a specific command (<code>godoc</code> in this case):
297</p>
298
299<pre>
300$ go get golang.org/x/tools/cmd/godoc
301</pre>
302
303<p>
304To install these tools, the <code>go</code> <code>get</code> command requires
305that <a href="#git">Git</a> be installed locally.
306</p>
307
308<p>
309You must also have a workspace (<code>GOPATH</code>) set up;
310see <a href="/doc/code.html">How to Write Go Code</a> for the details.
311</p>
312
313<p>
314<b>Note</b>: The <code>go</code> command will install the <code>godoc</code>
315binary to <code>$GOROOT/bin</code> (or <code>$GOBIN</code>) and the
316<code>cover</code> and <code>vet</code> binaries to
317<code>$GOROOT/pkg/tool/$GOOS_$GOARCH</code>.
318You can access the latter commands with
319"<code>go</code> <code>tool</code> <code>cover</code>" and
320"<code>go</code> <code>tool</code> <code>vet</code>".
321</p>
322
323<h2 id="community">Community resources</h2>
324
325<p>
326The usual community resources such as
327<code>#go-nuts</code> on the <a href="http://freenode.net/">Freenode</a> IRC server
328and the
329<a href="//groups.google.com/group/golang-nuts">Go Nuts</a>
330mailing list have active developers that can help you with problems
331with your installation or your development work.
332For those who wish to keep up to date,
333there is another mailing list, <a href="//groups.google.com/group/golang-checkins">golang-checkins</a>,
334that receives a message summarizing each checkin to the Go repository.
335</p>
336
337<p>
338Bugs can be reported using the <a href="//golang.org/issue/new">Go issue tracker</a>.
339</p>
340
341
342<h2 id="releases">Keeping up with releases</h2>
343
344<p>
345New releases are announced on the
346<a href="//groups.google.com/group/golang-announce">golang-announce</a>
347mailing list.
348Each announcement mentions the latest release tag, for instance,
Dan Willemsen6ff23252015-09-15 13:49:18 -0700349<code>go1.5</code>.
Colin Cross7bb052a2015-02-03 12:59:37 -0800350</p>
351
352<p>
353To update an existing tree to the latest release, you can run:
354</p>
355
356<pre>
357$ cd go/src
358$ git fetch
359$ git checkout <i>&lt;tag&gt;</i>
360$ ./all.bash
361</pre>
362
363Where <code>&lt;tag&gt;</code> is the version string of the release.
364
365
366<h2 id="environment">Optional environment variables</h2>
367
368<p>
369The Go compilation environment can be customized by environment variables.
370<i>None is required by the build</i>, but you may wish to set some
371to override the defaults.
372</p>
373
374<ul>
375<li><code>$GOROOT</code>
376<p>
377The root of the Go tree, often <code>$HOME/go</code>.
378Its value is built into the tree when it is compiled, and
379defaults to the parent of the directory where <code>all.bash</code> was run.
380There is no need to set this unless you want to switch between multiple
381local copies of the repository.
382</p>
383
384<li><code>$GOROOT_FINAL</code>
385<p>
386The value assumed by installed binaries and scripts when
387<code>$GOROOT</code> is not set explicitly.
388It defaults to the value of <code>$GOROOT</code>.
389If you want to build the Go tree in one location
390but move it elsewhere after the build, set
391<code>$GOROOT_FINAL</code> to the eventual location.
392</p>
393
394<li><code>$GOOS</code> and <code>$GOARCH</code>
395<p>
396The name of the target operating system and compilation architecture.
397These default to the values of <code>$GOHOSTOS</code> and
398<code>$GOHOSTARCH</code> respectively (described below).
399
400<p>
401Choices for <code>$GOOS</code> are
Dan Willemsen6ff23252015-09-15 13:49:18 -0700402<code>darwin</code> (Mac OS X 10.7 and above and iOS), <code>dragonfly</code>, <code>freebsd</code>,
403<code>linux</code>, <code>netbsd</code>, <code>openbsd</code>,
Colin Cross7bb052a2015-02-03 12:59:37 -0800404<code>plan9</code>, <code>solaris</code> and <code>windows</code>.
405Choices for <code>$GOARCH</code> are
406<code>amd64</code> (64-bit x86, the most mature port),
Dan Willemsen6ff23252015-09-15 13:49:18 -0700407<code>386</code> (32-bit x86), <code>arm</code> (32-bit ARM), <code>arm64</code> (64-bit ARM),
408<code>ppc64le</code> (PowerPC 64-bit, little-endian), and <code>ppc64</code> (PowerPC 64-bit, big-endian).
Colin Cross7bb052a2015-02-03 12:59:37 -0800409The valid combinations of <code>$GOOS</code> and <code>$GOARCH</code> are:
410<table cellpadding="0">
411<tr>
412<th width="50"></th><th align="left" width="100"><code>$GOOS</code></th> <th align="left" width="100"><code>$GOARCH</code></th>
413</tr>
414<tr>
415<td></td><td><code>darwin</code></td> <td><code>386</code></td>
416</tr>
417<tr>
418<td></td><td><code>darwin</code></td> <td><code>amd64</code></td>
419</tr>
420<tr>
Dan Willemsen6ff23252015-09-15 13:49:18 -0700421<td></td><td><code>darwin</code></td> <td><code>arm</code></td>
422</tr>
423<tr>
424<td></td><td><code>darwin</code></td> <td><code>arm64</code></td>
Colin Cross7bb052a2015-02-03 12:59:37 -0800425</tr>
426<tr>
427<td></td><td><code>dragonfly</code></td> <td><code>amd64</code></td>
428</tr>
429<tr>
430<td></td><td><code>freebsd</code></td> <td><code>386</code></td>
431</tr>
432<tr>
433<td></td><td><code>freebsd</code></td> <td><code>amd64</code></td>
434</tr>
435<tr>
436<td></td><td><code>freebsd</code></td> <td><code>arm</code></td>
437</tr>
438<tr>
439<td></td><td><code>linux</code></td> <td><code>386</code></td>
440</tr>
441<tr>
442<td></td><td><code>linux</code></td> <td><code>amd64</code></td>
443</tr>
444<tr>
445<td></td><td><code>linux</code></td> <td><code>arm</code></td>
446</tr>
447<tr>
Dan Willemsen6ff23252015-09-15 13:49:18 -0700448<td></td><td><code>linux</code></td> <td><code>arm64</code></td>
449</tr>
450<tr>
451<td></td><td><code>linux</code></td> <td><code>ppc64</code></td>
452</tr>
453<tr>
454<td></td><td><code>linux</code></td> <td><code>ppc64le</code></td>
455</tr>
456<tr>
Colin Cross7bb052a2015-02-03 12:59:37 -0800457<td></td><td><code>netbsd</code></td> <td><code>386</code></td>
458</tr>
459<tr>
460<td></td><td><code>netbsd</code></td> <td><code>amd64</code></td>
461</tr>
462<tr>
463<td></td><td><code>netbsd</code></td> <td><code>arm</code></td>
464</tr>
465<tr>
466<td></td><td><code>openbsd</code></td> <td><code>386</code></td>
467</tr>
468<tr>
469<td></td><td><code>openbsd</code></td> <td><code>amd64</code></td>
470</tr>
471<tr>
Dan Willemsen6ff23252015-09-15 13:49:18 -0700472<td></td><td><code>openbsd</code></td> <td><code>arm</code></td>
473</tr>
474<tr>
Colin Cross7bb052a2015-02-03 12:59:37 -0800475<td></td><td><code>plan9</code></td> <td><code>386</code></td>
476</tr>
477<tr>
478<td></td><td><code>plan9</code></td> <td><code>amd64</code></td>
479</tr>
480<tr>
481<td></td><td><code>solaris</code></td> <td><code>amd64</code></td>
482</tr>
483<tr>
484<td></td><td><code>windows</code></td> <td><code>386</code></td>
485</tr>
486<tr>
487<td></td><td><code>windows</code></td> <td><code>amd64</code></td>
488</tr>
489</table>
Dan Willemsen6ff23252015-09-15 13:49:18 -0700490<br>
Colin Cross7bb052a2015-02-03 12:59:37 -0800491
492<li><code>$GOHOSTOS</code> and <code>$GOHOSTARCH</code>
493<p>
494The name of the host operating system and compilation architecture.
495These default to the local system's operating system and
496architecture.
497</p>
498
499<p>
500Valid choices are the same as for <code>$GOOS</code> and
501<code>$GOARCH</code>, listed above.
502The specified values must be compatible with the local system.
503For example, you should not set <code>$GOHOSTARCH</code> to
504<code>arm</code> on an x86 system.
505</p>
506
507<li><code>$GOBIN</code>
508<p>
509The location where Go binaries will be installed.
510The default is <code>$GOROOT/bin</code>.
511After installing, you will want to arrange to add this
512directory to your <code>$PATH</code>, so you can use the tools.
513If <code>$GOBIN</code> is set, the <a href="/cmd/go">go command</a>
514installs all commands there.
515</p>
516
517<li><code>$GO386</code> (for <code>386</code> only, default is auto-detected
518if built on either <code>386</code> or <code>amd64</code>, <code>387</code> otherwise)
519<p>
Dan Willemsen6ff23252015-09-15 13:49:18 -0700520This controls the code generated by gc to use either the 387 floating-point unit
Colin Cross7bb052a2015-02-03 12:59:37 -0800521(set to <code>387</code>) or SSE2 instructions (set to <code>sse2</code>) for
522floating point computations.
523</p>
524<ul>
525 <li><code>GO386=387</code>: use x87 for floating point operations; should support all x86 chips (Pentium MMX or later).
526 <li><code>GO386=sse2</code>: use SSE2 for floating point operations; has better performance than 387, but only available on Pentium 4/Opteron/Athlon 64 or later.
527</ul>
528
529<li><code>$GOARM</code> (for <code>arm</code> only; default is auto-detected if building
530on the target processor, 6 if not)
531<p>
532This sets the ARM floating point co-processor architecture version the run-time
533should target. If you are compiling on the target system, its value will be auto-detected.
534</p>
535<ul>
536 <li><code>GOARM=5</code>: use software floating point; when CPU doesn't have VFP co-processor
537 <li><code>GOARM=6</code>: use VFPv1 only; default if cross compiling; usually ARM11 or better cores (VFPv2 or better is also supported)
538 <li><code>GOARM=7</code>: use VFPv3; usually Cortex-A cores
539</ul>
540<p>
541If in doubt, leave this variable unset, and adjust it if required
542when you first run the Go executable.
543The <a href="//golang.org/wiki/GoArm">GoARM</a> page
544on the <a href="//golang.org/wiki">Go community wiki</a>
545contains further details regarding Go's ARM support.
546</p>
547
548</ul>
549
550<p>
551Note that <code>$GOARCH</code> and <code>$GOOS</code> identify the
552<em>target</em> environment, not the environment you are running on.
553In effect, you are always cross-compiling.
554By architecture, we mean the kind of binaries
555that the target environment can run:
556an x86-64 system running a 32-bit-only operating system
557must set <code>GOARCH</code> to <code>386</code>,
558not <code>amd64</code>.
559</p>
560
561<p>
562If you choose to override the defaults,
563set these variables in your shell profile (<code>$HOME/.bashrc</code>,
564<code>$HOME/.profile</code>, or equivalent). The settings might look
565something like this:
566</p>
567
568<pre>
569export GOROOT=$HOME/go
570export GOARCH=amd64
571export GOOS=linux
572</pre>
573
574<p>
575although, to reiterate, none of these variables needs to be set to build,
576install, and develop the Go tree.
577</p>