Install golangci-lint on Ubuntu 24.04

Install golangci-lint on Ubuntu 24.04

The golangci-lint is a static analysis tool (also known linter) for Go programming language. It performs checks in parallel, utilizes caching, supports YAML configuration, and integrates with all major IDEs. This tutorial explains how to install golangci-lint on Ubuntu 24.04.

Prepare environment

Before you begin, ensure that you have installed Go on your system. Installation instructions are available in a separate post.

Install golangci-lint

Fetch the latest version of golangci-lint from its GitHub repository and store it in a variable:

GOLANGCI_LINT_VERSION=$(curl -s "https://api.github.com/repos/golangci/golangci-lint/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')

Download golangci-lint archive file:

wget -qO golangci-lint.tar.gz https://github.com/golangci/golangci-lint/releases/latest/download/golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz

Create temporary directory and extract a tar.gz file:

mkdir golangci-lint-temp
tar xf golangci-lint.tar.gz --strip-components=1 -C golangci-lint-temp

Move executable to /usr/local/bin directory:

sudo mv golangci-lint-temp/golangci-lint /usr/local/bin

We can check golangci-lint version as follows:

golangci-lint --version

Clean up unneeded files and directories:

rm -rf golangci-lint.tar.gz golangci-lint-temp

Testing golangci-lint

Create a simple Go file for testing purposes:

printf "package main\nimport \"fmt\"" > main.go

To use golangci-lint, run the command golangci-lint run followed by the name of the Go file or directory you want to analyze. For example:

golangci-lint run main.go

This command will execute various checks on the specified file, checking for issues such as style violations, errors, and performance improvements. Output:

main.go:2:8: "fmt" imported and not used (typecheck)
import "fmt"
       ^

Uninstall golangci-lint

To uninstall golangci-lint, remove the related file:

sudo rm -rf /usr/local/bin/golangci-lint

Leave a Comment

Cancel reply

Your email address will not be published.