How to Install Go on Ubuntu[3 Easy Methods]

Written by

Reviewed by

Last updated: June 23, 2024

Expert verified

SVG Image

TL;DR

Here is a step-by-step guide to install Go on Ubuntu:

  1. Update system packages using the command sudo apt update.
  2. Upgrade the system packages to the latest versions by using the command sudo apt upgrade -y for improved stability and security.
  3. To install Golang Ubuntu, you need to download and extract the latest stable release from the official website.
  4. Add the Go binaries to the system’s PATH variable to access them conveniently from any directory.
  5. Apply the changes to the shell session to make the updated configuration effective.
  6. Check the Go version by running the command go version to confirm the installation’s success.

Continue reading the guide below to learn to install Go on Ubuntu and the common errors you may encounter during installation.


Installing software on Ubuntu can sometimes be tricky, especially when it comes to programming languages like Go (Golang). If you’re looking for a straightforward way to get Go up and running on your system, you’re in the right place. This guide provides clear, step-by-step instructions to help you install Go with ease. Whether you’re new to coding or an experienced developer, this guide will ensure a smooth installation process. Let’s dive in and get Go set up on your Ubuntu system!

What is Go?

Go, also known as Golang, is a programming language created by Google. It was designed to be simple, efficient, and reliable. Go is often used for developing large-scale software applications because it compiles quickly, runs fast, and has a powerful standard library.

Importance of Installing Go on Ubuntu

  • Fast Performance: Go is known for its speed. Programs written in Go run fast because the language is compiled, not interpreted. This means the code is translated directly into machine language that your computer can execute, making Go applications highly efficient.
  • Ease of Use: Go has a clean and straightforward syntax, making it easy to learn and use. This simplicity helps developers write clear and maintainable code, reducing the likelihood of bugs and making it easier to collaborate with others.
  • Strong Concurrency Support: Concurrency is a way to perform multiple tasks at the same time. Go has built-in support for concurrent programming, making it ideal for modern applications that need to handle many tasks simultaneously, like web servers or database systems.
  • Large Community and Ecosystem: Go has a large and active community. This means there are many libraries, tools, and frameworks available to help you with your projects. Additionally, the community provides a wealth of tutorials, documentation, and forums where you can get help and advice.
  • Integration with Ubuntu: Ubuntu is a popular choice for developers due to its stability, security, and ease of use. Installing Go on Ubuntu ensures you have a reliable environment to develop, test, and deploy your applications. The combination of Ubuntu’s robust system and Go’s powerful capabilities creates a productive development experience.

How to Install Go on Ubuntu 

To install Go on Ubuntu, download the Go tarball from the official Go download page, extract it using sudo tar -C /usr/local -xzf goVERSION.linux-amd64.tar.gz, move the Go directory to /usr/local, set up environment variables by adding export PATH=$PATH:/usr/local/go/bin to your ~/.profile, and then verify the installation by running go version in your terminal.

That was the quick answer. For detailed steps for this method and two other methods to install go Ubuntu read the guide below:

1. Installing Go Using the Official Archive

Installing Go using the official archive is a reliable method to ensure you get the latest stable version directly from the source. This method involves downloading the Go tarball, extracting it, and setting up your environment. Follow these steps to Ubuntu install Golang.

  1. Open a Terminal and execute the following command to update system packages:
sudo apt update
  1. The command will update your system packages.
updating system packages
  1. Run the following command to upgrade your system:
sudo apt upgrade -y
  1. This will upgrade your system packages, ensuring you have the latest software.
upgrading system packages 1
  1. Visit the official Go website by clicking here.
visit official website of go
  1. On the Go page, locate the latest stable release compatible with Ubuntu. Click on the appropriate download link to begin the download.
downloading stable version of Go
  1. Open a Terminal and navigate to the directory where you downloaded the Go package.
navigating to directory
  1. Extract the package using the following command:
sudo tar -C /usr/local -xf goVERSION.linux-amd64.tar.gz

Replace VERSION with the version number you downloaded.

command to install go on ubuntu
  1. Next, you need to add Go binaries to your system’s PATH variable. Open the .profile file using a text editor:
nano ~/.profile
  1. This command will open the .profile file in the nano editor.
opening system PATHs
  1. Append the following lines at the end of the file:
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
  1. Save the changes and exit the text editor.
adding go binaries to system PATH
  1. Finally, apply the changes to your current shell session by executing the following command:
source ~/.profile
  1. Now you have successfully installed Go on Ubuntu.
applying changes to current shell session
  1. To ensure Go functions properly, you need to set up the necessary environment variables. Execute the following command to create the Go workspace directory:
mkdir -p $HOME/go
  1. The output will be:
creating workspace directory for go
  1. Next, append the Go environment variables to the .profile file:
echo "export GOPATH=\$HOME/go" >> ~/.profile
echo "export PATH=\$PATH:\$GOPATH/bin" >> ~/.profile
  1. Save the changes and exit the text editor.
appending go environment variables
  1. Apply the changes to your current shell session by executing the following command:
source ~/.profile
  1. Your environment is now properly configured for Go development on Ubuntu.
save the changes
  1. To ensure Go is correctly installed, execute the following command to check the Go version:
go version
  1. The output should display the installed Go version, confirming a successful installation.
verifying go installation

2. Installing Go Using APT Package Manager

Using the APT package manager to install Go is one of the simplest and most efficient methods, especially for users who prefer using Ubuntu’s package management system. This method ensures that Go is kept up-to-date automatically and integrates seamlessly with your system. Follow these steps to Golang install Ubuntu:

  1. Adding the Go repository is the first step. This ensures you are getting the latest stable version directly from a trusted source. To add the Go repository, open your terminal and execute the following commands:
sudo add-apt-repository ppa:longsleep/golang-backports
adding go repositoryy to the system
  1. After adding the repository, update your package lists to include the new repository:
sudo apt update

Once the repository is added and updated, you can proceed with the installation.

updating system package list 5
  1. To install Go, use the following command:
sudo apt install golang-go
installing go on ubuntu
  1. After the installation completes, verify it by checking the Go version:
go version

You should see output similar to go version go1.x.x linux/amd64, confirming that Go is installed correctly.

verifying go installation
  1. Setting up environment variables ensures Go works seamlessly across different sessions and projects. Add the Go binary path to your PATH environment variable by editing your ~/.bashrc or ~/.zshrc file:
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
adding go binary path to system path variable
  1. Reload the shell configuration to apply the changes:
source ~/.bashrc
reloading shell configuration to apply changes

3. Installing Go Using Snap Package

Snap is a package management system that allows you to install applications in a self-contained environment. Using Snap to install Go ensures an easy and clean installation, with automatic updates and rollback features. Follow these steps to go install Ubuntu using snap:

  1. Before you can install Go via Snap, you need to ensure Snap is installed on your system. If Snap is not already installed, you can install it using the following command:
sudo apt update

sudo apt install snapd
installing snapd on ubuntu
  1. After installing Snap, it’s a good idea to restart your system to ensure all Snap features are properly initialized.

With Snap installed, you can now proceed to install Go.

  1. To install Go using Snap, execute:
sudo snap install go --classic

The –classic flag ensures that Go is installed with classic confinement, allowing it more access to system resources.

installing go using snap package
  1. After installation, it’s important to verify that Go is correctly installed and functioning. Check the installed version of Go by running:
go version

You should see an output similar to go version go1.x.x linux/amd64.

verifying go installation through snap package

How to Uninstall Go from Ubuntu

Uninstalling Go from Ubuntu might be necessary if you need to switch versions, resolve conflicts, or simply free up system resources. This guide will walk you through a straightforward method to remove Go completely from your system, ensuring a clean environment for any future installations or updates.

  1. Open your terminal.
  2. Execute the following command to remove the Go directory:
sudo rm -rf /usr/local/go

This command deletes the entire Go directory and its contents.

deleting entire go directory
  1. Next, you need to remove the environment variables associated with Go. These variables tell your system where to find Go binaries and libraries. Open your ~/.bashrc file in a text editor (you can use nano or any other text editor):
nano ~/.bashrc
opening bashrc file in nano editor 2
  1. Look for lines that set the Go environment variables, such as:
export PATH=$PATH:/usr/local/go/bin

Delete these lines.

deleting line from bashrc file
  1. Save and close the file (Ctrl + O to save and Ctrl + X to exit in nano).
saving and exiting the file
  1. Reload your shell configuration to apply the changes. This step ensures that the Go environment variables are no longer active:
source ~/.bashrc
reloading shell configuration to apply changes 1
  1. Finally, verify that Go has been uninstalled successfully. Check if Go is still available by running:
go version
  1. You should see a command not found message, indicating that Go has been removed from your system.
verifying go is uninstalled

Best Practices for Optimizing Go Performance on Ubuntu

To get the best performance from your Go applications on Ubuntu, follow these best practices. Each tip is designed to help your code run more efficiently, making the most of your system’s resources. Lets explore:

  • 🔄 Keep Go Updated: Regularly update Go to the latest stable version. New releases often include performance improvements and bug fixes that can make your applications run faster and more reliably.
  • 🧹 Clean Up Dependencies: Regularly audit and remove unused dependencies from your projects. This reduces the size of your codebase, speeds up build times, and minimizes potential security risks.
  • 🖥️ Monitor System Resources: Use monitoring tools like htop or vmstat to keep an eye on CPU and memory usage. This helps identify and address performance bottlenecks in your Go applications.
  • 🏗️ Use Efficient Algorithms: Choose the right algorithms and data structures for your tasks. Well-designed algorithms can significantly reduce execution time and resource consumption.
  • 🔒 Manage Goroutines Wisely: Limit the number of goroutines to prevent overwhelming the system. Use synchronization techniques like channels and mutexes to avoid race conditions and ensure smooth concurrent execution.

Ubuntu Install Go: Summing Up

In this guide, I’ve shown you how to install Go in Ubuntu using various methods, uninstall it when needed, and optimize its performance. By following these steps, you’ll ensure a smooth and efficient Go development environment on your Ubuntu system, making your coding experience more productive and enjoyable.

If you found this guide helpful, I recommend checking out a few more topics that can enhance your Ubuntu experience. 

  • Learn how to fix the “You Have Held Broken Packages” error on Linux to troubleshoot and resolve common package installation issues. 
  • Understanding how to install a specific version of a package using APT will help you manage dependencies more effectively. 
  • Lastly, exploring how to install R on Ubuntu will expand your programming toolkit, especially if you’re interested in statistical computing and graphics.

Frequently Asked Questions

Can I install Go on Ubuntu using a package manager?

While Ubuntu’s default package manager, apt, does not provide the latest Go versions, you can still install Go on Ubuntu using package managers specifically designed for Go, such as apt-get. These Go-specific package managers install and manage Go packages, including updates and dependencies. They ensure that you have the latest version of Go and provide a seamless experience for managing your Go projects. Some popular Go package managers compatible with Ubuntu include apt-get, snap, and gimme. To install Go using a package manager, refer to the respective documentation and installation guides for detailed instructions tailored to your chosen package manager.

How can I uninstall a specific version of Go on Ubuntu?

To uninstall a specific version of Go on Ubuntu, identify the installation directory of the version you want to remove. Open a Terminal and execute the command sudo rm -rf {{INSTALLATION_DIRECTORY}}, replacing {{INSTALLATION_DIRECTORY}} with the actual path. This deletes the directory and its contents. Update your environment variables by modifying or removing the relevant lines in the .profile file. Save the changes, exit the text editor, and execute source ~/.profile to apply the updates. Following these steps ensures the clean removal of the specific Go version while preserving other installations on Ubuntu.

Can I use Go for web development on Ubuntu?

Absolutely! Go is well-suited for web development on Ubuntu. Its simplicity, efficient concurrency features, and a robust standard library make it an excellent choice. The built-in net/http package provides powerful tools for handling HTTP requests and building web servers. Go’s easy-to-understand syntax and comprehensive documentation enable developers to quickly create reliable web applications regardless of scale. Whether you’re developing a small web application or a large-scale web service, Go offers a versatile and reliable language for web development on Ubuntu. Take advantage of Go’s strengths and enjoy a productive web development experience on Ubuntu.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Get UUID in Linux [5 Best Methods]

Next Post

How to Install Arch Linux on VirtualBox [17 Effective Steps]

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next