Install Golang in WSL2 / Linux 💯
For windows users, it's a very straightforward installation. In this blog, I am going to show some steps to install this in WSL2 or Linux distributions (It should work any distributions).
If you are using Debian-based distributions you can use apt to install go but I would not recommend it because it will be always outdated.
You can use snap to install the latest one.
Just use this command and you will good to go.
sudo snap install go --classic
What if you don't want to use snap or you are using WSL2 in windows, you can install golang manually. Just follow these steps.
Go to this link and copy the tar file link
Now open your terminal (I am using WSL2) and type/copy this command.
wget [golang tar file link]
I am using 1.16.4 so my command will be this.
It will download this file and save it in your current directory. Once the download completes we have to put this in our /usr/local directory. As it is a tar file (compressed) we have to uncompressed and move this to our designated directory. To do that type out this command, make sure you type your golang-versioned tar file.
sudo tar -xvf go1.16.4.linux-amd64.tar.gz && sudo mv go /usr/local/
Okay as we moved this now we have to add a binary folder to our bashrc (environment path).
Open bashrc in your favorite editor, I am using nano.
sudo nano ~/.bashrc
Add these three lines to the end of your bashrc file.
export GOROOT=/usr/local/go export GOPAT=$HOME/go export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
Now we have to reload the bashrc file.
Oh yeah !!! Now we have successfully install golang in our system. Type this to verify