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