A Simple Guide to Setting Up Go With Vim (and Installing Go + Vim) on Ubuntu 18.04

Ricky Takkar
Created on May 8, 2020

My current internship requires me to write Go in Ubuntu. Since I have no prior experience in Go, I’ve been indulging in lots of reading and YouTube tutorials … about setting up the best development environment for it. Ultimately, I chose Vim because of how lightweight, customizable, and efficient it is for writing code.

Because of how long it took me to set up a proper environment, I decided to share what eventually ended up working for me as not only a way for me to remember but also to help others who find themselves in a similar situation.

This guide is inspired by the following resources:

Part 1: Install Go

Note that this is applicable to machines that have never installed Go before. If you are trying to follow these steps otherwise, either format the drive (brute force) or completely remove Go from your machine first. Not doing so will cause unnecessary problems and headaches somewhere down the road, I promise. Without further ado, here are the steps to install Go:

  1. Download the archive here

  2. To extract the archive’s contents, navigate to the directory where you downloaded the archive and enter:
    sudo tar -C /usr/local -xzf go<version-number>.linux-amd64.tar.gz

  3. Pick a home for your future Go projects. In that directory, create 3 new directories called bin, pkg, and src. Read this to get a more in-depth understanding of organizing your Go workspace. Ensure that those directories are empty and that nothing except those 3 directories exists there. More on this in the coming steps…

  4. Check your environment variables (you can use the printenv command to check your environment variables) and ensure PATH has nothing related to Go. Also, ensure that no environment variable named GOPATH exists.

  5. Add /usr/local/go/bin to the PATH environment variable. To do this, append the new Go bin directory to your existing PATH environment variable in /etc/profile using any editor: export PATH=$PATH:/usr/local/go/bin

  6. Now, we must set GOPATH. This is the step that the last part of step 3 was foreshadowing. Add a line to /etc/profile setting your GOPATH environment variable:
    export GOPATH=<path to your Go Workspace as specified in Step 3>

  7. Check to see if your installation works by simply typing: go version. This should return the version of Go you just installed on your machine. Now, “you are ready to Go :)” (source: Go)!

  8. Save these changes to /etc/profile and exit.

  9. Restart the device, or, simply reload the bash shell ~/.bashrc. Now, check your environment variables again. The most recent addition to PATH should be /usr/local/go/bin and you should also see your GOPATH environment variable set.

Part 2: Install + Configure Vim

Vim comes pre-installed with most Linux distros. To check the enabled features of your current Vim editor, simply enter: vim --version. If, for some reason, you wish to upgrade from your default installation, follow the steps here to install Vim 8.2. Note that vim-go (the essential plug-in that adds Go language support to Vim), which we will cover in Part 3, requires at minimum Vim 8.0.1453. Now that you are content with the version of Vim on your system, we may proceed to customization. By “customize”, I mean that we can change the appearance of our beloved editor and add functionality that may exponentially increase our efficiency. However, to do all this, we will need to create a file called .vimrc. Fun fact: Eric S. Raymond, author of The Art of Unix Programming, refers to “rc” files as “run-control” files. Before we proceed further, here are 3 important things to remember and note before proceeding onwards:

  1. Create your .vimrc file in your home directory touch ~/.vimrc. There is likely another .vimrc file elsewhere in your system but neither will we touch that nor discuss it. For all intent and purposes, the .vimrc that you create in the home directory is the only one we care about.

  2. In order to install plug-ins in Vim, I find it best to use one of many available Vim package managers. Personally, I use vim-plug. Click here to go on the GitHub repo for this plug-in to read more and learn how to install it on your system following the few and easy steps. Make sure you install at least one of the plug-in managers and learn how to use them in a .vimrc before continuing.

  3. Make sure you have git installed on your machine (click here for instructions). At this point, I suggest you play around a bit with Vim and learn some basic commands. No need to learn about things like tabs and buffers just yet… Focus on simple movement commands like going to a specific line, deleting/cutting lines, and, copyi- I mean yanking lines.

Part 3: Amalgamate

Now that you have created ~/.vimrc and familiarized yourself with how to install a Vim plug-in, we’re almost there! Here are the steps I took to complete my Go + Vim setup:

  • In ~/.vimrc, install the vim-go plug-in. Instructions for doing so using a variety of plug-in managers are mentioned on the all-important vim-go GitHub page here. Since I use vim-plug, these instructions will contain vim-plug specific notation.

  • If you read the short and concise vim-plug usage documentation as suggested earlier, you’ll know that all plug-ins go between the call plug#begin() and call plug#end() lines in your .vimrc. Furthermore, a line to install a plug-in generally follows the structure Plug $<GitHub repo owner's name>/<GitHub repo name>$. As such, to install vim-go, all we need to do is add the following line in its proper place: Plug 'fatih/vim-go’, { 'do’: ’:GoInstallBinaries’ }

  • After adding the line above in its proper place, enter command mode (hit the escape key) and enter the following command to install your new plug-in(s): :PlugInstall (the colon before ‘P’ is not a typo and must be included). You will see a new window open showing the status of your installation.

  • Restart your terminal and open Vim.

  • Learn about the awesome features available to you by opening Vim and entering :help vim-go in command mode

  • Apart from vim-go, there are many cool plug-ins that make life so much easier in Vim. Personal favorites include NERDTree and vim-airline. Read up on them to learn more. Of course, you can change the default font scheme too. Your choices are virtually unlimited – enjoy Vim!