Doyenhub

How to Setup and Run SonarScanner on Linux Local System

Introduction

Setting up and running SonarScanner on Linux is essential for developers who want to maintain code quality and consistency. SonarScanner, when paired with SonarQube, provides a comprehensive solution for static code analysis, helping identify and resolve potential bugs. This guide will walk you through how to setup and run SonarScanner on Linux efficiently.

What is SonarQube?

SonarQube is an open-source static code analysis platform that helps developers monitor source code quality and consistency. To set up SonarQube, refer to How to Set Up SonarQube Server on Linux Local System

What is SonarScanner?

SonarScanner is the official scanner for analyzing projects with SonarQube. It is ideal for standalone code scans or integrating into your CI/CD pipeline.

How to Setup SonarScanner on Linux

Step 1: Download and Install SonarScanner

  1. Download the latest version:

    wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.2.2472-linux.zip

  2. Unzip and move the scanner:
    unzip sonar-scanner-cli-4.6.2.2472-linux.zip mv sonar-scanner-4.6.2.2472-linux /opt/sonar-scanner

Step 2: Configure SonarScanner

  1. Edit the configuration file:
    vi /opt/sonar-scanner/conf/sonar-scanner.properties

    Add the following:

    properties
    sonar.host.url=http://localhost:9000 sonar.sourceEncoding=UTF-8
  2. Add the SonarScanner binary to the system PATH:
    vi /etc/profile.d/sonar-scanner.sh

    Add the following lines:

    #!/bin/bash export PATH=”$PATH:/opt/sonar-scanner/bin”

  3. Apply changes:
    source /etc/profile.d/sonar-scanner.sh

    Verify with:

    sonar-scanner -v

Running SonarScanner on Your Project

Using the Command Line

  1. Navigate to your project directory.
  2. Run the following command:
    sonar-scanner \ -Dsonar.projectKey=myproject \ -Dsonar.sources=. \ -Dsonar.host.url=http://localhost:9000 \ -Dsonar.login=<project_token>

Using a Properties File

  1. Create a file named sonar-project.propertiesn your project directory.
  2. Add the following configuration:

    properties

    sonar.projectKey=myproject sonar.projectName=My Project sonar.sourceEncoding=UTF-8 sonar.sources=. sonar.host.url=http://localhost:9000 sonar.login=<project_token> sonar.exclusions=database/migrations/**,resources/lang/**

  3. Run the scanner:
    sonar-scanner

Viewing the Results

Once the scan is complete, visit the SonarQube dashboard (URL provided in the output) to view detailed analysis and address any identified issues.

Conclusion

SonarScanner makes it easy to maintain and enhance your code quality. Follow this guide to set up and run SonarScanner on your Linux system and keep your projects consistent and bug-free.

Leave a comment

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