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