In this tutorial I will show you step by step instructions to upgrade go version using both manual method as well using apt package manager. You can choose your preferred method based on your environment and requirement.
1. Check the current Go version
This command outputs the currently installed Go version, providing a baseline for your upgrade decision.
go version
2. Determine How Go Was Installed Earlier
Go can be installed on Linux in various ways—via apt
, manually from the official site, or even using version managers. Knowing how Go was installed determines how you should proceed with the upgrade.
Check if Go was Installed via apt
If you're unsure whether Go was installed using apt
, you can check by querying the package manager:
dpkg -l | grep golang
This command will list any Go packages installed via apt
. If you see Go listed here, it was installed using the package manager.
Check for a Manually Installed Go
If Go was not installed via apt
, it might have been installed manually. You can check where Go is installed by:
which go
This command will tell you the path of the Go executable. The default location for a manually installed Go is usually /usr/local/go
. If you see a path like /usr/local/go/bin/go
, it's likely that Go was installed manually from the official Go website.
3. Decide on Upgrade Method
Choose between upgrading using apt
or manually.
- If Installed via
apt
and Available inapt
: Proceed to upgrade usingapt
. - If Not Available in
apt
or Manually Installed: Plan to manually download and install the newer version.
If a newer version is available via apt
, it's often easier to upgrade using apt
. If not, or if you need a specific version not offered in apt
, a manual upgrade is the way to go. Based on the output of the previous step, choose the upgrade method. If the desired version is listed in apt
, use apt
to upgrade. Otherwise, prepare for a manual upgrade.
3. Upgrade Go
Based on your preferred method, you can now plan to upgrade the go version on your Ubuntu server.
3.1 Manual Method
Remove the Current Version: First, remove the existing Go installation. Assuming you installed it in /usr/local/go
, use:
sudo rm -rf /usr/local/go
Download from Go website using wget or curl or any similar command:
cd /tmp/ wget https://go.dev/dl/go1.21.6.linux-amd64.tar.gz
Extract and install:
sudo tar -C /usr/local -xzf go1.21.6.linux-amd64.tar.gz
Make sure /usr/local
is available in your PATH variable. It not available then this should be added in your PATH variable via profile files such as .bash_profile
, .profile
, .bashrc
etc. You can read .bashrc vs .bash_profile [Which one to use?] if you are confused.
Edit your ~/.bashrc
, ~/.profile
, or ~/.bash_profile
:
export PATH=$PATH:/usr/local/go/bin
Apply Changes to the Current Session
source ~/.bashrc
3.2 Using apt package manager
Update Package Lists: This step ensures you have the latest package listings from all configured repositories, crucial for finding the newest Go version available via apt
.
sudo apt update
This command fetches the latest package information, making sure you see the most recent versions available for installation.
Search for Available Versions in apt
: Before deciding on the manual installation, it's good to check if a newer version is already available in your apt
repositories using apt-cache command.
apt-cache search golang | grep golang-
This lists available Go versions in apt
, helping you choose whether to upgrade via apt
or manually.
Remove Previous Version: To prevent conflicts and ensure a clean environment for the new version. This is essential regardless of the original installation method.
sudo apt remove <golang-package-name> sudo apt autoremove
The <golang-package-name>
needs to be replaced with the actual golang package name as received on the output from dpkg command we used earlier. The above command removes the Go package and any unused dependencies.
For example in my case the apt repo contains 1.21 version:
golang-1.21 - Go programming language compiler - metapackage golang-1.21-doc - Go programming language - documentation golang-1.21-go - Go programming language compiler, linker, compiled stdlib golang-1.21-src - Go programming language - source files
We can install it directly using apt
. For example:
sudo apt install golang-1.21-go golang-1.21-src
4. Verify upgrade
Check the version of Go to ensure it's been upgraded:
$ go version -bash: /usr/bin/go: No such file or directory
In my case while upgrading via apt, I received above error so let's troubleshoot the problem
5. Troubleshoot Errors
Since the go binary is missing so let's check the golang package content to identify the location where golang binary is added on our Ubuntu server. First let's get the package name for our golang:
$ dpkg -l | grep golang ii golang-1.21-go 1.21.1-1~ubuntu22.04.2 amd64 Go programming language compiler, linker, compiled stdlib ii golang-1.21-src 1.21.1-1~ubuntu22.04.2 all Go programming language - source files
Now that we have the package name, let's check the list of content added by this package:
$ dpkg -L golang-1.21-go | grep bin /usr/lib/go-1.21/bin /usr/lib/go-1.21/bin/go /usr/lib/go-1.21/bin/gofmt
Since we are only interested in binaries so I am grepping for bin
directory. As you can see, the go binary is added inside /usr/lib/go-1.21/bin instead of /usr/bin so we have two options:
- Update the PATH variable to declare
/usr/lib/go-1.21
so that we don't have to use full path - Add a symbolic link from
/usr/lib/go-1.21
to/usr/bin
We will use the symbolic link approach:
sudo ln -s /usr/lib/go-1.21/bin/go /usr/bin/go $ ls -l /usr/bin/go lrwxrwxrwx 1 root root 23 Jan 14 17:19 /usr/bin/go -> /usr/lib/go-1.21/bin/go
Now let's re-verify the go version to make sure our solution worked:
$ go version go version go1.21.1 linux/amd64
When we extract the tar fil, we get one more “bin” the go binary is present in here, we have to move this to the installation path, in my case it was /usr/local/bin.