golang

The installation process simply involves a clone of a GitHub repo and a single line in your .bashrc.

1. Clone the Repo and Add to User Directory

1

bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

This command uses cURL to grab the GitHub repo and install it within your user directory. The file that this repo is placed in is ~/.gvm.

2. Open Your ~/.bashrc and Source the GVM Directory

1

[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"

All we are doing here is linking the gvm binary to our environmental variables. This allows you to run GVM from any path on your command line.

3. Logout and Login with Your User

The easiest way to make the changes in your .bashrc take effect is to just log out and log back into your shell.

4. Check to Make Sure that GVM is Installed

1

2

$ gvm version

Go Version Manager v1.0.22 installed at /home/myuser/.gvm

This command will tell you which version of GVM is installed. If it reports a version back, then you have successfully installed GVM. Well done!

5. Install Go (Golang)

First let’s check the versions of Go that are available.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

$ gvm listall

...

go1.2rc5

go1.3

go1.3.1

go1.3.2

go1.3.3

go1.3beta1

go1.3beta2

go1.3rc1

go1.3rc2

go1.4

go1.4beta1

go1.4rc1

go1.4rc2

As you can see, GVM includes the beta versions of software and release candidates (rc).

Let’s install Go 1.4.

1

2

3

4

$ gvm install go1.4

Downloading Go source...

Installing go1.4...

* Compiling...

6. Tell GVM Which Version of Go to Use

Now that we have Go installed on our system, let’s let GVM know we wish to use that version.

1

2

$ gvm use go1.4

Now using version go1.4

Sweet. Everything is going great so far.

7. Verify Go Is Installed Correctly

1

2

$ go version

go version go1.4 linux/amd64

We are done! GVM makes this process simple, and upgrading, changing versions, and uninstalling Go is now simple as well.

Create a directory to contain your workspace, $HOME/work for example, and set the GOPATH environment variable to point to that location. in .bashrc file

$ export GOPATH=$HOME/work

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

$ export PATH=$PATH:$GOPATH/bin

You should put the above command in your shell startup script ($HOME/.profile for example). On Windows, follow the instructions above to set the GOPATH environment variable on your system.

Next, make the directories src/github.com/user/hello inside your workspace (if you use GitHub, substitute your user name for user), and inside the hello directory create a file named hello.go with the following contents:

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

Then compile it with the go tool:

$ go install github.com/user/hello

or

$ cd $GOPATH/src/github.com/user/hello $ go install

The command above will put an executable command named hello (or hello.exe) inside the bin directory of your workspace. Execute the command to see the greeting:

$ $GOPATH/bin/hello hello, world

Update: Installing Go 1.5 Might Take an Additional Step

Go has changed how they compile their source and it has caused some issues with gvm. This is reported as fixed but I figured we should mention it just in case. Basically, just install go1.4 before you install go1.5.

1

2

3

4

5

gvm install go1.4

gvm use go1.4

export GOROOT_BOOTSTRAP=$GOROOT

gvm install go1.5