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