Friday, 21 August 2020

How to upgrade Go on Ubuntu

I had oldish version (1.12.1) of Go on my Ubuntu 18.04 and I wanted to update it to the most recent one (1.15). 



This was my old version:

$ go version
go version go1.12.1 linux/amd64

It was using symlink pointing to my previous go installation which was in /var/lib/go:

$ which go /usr/local/bin/go $ ls -la /usr/local/bin/go lrwxrwxrwx 1 root root 18 Apr 1 2019 /usr/local/bin/go -> /var/lib/go/bin/go $ ls -la /var/lib/go/bin/go -rwxr-xr-x 1 root root 14609408 Mar 14 2019 /var/lib/go/bin/go $ ls -la /var/lib/go total 216 drwxr-xr-x 10 root root 4096 Mar 14 2019 . drwxr-xr-x 78 root root 4096 May 26 10:02 .. drwxr-xr-x 2 root root 4096 Mar 14 2019 api -rw-r--r-- 1 root root 55358 Mar 14 2019 AUTHORS drwxr-xr-x 2 root root 4096 Mar 14 2019 bin -rw-r--r-- 1 root root 1339 Mar 14 2019 CONTRIBUTING.md -rw-r--r-- 1 root root 78132 Mar 14 2019 CONTRIBUTORS drwxr-xr-x 8 root root 4096 Mar 14 2019 doc -rw-r--r-- 1 root root 5686 Mar 14 2019 favicon.ico drwxr-xr-x 3 root root 4096 Mar 14 2019 lib -rw-r--r-- 1 root root 1479 Mar 14 2019 LICENSE drwxr-xr-x 13 root root 4096 Mar 14 2019 misc -rw-r--r-- 1 root root 1303 Mar 14 2019 PATENTS drwxr-xr-x 6 root root 4096 Mar 14 2019 pkg -rw-r--r-- 1 root root 1607 Mar 14 2019 README.md -rw-r--r-- 1 root root 26 Mar 14 2019 robots.txt drwxr-xr-x 46 root root 4096 Mar 14 2019 src drwxr-xr-x 21 root root 12288 Mar 14 2019 test -rw-r--r-- 1 root root 8 Mar 14 2019 VERSION

I then removed the old installation:

$ sudo rm -rf /var/lib/go/

...and downloaded and unpacked the archive containing the latest version at recommended locaton (/usr/local):

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

$ ls -la /usr/local/go
total 236
drwxr-xr-x 10 root root  4096 Aug 11 20:16 .
drwxr-xr-x 12 root root  4096 Aug 21 13:14 ..
drwxr-xr-x  2 root root  4096 Aug 11 20:16 api
-rw-r--r--  1 root root 55669 Aug 11 20:16 AUTHORS
drwxr-xr-x  2 root root  4096 Aug 11 20:18 bin
-rw-r--r--  1 root root  1339 Aug 11 20:16 CONTRIBUTING.md
-rw-r--r--  1 root root 95475 Aug 11 20:16 CONTRIBUTORS
drwxr-xr-x  7 root root  4096 Aug 11 20:16 doc
-rw-r--r--  1 root root  5686 Aug 11 20:16 favicon.ico
drwxr-xr-x  3 root root  4096 Aug 11 20:16 lib
-rw-r--r--  1 root root  1479 Aug 11 20:16 LICENSE
drwxr-xr-x 12 root root  4096 Aug 11 20:16 misc
-rw-r--r--  1 root root  1303 Aug 11 20:16 PATENTS
drwxr-xr-x  6 root root  4096 Aug 11 20:19 pkg
-rw-r--r--  1 root root  1607 Aug 11 20:16 README.md
-rw-r--r--  1 root root    26 Aug 11 20:16 robots.txt
-rw-r--r--  1 root root   397 Aug 11 20:16 SECURITY.md
drwxr-xr-x 47 root root  4096 Aug 11 20:16 src
drwxr-xr-x 23 root root 12288 Aug 11 20:16 test
-rw-r--r--  1 root root     6 Aug 11 20:16 VERSION

$ cat /usr/local/go/VERSION 
go1.15

As expected, go binary was not available at the moment for the broken symlink:

$ go version
bash: /usr/local/bin/go: No such file or directory

$ ls -la /usr/local/bin/go
lrwxrwxrwx 1 root root 18 Apr  1  2019 /usr/local/bin/go -> /var/lib/go/bin/go // (symlink error)

I then deleted the old symlink and created a new one:

$ sudo rm /usr/local/bin/go 
$ sudo ln -s /usr/local/go/bin/go /usr/local/bin/go

After this my go symlink was pointing to the new version:

$ go version
go version go1.15 linux/amd64

$ which go
/usr/local/bin/go

$ ls -la /usr/local/bin/go
lrwxrwxrwx 1 root root 20 Aug 21 13:20 /usr/local/bin/go -> /usr/local/go/bin/go

$ ls -la /usr/local/go/bin/go
-rwxr-xr-x 1 root root 14256244 Aug 11 20:18 /usr/local/go/bin/go

$ /usr/local/go/bin/go version
go version go1.15 linux/amd64