在Ubuntu上正确安装Go遇到了问题。

huangapple go评论95阅读模式
英文:

Trouble installing Go correctly on Ubuntu

问题

我在我的Mac上成功安装了这个,但在Linux上遇到了问题。我正在按照他们的文档https://golang.org/doc/install进行操作。我正在运行一个64位的机器,所以我下载了64位的存档。下载完成后,我运行以下命令:

sudo tar -C /usr/local/ -xzf ~/Downloads/go1.4.2.linux-amd64.tar.gz

我在我的主文件夹中创建了一个名为go的目录。我的目录结构如下:

/home
--/chrism
----/go
------/src
------/pkg
------/bin

然后我将以下内容添加到/etc/profile中。保存后,我运行source /etc/profile

export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go

src/目录下,我添加了一个名为git.mycompany.com的目录,然后在其中又创建了一个名为test的目录。在test/目录下,我创建了test.go文件,并将教程中的代码块粘贴进去:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

当我运行go run test.go时,输出如下:

chrism@ubuntu:~/go/src/git.mycompany.com/test$ go run test.go 
# fmt
Usage: pack op file.a [name....]
Where op is one of cprtx optionally followed by v for verbose output.
For compatibility with old Go build environments the op string grc is
accepted as a synonym for c.

For more information, run
    godoc cmd/pack
# runtime
Usage: pack op file.a [name....]
Where op is one of cprtx optionally followed by v for verbose output.
For compatibility with old Go build environments the op string grc is
accepted as a synonym for c.

For more information, run
    godoc cmd/pack

如果我在导入语句中添加更多的包,它还会输出所有这些包的文档。

编辑0:
我还尝试使用apt-get进行安装。我卸载并删除了之前的更改,然后进行了安装。运行时出现以下结果:

chrism@ubuntu:~/go/src/git.mycompany.com/test$ go run test.go 
go build fmt: exec: "/usr/local/go/pkg/tool/linux_amd64/pack": stat /usr/local/go/pkg/tool/linux_amd64/pack: no such file or directory
go build runtime: exec: "/usr/local/go/pkg/tool/linux_amd64/pack": stat /usr/local/go/pkg/tool/linux_amd64/pack: no such file or directory

编辑1:
这是运行go env的输出:

GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="amd64"
GOCHAR="6"
GOOS="linux"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CGO_ENABLED="1"
英文:

I successfully installed this on my Mac, just having trouble on Linux. I'm following along their doc https://golang.org/doc/install. I'm running a 64bit machine so I downloaded the 64bit archive. Once downloaded I run

sudo tar -C /usr/local/ -xzf ~/Downloads/go1.4.2.linux-amd64.tar.gz

I created a go directory in my home folder. I have the structure

/home
--/chrism
----/go
------/src
------/pkg
------/bin

And I add the following to /etc/profile. After saving I run source /etc/profile

export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go

In src/ I added a directory git.mycompany.com and in their another directory called test. In test/ I made test.go and pasted the block of code from the tutorial above in

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

When I run go run test.go, it outputs the following

chrism@ubuntu:~/go/src/git.mycompany.com/test$ go run test.go 
# fmt
Usage: pack op file.a [name....]
Where op is one of cprtx optionally followed by v for verbose output.
For compatibility with old Go build environments the op string grc is
accepted as a synonym for c.

For more information, run
    godoc cmd/pack
# runtime
Usage: pack op file.a [name....]
Where op is one of cprtx optionally followed by v for verbose output.
For compatibility with old Go build environments the op string grc is
accepted as a synonym for c.

For more information, run
    godoc cmd/pack

If I add more packages to my import statement, it'll output the documentation for all those packages as well.

EDIT 0:
I also tried to install with apt-get. I uninstalled and removed my previous changes and then installed. This resulted in the following when running:

chrism@ubuntu:~/go/src/git.mycompany.com/test$ go run test.go 
go build fmt: exec: "/usr/local/go/pkg/tool/linux_amd64/pack": stat /usr/local/go/pkg/tool/linux_amd64/pack: no such file or directory
go build runtime: exec: "/usr/local/go/pkg/tool/linux_amd64/pack": stat /usr/local/go/pkg/tool/linux_amd64/pack: no such file or directory

EDIT 1:
This is the output of running go env

GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="amd64"
GOCHAR="6"
GOOS="linux"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CGO_ENABLED="1"

答案1

得分: 13

以下是我在Ubuntu系统中安装Go的步骤:

简短版本:

  1. 运行以下命令进行安装:

    sudo apt-get remove -y gccgo-go && wget http://golang.org/dl/go1.8.linux-amd64.tar.gz && sudo apt-get -y install gcc && sudo tar -C /usr/local -xzf go1.8.2.linux-amd64.tar.gz && echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc

> **注意:**更改版本号以安装特定版本的Go。例如,要安装1.9而不是1.8,请将文件名更改为go1.9.linux-amd64.tar.gz。最新的Go发行版始终可以在官方Go下载页面找到。

  1. 设置工作区。(第6步)

详细版本:

  1. 这里下载二进制发行版。对于Ubuntu,请使用go x.x.x.linux-amd64.tar.gz。
    对于版本1.4.2,您可以在终端中输入以下命令。

     wget http://golang.org/dl/go1.8.linux-amd64.tar.gz
    
  2. 安装gcc以支持cgo。

     sudo apt-get install gcc 
    
  3. 在/usr/local中解压tarball。它应该创建一个go目录。

     tar -C /usr/local -xzf go1.8.linux-amd64.tar.gz
    

    (通常这些命令必须以root或通过sudo运行。)

  4. 将/usr/local/go/bin添加到PATH环境变量中。

     gksu gedit ~/.bashrc
    
  5. 在文件末尾添加以下行。

     export PATH=$PATH:/usr/local/go/bin
    
  6. 设置工作区

    a. 在您首选的位置创建一个名为go的工作区目录(除非您使用默认的go安装位置)。我使用的是/home/vembu/work/projects/go。

     mkdir -p /home/vembu/work/projects/go
    

    b. 导出GOPATH

     gedit ~/.bashrc
    

    c. 在倒数第二行添加以下行。

     export GOPATH=/home/vembu/work/projects/go
    

    d. 为方便起见,将工作区的bin子目录添加到您的PATH中:

     export PATH=/usr/local/go/bin:$PATH:$GOPATH/bin
    

    e. 最后,.bashrc的最后两行应该是这样的。

     export GOPATH=/home/vembu/work/projects/go
     export PATH=/usr/local/go/bin:$GOPATH/bin:$PATH
    

    f. 重新启动终端。

英文:

These are the steps I followed for installing Go in my Ubuntu system :

Short Version :

  1. Run following commands for installation:

    sudo apt-get remove -y gccgo-go && wget http://golang.org/dl/go1.8.linux-amd64.tar.gz && sudo apt-get -y install gcc && sudo tar -C /usr/local -xzf go1.8.2.linux-amd64.tar.gz && echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc

> Note: Changes the version number to install a specific version of Go. For example, to install 1.9 instead of 1.8 change the file name to go1.9.linux-amd64.tar.gz. The latest Go distributions can always be found at the official Go downloads page

  1. Setup workplace. (Point 6)

Long Version:

  1. Download the binary release from here. Use go x.x.x.linux-amd64.tar.gz for ubuntu.
    For version 1.4.2 you can type following in terminal.

     wget http://golang.org/dl/go1.8.linux-amd64.tar.gz
    
  2. Install gcc for cgo

     sudo apt-get install gcc 
    
  3. Extract the tarball in /usr/local. It should create a go directory.

     tar -C /usr/local -xzf go1.8.linux-amd64.tar.gz
    

    (Typically these commands must be run as root or through sudo.)

  4. Add /usr/local/go/bin to the PATH environment variable.

     gksu gedit ~/.bashrc
    
  5. Add following line in the end of file

     export PATH=$PATH:/usr/local/go/bin
    
  6. Setup Workspace

    a. Create a workspace directory name go in your preferred location (unless you are using the default go installation location). I am using /home/vembu/work/projects/go

     mkdir -p /home/vembu/work/projects/go
    

    b. Export GOPATH

     gedit ~/.bashrc
    

    c. Add following line in the second last line

     export GOPATH=/home/vembu/work/projects/go
    

    d. For convenience, add the workspace's bin subdirectory to your PATH:

     export PATH=/usr/local/go/bin:$PATH:$GOPATH/bin
    

    e. Finally .bashrc’s last two line should look like this

     export GOPATH=/home/vembu/work/projects/go
     export PATH=/usr/local/go/bin:$GOPATH/bin:$PATH
    

    f. Restart terminal.

答案2

得分: 10

这是我通常在Ubuntu机器上安装的方法,从未失败过。希望对你有帮助。

  1. 下载Go语言

    wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz

  2. 解压到/usr/local目录

    sudo tar -C /usr/local -xzf go1.4.2.linux-amd64.tar.gz

  3. 添加以下内容到~/.bashrc文件中

    export PATH=$PATH:/usr/local/go/bin

    export GOPATH=$HOME/go

    export GOBIN=$GOPATH/bin

    export PATH=$PATH:$GOPATH/bin

  4. 保存并重新加载资源

    source ~/.bashrc

  5. 验证安装是否成功

    go env

  6. 创建go目录

    mkdir ~/go

  7. 尝试获取一些包

    go get github.com/smartystreets/goconvey

编辑

我创建了一个Bash脚本,可以自动执行上述步骤。它总是指向最新的Go版本,所以请小心使用。

wget -O - https://raw.githubusercontent.com/mauleyzaola/scripts/master/go/go.install.sh | sh

然后,重新加载.bashrc文件

source ~/.bashrc

英文:

This method is how I usually install on ubuntu machines and never fails. Hope it helps you.

  1. download go

    wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz

  2. extract into /usr/local

    sudo tar -C /usr/local -xzf go1.4.2.linux-amd64.tar.gz

  3. add these lines to modify ~/.bashrc

    export PATH=$PATH:/usr/local/go/bin

    export GOPATH=$HOME/go

    export GOBIN=$GOPATH/bin

    export PATH=$PATH:$GOPATH/bin

  4. save and reload sources

    source ~/.bashrc

  5. verify installation

    go env

  6. Create go directory

    mkdir ~/go

  7. Try getting some package

    go get github.com/smartystreets/goconvey

EDIT

I have created a bash script which does the above automatically for you. It always points to the latest go version, so be careful with that.

wget -O - https://raw.githubusercontent.com/mauleyzaola/scripts/master/go/go.install.sh | sh

Then, just reload .bashrc

source ~/.bashrc

答案3

得分: 2

你选择了系统范围的安装,所以你可以从/etc/profile中删除这行代码:export GOPATH=$HOME/go,然后再次执行source命令来加载这个文件。你提到的其他步骤都是不必要的。而且,与之前的回答中提到的重新启动终端不同,你需要重新启动你的机器。所以最后你需要做的就是仔细按照官方文档中提到的步骤进行操作。

英文:

You have chosen a system wide installation, so you could remove this line: export GOPATH=$HOME/go from /etc/profile source again this later file. All other steps you mentioned are not necessary at all. And instead of restarting Terminal as mentioned by a previous answer, you will need to reboot your machine. So by the end all what you have to do is to carefully follow the steps mentioned in the official documentation itself.

huangapple
  • 本文由 发表于 2015年7月18日 04:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/31484085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定