Ubuntu is one of the most popular operating systems for VPS servers. I have been working on creating a few Go scripts for my projects. This tutorial will teach us how to install Go on an Ubuntu VPS server. If you are not tech-savvy, you can always go with a managed VPS server.
Table of Contents
Using apt to install Go on Ubuntu
The simple solution is to use the apt command to install Go. If your system doesn’t have Golang already installed, running the “go” command will show you the apt command to install Golang. It will also show the version that will be installed.
root@localhost:~# go
Command 'go' not found, but can be installed with:
snap install go # version 1.15.6, or
apt install golang-go # version 2:1.13~1ubuntu2
apt install gccgo-go # version 2:1.13~1ubuntu2
See 'snap info go' for additional versions.
root@localhost:~#
Now, run the following command to install the Golang.
root@localhost:~# apt install golang-go
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-5.4.0-42 linux-headers-5.4.0-42-generic linux-headers-5.4.0-48 linux-headers-5.4.0-48-generic linux-image-5.4.0-42-generic linux-image-5.4.0-48-generic
linux-modules-5.4.0-42-generic linux-modules-5.4.0-48-generic linux-modules-extra-5.4.0-42-generic linux-modules-extra-5.4.0-48-generic
...
...
Setting up golang-1.13-go (1.13.8-1ubuntu1.1) ...
Setting up golang-src (2:1.13~1ubuntu2) ...
Setting up golang-race-detector-runtime (2:1.13~1ubuntu2) ...
Setting up golang-go (2:1.13~1ubuntu2) ...
Processing triggers for man-db (2.9.1-1) ...
root@localhost:~#
Now, run the “go version” command to check if the installation was successful.
root@localhost:~# go version
go version go1.13.8 linux/amd64
root@localhost:~#
Installing the latest Golang on Ubuntu
If you notice the above installation, it’s go1.13.8, released in 2020. So, it’s an ancient version. You should install the latest version manually.
Downloading the latest Go binaries
The first step is to go to the Golang downloads page and get the link to the latest Linux binary file. We can use wget command to download it.
root@localhost:~# wget https://go.dev/dl/go1.20.linux-amd64.tar.gz
--2023-02-13 15:43:17-- https://go.dev/dl/go1.20.linux-amd64.tar.gz
Resolving go.dev (go.dev)... 2001:4860:4802:34::15, 2001:4860:4802:38::15, 2001:4860:4802:32::15, ...
Connecting to go.dev (go.dev)|2001:4860:4802:34::15|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://dl.google.com/go/go1.20.linux-amd64.tar.gz [following]
--2023-02-13 15:43:17-- https://dl.google.com/go/go1.20.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 2404:6800:4009:821::200e, 142.250.183.46
Connecting to dl.google.com (dl.google.com)|2404:6800:4009:821::200e|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 99869470 (95M) [application/x-gzip]
Saving to: ‘go1.20.linux-amd64.tar.gz’
go1.20.linux-amd64.tar.gz 100%[=====================================================================================================>] 95.24M 170MB/s in 0.6s
2023-02-13 15:43:18 (170 MB/s) - ‘go1.20.linux-amd64.tar.gz’ saved [99869470/99869470]
root@localhost:~#
Untar the binaries
Then, untar the file at a convenient location. For this tutorial, I am keeping it in the /root directory but it’s not recommended for production usage.
root@localhost:~# tar -xvf go1.20.linux-amd64.tar.gz
You will notice a new directory named “go” in the current directory. Go to the bin folder and check if the downloaded binaries are working fine or not.
root@localhost:~# cd go/bin/
root@localhost:~/go/bin# pwd
/root/go/bin
root@localhost:~/go/bin# ./go version
go version go1.20 linux/amd64
root@localhost:~/go/bin#
Setting the PATH Variable
The final step is to add the go binaries location in the PATH variable so that it’s used automatically in all the go programs. Open ~/.profile file using your favorite editor and add the following lines to it.
export PATH=/root/go/bin:$PATH
Then use the source command to install the profile changes.
root@localhost:~# source .profile
Now, test the go version again. It should show the latest version.
root@localhost:~# go version
go version go1.20 linux/amd64
root@localhost:~#
Conclusion
We learned about two ways to install Golang. The second way is recommended so that you work with the latest version.