Install Go 1.11.5 in Ubuntu 18

This article describes how to install Go 1.11.5 in Ubuntu 18. Go is an open source programming language that is quickly growing in popularity. You can discover more about Go at the official website: https://golang.org/

If you prefer to use snap to install Go please refer my other article Install Go in Ubuntu 18 using snap.

How to install Go 1.11.5 in Ubuntu 18

Go 1.11.5 is the latest stable version as of the time of writing and was released on 23 January 2019.

  1. In Ubuntu, open your browser and navigate to https://golang.org/dl/

    This page provides download links for the latest stable versions of Go 1.10 and 1.11.

    Download the latest Linux version, which at the time of writing is go1.11.5.linux-amd64.tar.gz.

  2. Once the download has finished, open a terminal and change into the directory containing the downloaded file (e.g. /Downloads)

    cd Downloads
    
  3. Extract the archive into /usr/local with the following command:

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

    This will extract go into /usr/local/go.

    /usr/local/go file list

  4. Open a terminal and change into the /usr/local/go/bin directory.

    From this directory you can execute the go binary, for example ./go version will print the version details.

    cd /usr/local/go/bin
    ./go version
    

    go version command

    But this is of limited use. To easily run go outside this directory, you need to update the PATH environmental variable.

  5. Add /usr/local/go/bin to the PATH environment variable by editing the ~/.profile file.

    gedit ~/.profile &
    

    Add the following to the end of the file on a new line:

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

    Save the changes and close the file.

    Editing ~/.profile

  6. Generally you need to log out and back in before any changes to ~/.profile take affect. Alternatively, you can avoid this and force a reload with the following command:

    . ~/.profile
    

    You should then be able to run go from any location. Try this out by printing the version details again:

    go version
    

    go version

GOPATH

The GOPATH environmental variable specifies the path of your go workspace. The workspace is a directory where you organise your project source files and binaries. By default it will be set to $HOME/go. For example, mine is /home/codeooze/go.

You can check what GOPATH is currently set to by printing the go environment information with the following command:

go env

go env command

You can read more about workspaces in the Go documentation: https://golang.org/doc/code.html#Workspaces

If you would like to use a different directory for your workspace, simply edit ~/.profile again and set the GOPATH variable. For example, to use $HOME/projects/go add the following line:

export GOPATH=$HOME/projects/go

Save the file and re-load the profile so the changes take affect. Then verify the GOPATH is updated by printing the go environment information again:

. ~/.profile
go env

Setting GOPATH

GOROOT

The GOROOT variable is the location where the Go package is installed on your system. If you’ve followed the above install steps then you will have extracted the package into /usr/local/go. This is the default setting for GOROOT, so you shouldn’t need to change it. If you extracted Go into a different location, simply set the GOROOT variable using the same method described above for GOPATH.

Writing your first Go program

This section provides steps to write and run the traditional “Hello, world” program in Go.

  1. Open a terminal and change into your Go workspace directory. By default this is $HOME/go

    Note: the $HOME/go directory will not exist, so you will need to create it.

    mkdir $HOME/go
    cd $HOME/go
    
  2. Create the src directory and change into it. This is where your go source files will be kept.

    mkdir src
    cd src
    
  3. Typically each program you write in Go will be organised into a separate directory. Create a new directory to store the “Hello, world” program, then change into it.

    mkdir hello
    cd hello
    

    You should now have the following folder structure within your workspace: src/hello.

  4. Use a text editor to create a new file named hello.go. This is where the source code will be written. I’ll be using gedit again:

    gedit hello.go &
    

    Now paste the below and save the file:

    package main
    import "fmt"
        
    func main() {
        fmt.Printf("hello, world\n")
    }
    
  5. In the terminal, use go to build the source file.

    This command should be run in the same directory as hello.go exists.

    go build
    

    If all was successful, you will now have an executable binary named hello.

    go build command

  6. Run the hello binary and you should see “hello, world” printed out:

    ./hello
    

    hello world

Installing Go Commands

The workspace /bin directory is a convenient place to store compiled Go binaries, called Commands. It’s default location is $HOME/go/bin.

For ease of access, add this to your PATH environment variable by editing ~/.profile. For example:

gedit ~/.profile &

Add the following on a new line at the end of the file:

export PATH=$PATH:$HOME/go/bin

Then re-load the profile so the changes take affect immediately:

. ~/.profile

You can now install the compiled hello binary into your workspace /bin directory. This will allow you to run it from any location.

Ensure you are in the /src/hello directory and enter the command:

go install

This will create the [workspace]/bin directory if it dosn’t exist, and place the hello binary there. Now try out the hello command:

hello

hello world

Conclusion

This concludes my article on installing Go 1.11.5 in Ubuntu 18. I hope you have enjoyed it.

I encourage you to browse the Go documentation to learn more about this programming language: https://golang.org/doc/