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