31

This is not a programming question.

I have a machine running Ubuntu, and I installed Golang on it. It was working fine... I even ran a few programs, but the "go1.11.2.linux-amd64.tar.gz" file was in my home directory so I thought it would be okay to move it to the Downloads directory. After moving it, I can't use any Go command, and I get command 'go' not found. I tried moving the file back to the home directory, but I'm still getting the same error.

Can anybody explain to me what's going on? Thanks!!

richie@richie-ThinkPad-T430:~$ go version

Command 'go' not found, but can be installed with:

sudo snap install go         # version 1.11.2, or
sudo apt  install golang-go
sudo apt  install gccgo-go 

See 'snap info go' for additional versions.

The commands I used to install Go :

wget https://dl.google.com/go/go1.11.2.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.11.2.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
source ~/.profile
richie
  • 415
  • 1
  • 5
  • 9
  • The tar,gz file isn't relevant. Did you install from source? Please [edit] your question and show us the commands you used to install go. – terdon Nov 13 '18 at 15:27
  • was it because I saved the environment variable only to that specific shell session? – richie Nov 13 '18 at 15:34
  • 2
    You probably were supposed to put the export command into the .profile. Please manually execute the correct export command and try again. – Jos Nov 13 '18 at 15:34
  • you were right, the location of the tar,gz file has nothing to do with it. I ran export PATH=$PATH:/usr/local/go/bin and then go version and I get the right version – richie Nov 13 '18 at 15:41
  • but I don't wanna have to explicit use the export PATH=$PATH:/usr/local/go/bin command every time I need to build a project...can you show me how I'm supposed to put the export command into the .profile – richie Nov 13 '18 at 15:43
  • If you want to use a command line editor to do that, try nano. Otherwise, start up gedit or install geany. – Jos Nov 13 '18 at 15:54
  • I'm trying to add export PATH="/home/richie/go/src/hello:$PATH" to the bottom of the gedit ~./profile file but it still isn`t working – richie Nov 13 '18 at 16:12
  • You may need to ask another question for that. Your go command has now been found. – Jos Nov 13 '18 at 17:04
  • I had the same problem. Installed with sudo aptitude install golang-1.12-go (I needed that specific version). Installation was fine but couldn't fine the actual command. The binary is not even in /usr/local/go/bin so not even sure where aptitude has installed it – Giuseppe Salvatore Mar 04 '20 at 11:28
  • cd to make sure you're in the home directory. nano .profile to open the .profile file. add the export line at the end of the file. And that's it. You'll need to either restart your terminal or maybe reboot for the changes to take effect. Also, go through a command line basics tutorial before you jump deeper. "The Linux Commandline" by William Shotts is pretty good. – kchak Jan 16 '21 at 11:13

7 Answers7

40

Jos in the comments above is likely correct. You need to add the change to PATH in your .profile. From the install doc (emphasis added):

Add /usr/local/go/bin to the PATH environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:

export PATH=$PATH:/usr/local/go/bin
Benny Jobigan
  • 516
  • 4
  • 8
16

From: Installed golang still go: command not found #20

Use this command:

sudo apt update && sudo apt install golang
wjandrea
  • 14,236
  • 4
  • 48
  • 98
  • 1
    but I still have a question...did I just install Go x2? I mean, I was already able to build projects and etc..I just had to ran export PATH=$PATH:/usr/local/go/bin everytime – richie Nov 14 '18 at 02:53
  • @richie Sorry I'm not an expert on the various Go versions. If you find you need a different version ti's usually a straight forward change. – WinEunuuchs2Unix Nov 14 '18 at 03:40
  • 1
    Oh no, I'm sorry haha I didn't mean as in x2 version of Go. What I meant was since I had already installed Go, and needed to export PATH=$PATH:/usr/local/go/bin to be able to build programs which isn't ideal. did I install Go a second time by using sudo apt update && apt install golang? – richie Nov 14 '18 at 05:09
  • 1
    I mean, I ran all the commands in the question's description but they only sort of worked since I had to export PATH every time I needed to build a project.. by using sudo apt update && apt install golang did I install Go a second time – richie Nov 14 '18 at 05:11
  • 1
    Yeah that installs another version of golang . – I.Tyger Aug 31 '19 at 09:32
8

Use nano ~/.profile to edit the file and add the following:

export PATH=$PATH:/usr/local/go/bin

Save the file using the command source ~/.profile. Check the version:go version

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
sree
  • 101
3

Try adding the exact export command to the ~/.bashrc file.

You need to source the ~/.bashrc file for changes to take place in your current terminal. From next time onward whenever you open a terminal, you should be able to find the go command. This worked for me.

Eliah Kagan
  • 117,780
  • to me, I had to use this approach as whenever I opened a new terminal I had to run export PATH=$PATH:/usr/local/go/bin command even appending this within .profile doesn't work forever. – Benyamin Jafari Oct 15 '22 at 17:01
3

One line command to install go,

[ ! -d "/usr/local/go" ] && cd /tmp && wget https://go.dev/dl/go1.17.4.linux-amd64.tar.gz && tar -C /usr/local/ -xzf go1.17.4.linux-amd64.tar.gz && cd /usr/local/ && echo "export PATH=\$PATH:/usr/local/go/bin:\$HOME/go/bin" >> ~/.bashrc && echo "export GOROOT=/usr/local/go" >> ~/.bashrc && echo "export PATH=\$PATH:/usr/local/go/bin:\$HOME/go/bin" >> /home/*/.bashrc && echo "export GOROOT=/usr/local/go" >> /home/*/.bashrc && source ~/.bashrc && source /home/*/.bashrc

Note: Run rm /usr/local/go before running this code if it is not working. It will install it for the user you are logged in.

Command explanation (for those who wants to know, so you can edit it if you want):

  1. [ ! -d "/usr/local/go" ] to check if go already downloaded. If it is already there the command will not work. You need to run rm /usr/local/go to make it working.
  2. cd /tmp && wget https://go.dev/dl/go1.17.4.linux-amd64.tar.gz to move to tmp directory and download go binary.
  3. tar -C /usr/local/ -xzf go1.17.4.linux-amd64.tar.gz to unzip the downloaded tar file to installation directory /usr/local
  4. cd /usr/local/ && echo "export PATH=\$PATH:/usr/local/go/bin:\$HOME/go/bin" >> ~/.bashrc && echo "export GOROOT=/usr/local/go" >> ~/.bashrc && echo "export PATH=\$PATH:/usr/local/go/bin:\$HOME/go/bin" >> /home/*/.bashrc && echo "export GOROOT=/usr/local/go" >> /home/*/.bashrc && source ~/.bashrc && source /home/*/.bashrc to set GOPATH and GOROOT for bash terminal.
  • Thank you so much! If you run step 3 and you get the error "tar :go:Cannot mkdir: Permission denied", run the command in sudo mode. sudo tar -C /usr/local/ -xzf go1.17.4.linux-amd64.tar.gz – Remi Jan 28 '23 at 07:19
  • This is the only solution that worked even after closing and opening the terminal. Thank you. – Ruby Jul 12 '23 at 22:52
2

Make sure that the GOPATH environment variable is set to /usr/local/bin.

Eliah Kagan
  • 117,780
1

To leverage Ubuntu/Debian ecosystem (instead editing paths by hand) one can install any version of golang-*-go for example:

sudo apt install golang-1.20-go

and then configure it as default:

sudo update-alternatives --install /usr/local/bin/go go /usr/lib/go-1.20/bin/go 1

Or in the OPs case when go is installed with wget under different path:

sudo update-alternatives --install /usr/local/bin/go go /usr/local/go/bin/go 1

That way you can even install and configure multiple versions and then select which one you prefer, for example:

sudo update-alternatives --config go    
There are 2 choices for the alternative go (providing /usr/local/bin/go).

Selection Path Priority Status

  • 0 /usr/lib/go-1.20/bin/go 1 auto mode 1 /usr/lib/go-1.18/bin/go 1 manual mode 2 /usr/lib/go-1.20/bin/go 1 manual mode

Press <enter> to keep the current choice[*], or type selection number:

This is system wide solution, will work for all users, "survives" restarts etc.

suside
  • 675
  • 7
  • 10