How to install Golang on Ubuntu (in 5 min)
Here I can explain what is Golang and its origins, but you probably ignore all this text, so, let’s go to the important.
Download and installation:
You need to download the tar.gz file, keep in mind that you will need a specific version of it, just change the version in the file name according what you need, like this:
go1.20.11.linux-amd64 -> go1.19.3.linux-amd64
And then, in a terminal, you will put this command, but check what your current directory is, because where you are currently working will be the path where you are about to download the compiled file:
curl -OL https://golang.org/dl/go1.20.11.linux-amd64.tar.gz
Now, you have to uncompress the file on the /local directory to give the necessary components to execute Golang on your computer. This step is crucial, because this path (/usr/local/) means the direct connection between the language and Ubuntu.
sudo tar -C /usr/local -xvf go1.20.11.linux-amd64.tar.gz
When the last step is over, you are going to declare some environment variables, to indicate where is the language installed, and where we are going to use it.
First of all, let’s update /.profile file on /home path:
sudo nano ~/.profile
Put the following code:
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
And then, let’s refresh the file and environment:
source ~/.profile
After all of this, you will be able to check the current version of Golang in your computer:
go version
-> Output
go version go1.20.11 linux/amd64
Create work path:
Now it’s possible to run Golang code, but before to do it, it’s important to setup the Gopath, because we declared that all the code should be there. It’s pretty simple, just create these folders:
home/
|- go/
|- bin/
|- pkg/
|- src/
|- gitlab.com/ (If you use gitlab)
|- github.com/
|- your_profile_name/
|- all_your_repos_here
It should looks like this:
And it’s done, everything is ready to start to code!
Just more stuff:
If you want more content like this, just follow me on my social networks!
For your attention, thank you so much :D