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