Tools: How To Install Go on Ubuntu

Tools: How To Install Go on Ubuntu

Source: DigitalOcean

By Jeanelle Horcasitas, Brennen Bearnes and Manikandan Kurup Go, sometimes referred to as “Golang”, is an open-source programming language that was released by Google in 2012. It is widely used for building reliable command-line tools, backend services, and cloud-native applications, and it is known for its simple syntax, strong standard library, and fast compile times. In this tutorial, you will install Go on an Ubuntu system using one of three common methods (the official tarball, APT, or snap), configure your shell PATH so your terminal consistently uses the correct go binary, and verify the installation by building and running a small module-based “Hello, World!” program. You will also learn how to build an executable binary, understand where go install places compiled tools, and troubleshoot common issues like version conflicts caused by multiple installations. A successful Go setup mostly comes down to choosing the right installation method for your environment and making sure your PATH points to the Go toolchain you intend to use. Deploy your Go applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app. This tutorial requires an Ubuntu system configured with a non-root user with sudo privileges and a firewall as described in Initial Server Setup with Ubuntu. Note: This tutorial was tested with Ubuntu 24.04 and Go version go1.26.0. There are three primary ways to install Go on Ubuntu. The correct choice depends on your environment and requirements. Installing Go from the official tarball gives you the most control over the Go version and ensures you are using the upstream release directly from the Go team. This method is preferred if you need the latest Go version as soon as it is released, if you want predictable version management, if you are setting up development environments or CI systems, or if you do not want to rely on Ubuntu’s package update cycle. Unlike the apt method, this approach installs Go into /usr/local/go and requires you to manage updates manually, which is often desirable for development workflows. First, download the official Go binary archive from the Go project. Open the official Go downloads page in your browser and look for the latest Linux AMD64 release and copy its download link. At the time of this writing, the latest release is go1.26.0. The latest stable release may change over time, so make sure you replace the version number in the commands below with the current version listed on go.dev. Now download the archive using curl: Always replace the filename with the latest version shown on go.dev. Installing outdated Go versions can lead to module incompatibilities and missing security fixes. You can confirm the file downloaded successfully: Before installing any manually downloaded software, you should verify its integrity. This ensures: To verify its integrity, run: This command computes the SHA-256 hash of the archive. Your output will look similar to the following (example only — your actual value must match the checksum shown on the Go downloads page): Next, return to the Go downloads page, locate the checksum listed for the file you downloaded, and carefully compare it with the hash shown in your terminal output. If the two values match exactly, the archive is intact and safe to use. However, if they differ, you should delete the downloaded file, download it again from the official source, and avoid proceeding with extraction until the checksums match. Performing this verification is particularly important on production systems, where installing a corrupted or tampered binary could lead to security and stability issues. If Go was previously installed using the tarball method, remove the old version to prevent conflicts. The tarball install does not automatically upgrade existing versions, so old files can remain and cause unpredictable behavior; removing /usr/local/go first ensures you don’t end up with multiple Go trees that can break builds. This command is safe even if Go is not currently installed; it will simply do nothing. Warning: Do not remove /usr/bin/go if it exists from an apt installation; only remove /usr/local/go for tarball installs. If you’re unsure where Go is installed, run: Now extract the archive into /usr/local, which is the standard location for manually installed software, by running: The -C /usr/local flag tells tar to extract into /usr/local, -x extracts files from the archive, -z enables gzip decompression, and -f specifies the archive filename to operate on. After extraction, Go will be installed at: You can confirm this by running: At this stage, Go is installed but not yet available in your shell PATH, which is why the next step is required. To use the go command from anywhere in the terminal, you must add Go’s binary directory to your PATH. On Ubuntu, the file you need to edit depends on the shell you are using: If you are using the Bash shell (which is the default on many Ubuntu systems), you should update the ~/.profile file because it is loaded for login shells and ensures your PATH changes persist across sessions. If you are using the Zsh shell, you should instead edit the ~/.zshrc file, which is the primary configuration file that Zsh reads when starting a new interactive shell. If you are unsure which shell is currently active, you can run the following command in your terminal to check: This command prints the path of your current shell, helping you decide which configuration file to modify. Editing the correct file ensures that the Go binary path is loaded properly every time you start a new terminal session. To make the go command available from any directory, you need to add Go’s binary directory to your PATH. The PATH environment variable is a colon-separated list of directories that your shell searches when you type a command. Append the following line at the end of the file you opened (~/.profile for Bash login shells, or ~/.zshrc for Zsh): This line preserves your existing PATH value and then adds /usr/local/go/bin to the end of it. The /usr/local/go/bin directory contains the go executable (and tools like gofmt), so adding it to PATH is what allows go version, go env, go run, and other commands to work without typing the full path (for example, /usr/local/go/bin/go). If you already have another Go installation on the system (for example, from apt), be aware that PATH order matters. Whichever directory appears first on PATH is the one your shell will use when you type go. Save and exit the editor. After you edit your shell configuration file, your current terminal session will not automatically see the change until the file is reloaded. You can apply the updated configuration without logging out by “sourcing” the file, which runs it in your current shell. If you prefer, you can also open a new terminal session instead of sourcing the file. The goal is simply to ensure the updated PATH value is active in the shell where you are running Go commands. Now confirm that Go is correctly installed and accessible from your shell: Expected output will vary depending on the version you installed, but it should look similar to the following: If you see output like this, Go is installed and your PATH is configured correctly. If you want to confirm that the go command is coming from the tarball installation, you can also check the resolved path: For a tarball installation, the output should typically be: For additional confirmation, you can inspect the Go environment variables that control where Go is installed and where it stores your workspace files: With a tarball installation, GOROOT will typically be /usr/local/go. If you have not explicitly set a GOPATH, it usually defaults to $HOME/go (for example, /home/sammy/go). The GOPATH directory is commonly used for Go build caches and for installed tool binaries when you run go install. This method installs Go from Ubuntu’s official APT repositories and manages it like any other system package. This approach is often convenient on servers because it integrates cleanly with OS upgrades, security updates, and configuration management tools. You should keep one limitation in mind before you choose this method: the Go version provided by Ubuntu is selected for stability and may not be the newest upstream release available at go.dev. If you need a specific Go version for a project, or if you need the newest language and toolchain features immediately, the official tarball method is usually a better fit. If you want to see which Go version Ubuntu will install before you make any changes, you can check the candidate version first: If apt policy is not available, you can use the equivalent command: First, update the package lists: This command updates your local package index so your system knows about the latest versions available in the repositories. Running apt update first helps ensure that apt install resolves dependencies correctly and installs the most current repository build of Go for your Ubuntu release. Now, you can install Go directly by running: This command installs the golang-go package and any required dependencies. With an APT installation, the go command is typically available at /usr/bin/go, and the Go toolchain files are stored under system-managed directories (commonly under /usr/lib/...). To verify the installation, run: You can also confirm which go binary your shell is using: If you want a stronger check (especially if you suspect multiple Go installations), list all go binaries visible to your shell: Finally, you can inspect where Go believes its root directory is: With an APT installation, GOROOT will typically point somewhere under /usr/lib/... rather than /usr/local/go. It is common for Ubuntu’s repositories to provide an older version of Go than upstream. For development environments where version parity matters (for example, when you need to match the Go version used in CI/CD or production), you should prefer the official tarball installation. If you decide to stay with APT, you can upgrade Go later using the normal upgrade workflow: If you want to remove the APT-managed version of Go, you can uninstall it with: If you also want to remove configuration files and clean up unused dependencies, you can use purge and autoremove: As with the tarball method, you should avoid mixing installation methods unless you are intentionally managing multiple versions. If you install Go via APT and later install a tarball into /usr/local/go, the PATH order will determine which go command runs, which can lead to confusing and inconsistent results. Snap packages provide automatic updates and a managed installation experience through Canonical’s snap system. This approach can be convenient on developer workstations because it reduces manual maintenance, but it generally provides less direct control over exactly which Go version you are running at any given time. You should also be aware that snap-installed binaries typically live under /snap and are exposed through /snap/bin, which may affect how you diagnose PATH and version conflicts if you also install Go using apt or the upstream tarball. Use the following command to install Go: The --classic flag is important here because it installs the Go snap with classic confinement. In practice, this gives the Go toolchain broader access to your filesystem and developer directories than a strictly confined snap, which tends to reduce friction for typical Go development tasks. If the snap command is not available on your system, you can install snap support with: Verify the installation using: After confirming the Go version, it is also useful to confirm where the go binary is coming from: On many systems, the snap-installed go will resolve to /snap/bin/go. If you see a different path, you may be using an apt installation (/usr/bin/go) or a tarball installation (/usr/local/go/bin/go) instead. For additional verification, you can also inspect Go’s root directory: With snap, GOROOT will typically point somewhere under /snap/..., which is expected. Snap installs Go in a managed environment that is updated by the snap system. It can work well for development systems, but it offers less direct control over versioning compared to the tarball method, which is why many teams still prefer the tarball approach for CI/CD and production environments. If you want to see snap-specific details about the installation, you can run: To update the snap to the latest revision available: To remove the snap-installed Go, use: Now that Go is installed and the paths are set for your server, you can try creating your Hello, World! application to ensure that Go is working. First, create a new directory for your project. This is where you will create the module files and build your program: Then move into the directory you just created: When you build and run Go code, dependencies are managed through the project’s module. You can set up a module by creating a go.mod file with the go mod init command: Next, create a Hello, World! Go file in your preferred text editor: Add the following text into your hello.go file: Then, save and close the file by pressing CTRL+X, then Y, and then ENTER. Test your code to check that it prints the Hello, World! greeting: The go run command compiles and runs the Go package from a list of .go source files from the new hello directory you created and the path you imported. But, you can also use go build to make an executable file that can save you some time. The go run command is typically used as a shortcut for compiling and running a program while you are still making frequent changes. Once you are ready to run the program without compiling it each time, you can use go build to create an executable binary. This produces a single executable file that you can run directly. After that, you can use go install to place your program on an executable file path so you can run it from anywhere on your system. Try it out and run go build. Make sure you run this from the same directory where your hello.go file is stored: Next, run ./hello to confirm the code is working properly: This confirms that you’ve successfully turned your hello.go code into an executable binary. However, you can only invoke this binary from within this directory. If you wanted to run this program from a different location on your server, you would need to specify the binary’s full file path in order to execute it. Typing out a binary’s full file path can quickly become tedious. As an alternative, you can run the go install command. This is similar to go build but instead of leaving the executable in the current directory, go install places it in the $GOPATH/bin directory, which will allow you to run it from any location on your server. In this example, you can run go install from your project directory to build and install the program. By default, Go installs binaries to $(go env GOPATH)/bin (or to $GOBIN if you have set it). If you want to confirm where the hello binary will be installed, you can check its target path with go list: go list generates a list of any named Go packages stored in the current working directory. The f flag will cause go list to return output in a different format based on whatever package template you pass to it. This command tells it to use the Target template, which will cause go list to return the install path of any packages stored in this directory: This is the install path of the binary file you created with go build. This means that the directory where this binary is installed is /home/sammy/go/bin/. Add this install directory to your system’s shell path. Be sure to change the highlighted portion of this command to reflect the install directory of the binary on your system, if different: Finally, run go install to compile and install the package: Try running this executable by entering hello: When you install Go using the official tarball, the Go toolchain is installed under /usr/local/go, and the executable programs that you run from the terminal are located in the bin directory. For a tarball installation, the directory that contains the go executable is: The PATH environment variable tells your shell where to look for executables when you type a command such as go, gofmt, or goimports. When you run go version, your shell searches each directory in PATH from left to right and executes the first matching go binary it finds. You can inspect your PATH using: If /usr/local/go/bin is not included, your system cannot find the go command. If you see an error like command not found: go after installing the tarball, it almost always means that /usr/local/go/bin is missing from PATH, or that another Go installation is being chosen first. To confirm which go binary your shell is currently using, run: If you suspect multiple installations, list every go binary visible to your shell: For the tarball method, you typically want which go to resolve to /usr/local/go/bin/go. Adding /usr/local/go/bin to PATH ensures that go, gofmt, and go tool are globally accessible from any directory. Many developers also add $HOME/go/bin to PATH. This is the default location where Go places binaries for command-line tools you install with go install, and it prevents a second, common “command not found” issue later when you start installing Go-based developer tools. Ubuntu uses Bash as the default shell on most installations, but many developers switch to Zsh. In the context of installing Go, this matters because adding /usr/local/go/bin (and often $HOME/go/bin) to your PATH only works if you add the export line to a shell startup file that your terminal actually reads. To check which shell your account uses by default, run: This command prints the path to your login shell (for example, /bin/bash or /usr/bin/zsh). However, keep in mind that many terminal applications start an interactive shell that is not a login shell, while SSH sessions typically start a login shell. That is why it is helpful to understand which files are read in each case. In practice, these are the most common and reliable places to put the Go PATH export: If you are unsure which file your terminal is reading, you can add the export line to the appropriate login file and interactive file for your shell and then remove duplicates later. When you do this, pay attention to PATH order, because the first go found on your PATH is the one that will run. This is especially important if you installed Go using more than one method (for example, both apt and the tarball, or snap and the tarball). This is one of the most misunderstood parts of Go because older tutorials and older tooling assume a GOPATH-centric workflow. Modern Go uses modules by default, but GOPATH still exists and still affects where certain caches and installed binaries are placed. GOPATH was the original workspace mechanism used before Go 1.11. In the pre-modules era, Go expected your source code and dependencies to live inside a specific directory structure, and many commands assumed that layout. The default GOPATH location is: This directory typically contains the following subdirectories: Historically, all Go projects had to exist inside GOPATH/src. Today, you generally do not need to place your projects inside GOPATH/src, but the GOPATH directory is still used for several things, including the default location for installed tool binaries ($GOPATH/bin) and various caches. Go modules are the standard dependency and versioning system for Go projects. A module is defined by a go.mod file, which declares the module path and records the versions of required dependencies. Modules eliminate the need to manually manage GOPATH for most workflows. In a module-based project, common commands include go mod tidy (to add and remove dependencies as needed), go test ./... (to run tests across the module), and go build (to compile). GOPATH is still relevant in a few places, even when you are using modules: Compiled binaries installed via go install (especially go install example.com/tool@version) are placed in $GOBIN if it is set, and otherwise in $GOPATH/bin. To ensure installed tools are executable from anywhere, add: If you already added $HOME/go/bin to your PATH earlier, this command accomplishes the same goal in a more dynamic way because it uses the current value of go env GOPATH. In this section, you’ll see how to check the Go version that is currently active in your shell, how to upgrade Go safely, and how to remove Go if you no longer need it. These tasks are straightforward once you know which go binary your terminal is using, but they can become confusing if you have installed Go more than once (for example, using both APT and the official tarball) and your PATH is selecting a different installation than you expected. By verifying the version and the binary location first, you can make changes confidently and avoid breaking existing projects. Use the following command to check the current version of Go: If you want to confirm which installation method you are currently using, it is also useful to check the resolved binary path and Go’s root directory: For a tarball installation, upgrading Go is a manual process because you are managing the files under /usr/local/go yourself. Download the new tarball from go.dev. Remove the old installation directory: Extract the new version. Verify the upgrade with go version. If the version does not appear to change in your current terminal session after upgrading, you can clear the shell’s command lookup cache and try again: The correct uninstallation steps depend on how you installed Go, because each method places the toolchain in a different location. Before you remove anything, it is a good idea to confirm which go binary you are currently using so you do not delete the wrong directory. Once you know where Go is installed, follow the matching method below. If you installed Go from the official tarball, the Go directory is usually /usr/local/go. Uninstalling the tarball method means deleting that directory and then removing any PATH entries you added for /usr/local/go/bin. Next, remove the Go export line from your shell startup file (for example, ~/.profile, ~/.bashrc, or ~/.zshrc) so your shell does not continue to reference a directory that no longer exists. After you edit the file, reload it or start a new terminal session. Finally, verify that your shell no longer resolves go to the tarball installation: If which go still returns a path, you likely still have Go installed via APT or snap, or you have another Go binary earlier in your PATH. If you installed Go using APT, remove the package using apt remove. This keeps your system consistent with how other packages are managed, and it also makes it easier to reinstall later if you change your mind. If you also want to remove package configuration files (when applicable) and clean up unused dependencies, you can use purge and autoremove: After uninstalling, you can confirm that APT is no longer providing Go with the following commands: If go version still works, it usually means another Go installation is still present (for example, a tarball install in /usr/local/go or a snap install under /snap). If you installed Go using snap, remove it with snap remove. This deletes the snap-managed installation and prevents future automatic updates. After removal, verify that the go command is no longer resolving to /snap/bin/go: If you had added a Go-related PATH export line manually, you should remove that line as well so your shell configuration matches the current state of your system. Let’s cover some of the most common problems people run into after installing Go on Ubuntu and explains how to diagnose them using simple, repeatable checks. In most cases, the underlying cause is either a missing PATH entry or multiple Go installations on the same system, so the troubleshooting steps focus on confirming which go binary your shell is actually using. This error means your shell cannot find a go executable in any of the directories listed in your PATH, or it means you installed Go but have not yet opened a new terminal session or reloaded your shell configuration. Start by confirming whether go can be resolved at all: If those commands print nothing, check your PATH and confirm whether the Go binary directory is present: If you installed Go using the official tarball, the directory you usually need is /usr/local/go/bin. You can confirm the binary exists on disk with the following command: If the file exists but go is still not found, add /usr/local/go/bin to your shell startup file (such as ~/.profile, ~/.bashrc, or ~/.zshrc) and then reload it. If the file does not exist, re-check the installation steps and make sure the tarball was extracted to /usr/local. Note: If you want to quickly see your PATH one entry per line, this is a convenient diagnostic command: This issue almost always happens when more than one Go installation exists on the same machine and your shell is picking a different go than the one you intended to use. For example, APT typically installs Go at /usr/bin/go, snap often exposes it at /snap/bin/go, and the tarball method installs it at /usr/local/go/bin/go. First, check both the version and the resolved path: If you want a stronger check, list every go that is visible to your shell: You can also confirm which installation tree Go thinks it is using by checking GOROOT: Once you identify the unwanted installation, you can either uninstall it or adjust your PATH so the directory for the intended installation appears earlier. After changing your PATH, clear your shell’s command lookup cache (if your shell caches command paths) and verify again: If go install succeeds but the tool you installed cannot be found (for example, you run a command and get “command not found”), the most common explanation is that the directory where Go places installed binaries is not on your PATH. Start by checking where Go will install binaries on your system: If GOBIN is set, Go installs binaries there. If GOBIN is empty, Go typically installs binaries to $(go env GOPATH)/bin, which usually resolves to $HOME/go/bin. Next, confirm whether that directory is already in your PATH: If it is missing, add the following line to your shell startup file and reload your shell: After reloading, run which <tool> (replacing <tool> with the command you installed) to confirm the binary is now discoverable. If you see the following error: This error usually occurs when you run go commands in a directory that is not part of a module and does not contain a go.mod file. In most cases, the fix is either to change into the correct project directory or to initialize a new module in the directory you intend to use as a project root. If you are not sure whether Go thinks you are in a module, you can check the GOMOD value. When you are inside a module, GOMOD points to the go.mod file, and when you are outside a module it will often be empty or set to /dev/null: If you intended to work inside an existing project, navigate to the directory that contains go.mod and re-run your command. If you intended to start a new project, run go mod init once, and then use go mod tidy to resolve and record dependencies. The most reliable way to install the latest Go release on Ubuntu is to use the official tarball from the Go website. Ubuntu’s repositories often lag behind the newest version. Remove any existing Go installation (optional but recommended): Download the latest Go tarball. Visit go.dev/dl/ and copy the link for the latest Linux AMD64 release, then download it: (Replace the version with the latest available.) Extract to /usr/local: Verify the installation: This method ensures you always get the newest official release directly from the Go team. Yes. Go is available via Ubuntu’s APT repositories. You can install it with: The version in the Ubuntu repositories is usually older than the latest upstream release, which means you may not get the newest language features and toolchain improvements right away. As a result, the APT method is best suited for quick setups, stable environments where the newest Go features are not required, and CI or reproducible builds that are intentionally tied to Ubuntu’s packaged versions. To check the available version: The recommended way to install Go on Linux is to use the official tarball provided on go.dev. This method gives you the latest stable version directly from the Go team and provides consistent behavior across Linux distributions. While the APT package is easier to manage, most developers prefer the official tarball because it stays up to date and aligns with Go’s official documentation. The upgrade process depends on how you installed Go, because each installation method manages versions differently. If you installed Go using the official tarball, you can upgrade it by following these steps: You should verify which go binary you are currently using so you do not accidentally upgrade the wrong installation. You should remove the existing tarball installation directory at /usr/local/go so the new release is installed cleanly. You should download the newer tarball from go.dev and extract it to /usr/local again. You should verify the upgrade and clear your shell command cache if needed. If you installed Go using APT, you can upgrade it using the standard package upgrade workflow, but you should keep in mind that this will only upgrade to the version available in Ubuntu’s repositories: If you installed Go using snap, you can upgrade it by refreshing the snap to the latest revision available for your system: You can verify that Go is installed correctly by checking both the version and the location of the go binary, and by confirming that Go’s key environment values look reasonable. You can use the following checklist: You should confirm that Go runs and prints a version. You should confirm where your shell is resolving the go command from, especially if you have used more than one installation method. You should confirm the Go installation root and default workspace location. If go version or which go fails with a “command not found” error, you should revisit your PATH configuration and make sure the correct Go binary directory (such as /usr/local/go/bin for the tarball method) is being loaded by your shell. You now have three practical ways to install Go on Ubuntu, and the best choice depends on how much control you need over the version. The official tarball method is usually the best fit when you want the newest stable release and predictable version management (which is why it is common in CI/CD and production). The APT method is a good option when you prefer OS-managed packages and upgrades tied to Ubuntu’s repositories. The snap method is often convenient on developer workstations because it is managed and can update automatically. After installation, the most important next step is confirming that your PATH is selecting the go binary you expect, and then running a simple module-based “Hello, World!” to verify your setup end-to-end. From there, you can use Go modules (go.mod and go.sum) for dependency management, and refer back to the version management and troubleshooting sections if you need to upgrade, uninstall, or resolve PATH conflicts. For official documentation and release notes, refer to https://go.dev/doc/. For more Ubuntu-related tutorials, check out the following articles: Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about our products Educator and writer committed to empowering our community by providing access to the knowledge and tools for making creative ideas into a reality With over 6 years of experience in tech publishing, Mani has edited and published more than 75 books covering a wide range of data science topics. Known for his strong attention to detail and technical knowledge, Mani specializes in creating clear, concise, and easy-to-understand content tailored for developers. This textbox defaults to using Markdown to format your answer. You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link! Simple and easy to understand, as Digital Ocean tutorials are, thank you very much! Please complete your information! Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation. Full documentation for every DigitalOcean product. The Wave has everything you need to know about building a business, from raising funding to marketing your product. Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter. New accounts only. By submitting your email you agree to our Privacy Policy Scale up as you grow — whether you're running one virtual machine or ten thousand. Sign up and get $200 in credit for your first 60 days with DigitalOcean.* *This promotional offer applies to new accounts only.