^_^

2010年1月29日星期五

Ubuntu 下 Go 的安装过程

GCC 宣布对 Google Go 语言的支持,自己也来装个 Go 环境玩玩:
  1. 添加 Go 的环境变量到文件 .bashrc 中,并重新登录:
    export GOROOT=$HOME/go
    export GOBIN=$HOME/gobin
    export GOARCH=amd64
    export GOOS=linux
    export PATH=$GOBIN:$PATH
    验证环境变量是否成功加入:
    root001@ubuntu001:~$ env | grep '^GO'
    GOARCH=amd64
    GOBIN=/home/root001/gobin
    GOROOT=/home/root001/go
    GOOS=linux
  2. 安装源码管理客户端:
    root001@ubuntu001:~$ sudo apt-get install python-setuptools
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following NEW packages will be installed:
      python-setuptools
    0 upgraded, 1 newly installed, 0 to remove and 18 not upgraded.
    Need to get 195kB of archives.
    After this operation, 909kB of additional disk space will be used.
    Get:1 http://mirrors.163.com karmic/main python-setuptools 0.6c9-0ubuntu5 [195kB                                                                                                                               ]
    Fetched 195kB in 3s (57.0kB/s)
    Selecting previously deselected package python-setuptools.
    (Reading database ... 47300 files and directories currently installed.)
    Unpacking python-setuptools (from .../python-setuptools_0.6c9-0ubuntu5_all.deb)                                                                                                                                ...
    Setting up python-setuptools (0.6c9-0ubuntu5) ...
    root001@ubuntu001:~$ sudo apt-get install python-dev
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following extra packages will be installed:
      python2.6-dev
    The following NEW packages will be installed:
      python-dev python2.6-dev
    0 upgraded, 2 newly installed, 0 to remove and 32 not upgraded.
    Need to get 4,706kB of archives.
    After this operation, 14.8MB of additional disk space will be used.
    Do you want to continue [Y/n]? y
    Get:1 http://mirrors.163.com karmic-updates/main python2.6-dev 2.6.4-0ubuntu3 [4,705kB]
    Get:2 http://mirrors.163.com karmic-updates/main python-dev 2.6.4-0ubuntu1 [976B]
    Fetched 4,706kB in 2min 9s (36.5kB/s)
    Selecting previously deselected package python2.6-dev.
    (Reading database ... 47451 files and directories currently installed.)
    Unpacking python2.6-dev (from .../python2.6-dev_2.6.4-0ubuntu3_amd64.deb) ...
    Selecting previously deselected package python-dev.
    Unpacking python-dev (from .../python-dev_2.6.4-0ubuntu1_all.deb) ...
    Setting up python2.6-dev (2.6.4-0ubuntu3) ...
    Setting up python-dev (2.6.4-0ubuntu1) ...
    root001@ubuntu001:~$ sudo easy_install mercurial
    Searching for mercurial
    Reading http://pypi.python.org/simple/mercurial/
    Reading http://www.selenic.com/mercurial
    Best match: mercurial 1.4.2
    Downloading http://mercurial.selenic.com/release/mercurial-1.4.2.tar.gz
    Processing mercurial-1.4.2.tar.gz
    Running mercurial-1.4.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-j8YMBi/mercurial-1.4.2/egg-dist-tmp-SPCtzN
    zip_safe flag not set; analyzing archive contents...
    mercurial.lsprof: module references __file__
    mercurial.extensions: module references __file__
    mercurial.help: module references __file__
    mercurial.templater: module references __file__
    mercurial.i18n: module references __file__
    Adding mercurial 1.4.2 to easy-install.pth file
    Installing hg script to /usr/local/bin
    
    Installed /usr/local/lib/python2.6/dist-packages/mercurial-1.4.2-py2.6-linux-x86_64.egg
    Processing dependencies for mercurial
    Finished processing dependencies for mercurial
  3. 下载源码:
    root001@ubuntu001:~$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT
    requesting all changes
    adding changesets
    adding manifests
    adding file changes
    
    added 4712 changesets with 19773 changes to 3235 files
    updating to branch default
    1910 files updated, 0 files merged, 0 files removed, 0 files unresolved
  4. 安装依赖库及编译 Go:
    root001@ubuntu001:~$ sudo apt-get install bison gcc libc6-dev ed gawk make
    [sudo] password for root001:
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    gcc is already the newest version.
    gcc set to manually installed.
    libc6-dev is already the newest version.
    libc6-dev set to manually installed.
    ed is already the newest version.
    make is already the newest version.
    Suggested packages:
      bison-doc
    The following NEW packages will be installed:
      bison gawk
    0 upgraded, 2 newly installed, 0 to remove and 18 not upgraded.
    Need to get 823kB of archives.
    After this operation, 3,793kB of additional disk space will be used.
    Get:1 http://mirrors.163.com karmic/main gawk 1:3.1.6.dfsg-0ubuntu2 [549kB]
    Get:2 http://mirrors.163.com karmic/main bison 1:2.4.1.dfsg-1 [274kB]
    Fetched 823kB in 17s (47.4kB/s)
    Selecting previously deselected package gawk.
    (Reading database ... 47565 files and directories currently installed.)
    Unpacking gawk (from .../gawk_1%3a3.1.6.dfsg-0ubuntu2_amd64.deb) ...
    Selecting previously deselected package bison.
    Unpacking bison (from .../bison_1%3a2.4.1.dfsg-1_amd64.deb) ...
    Processing triggers for install-info ...
    Processing triggers for man-db ...
    Setting up gawk (1:3.1.6.dfsg-0ubuntu2) ...
    
    Setting up bison (1:2.4.1.dfsg-1) ...
    update-alternatives: using /usr/bin/bison.yacc to provide /usr/bin/yacc (yacc) in auto mode.
    update-alternatives: warning: not replacing /usr/share/man/man1/yacc.1.gz with a link.
    root001@ubuntu001:~$ mkdir $GOBIN
    root001@ubuntu001:~/go/src$ cd $GOROOT/src
    root001@ubuntu001:~/go/src$ ./all.bash
    rm -f *.o *.6 6.out lib9.a
    rm -f bbuffered.o bfildes.o bflush.o bgetc.o bgetrune.o bgetd.o binit.o boffset.o bprint.o bputc.o bputrune.o brdline.o brdstr.o bread.o bseek.o bwrite.o  *.6 6.out libbio.a
    ...
    --- cd ../test
    2 known bugs; 0 unexpected bugs
    
    我编译 Go 的过程中出错了,在 making libcgo 的时候,错误如下:
    %%%% making libcgo %%%%
    
    gcc -m32 -O2 -fPIC -o linux_386.o -c linux_386.c
    gcc -m32 -O2 -fPIC -o 386.o -c 386.S
    gcc -m32 -O2 -fPIC -o util.o -c util.c
    In file included from /usr/include/features.h:378In file included from /usr/include/features.h:378,
                     from /usr/include/stdint.h:26,
                     from /usr/include/pthread.h:23,
                     from libcgo.h:5,
                     from linux_386.c:5,
                     from util.c:5:
    :
    /usr/include/gnu/stubs.h:7:27:/usr/include/gnu/stubs.h:7:27:  error: error: gnu/stubs-32.h: No such file or directorygnu/stubs-32.h: No such file or directory
    
    make: *** [util.o] Error 1
    make: *** Waiting for unfinished jobs....
    make: *** [linux_386.o] Error 1
    但是在 $GOBIN 下已经成功生成 8g/8l,可以一用,我也懒得折腾了。有达人知道如何解决,请不吝告知,thanks。

    上面的问题,在我找到组织(QQ 群 102319854)后,终于搞清楚了。原来是我弄错了架构(通过“cat /proc/cpuinfo”、“uname -a”查看确认),我是 64 位系统,并非 386,所以只需要第一步环境变量 GOARCH 从 386 改成 amd64 即可,^_^。
    root001@ubuntu001:~/go/src$  ls $GOBIN
    6a  6cov  6l   6prof  8c  8l   ebnflint  godoc  gomake  gotest  hgpatch
    6c  6g    6nm  8a     8g  cgo  godefs    gofmt  gopack  goyacc  quietgcc
  5. 测试一下吧。
    root001@ubuntu001:~/foolbear$ cat hello.go
    package main
    
    import "fmt"
    
    func main() {
            fmt.Printf("hello, world\n")
    }
    root001@ubuntu001:~/foolbear$ 6g hello.go
    root001@ubuntu001:~/foolbear$ 6l hello.6
    root001@ubuntu001:~/foolbear$ ls
    6.out  hello.6  hello.go
    root001@ubuntu001:~/foolbear$ ./6.out
    hello, world
FYI:官方《Installing Go
本站文章除注明外,均为本站原创
转载请注明文章转载自: 大笨熊乐园 [ https://blog.foolbear.com/?m=0 ]
文章标题: Ubuntu 下 Go 的安装过程
文章地址: https://blog.foolbear.com/2010/01/ubuntu-go.html?m=0

1 条评论 :

发表评论

Related Posts with Thumbnails