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