How to setup and Run SonarScanner on Linux local system

What is SonarQube?

It is open source Static Code analysis tool which is used by developer to manage source code quality and consistency.

You can find the more detail for this on “How to setup SonarQube Server on Linux local system

What is SonarScanner?

SonarScanner is the scanner to use when you want to scan your project standalone in the SonarQube.


Sonar Scanner Setup:

1) Installation

Download, unzip and move scanner using below command (latest you can download from

mv sonar-scanner- /opt/sonar-scanner


Edit sonar scanner properties and add below line of code. Here, host will be URL of your SonarQube server

vi /opt/sonar-scanner/conf/


We need to add the sonar-scanner command to the PATH variable. Let’s create a file to automate the required environment variables configuration

vi /etc/profile.d/


Add below line of code in the file:

export PATH="$PATH:/opt/sonar-scanner/bin"


Reboot your computer or use the source command to add the sonar scanner command to the PATH variable.

source /etc/profile.d/


Check the variable set for the scanner with below command:

env | grep PATH


It will output below list of details:



To check the version of SonarScanner run below line of code:

sonar-scanner -v


2) Project Setup and run scanner

Create project in SonarQube with project key and a display name. Enter a string for the project token name and click on the Generate button. After that copy that token which we will use for the scanner code setup in project directory or in command prompt


For the first time, you can scan project 2 ways either using the command prompt directly or using the properties file setup


Using Command prompt:

Traverse to your project directory for which you want to run scan. In root of the directory run the below command and replace the detail which you have setup and got from the SonarQube project setup. Here replace the projectKey and sonar.login value with your detail

sonar-scanner \
-Dsonar.projectKey=myproject \
-Dsonar.sources=. \ \

Once you will run this command, it will automatically create the properties file in the root of the project. So next time you can directly run below command and also update detail in that file.


Properties File setup:

Traverse to your project directory for which you want to run scan. Create one new file inside project folder with name “sonar-project” and extension will be “properties” as “”


Add basic configuration given below:

sonar.projectName="My project"
sonar.sources=. //list of folders which will scan
sonar.exclusions=database/migrations/**,resources/lang/** //list of folders which will exclude from scan

“sonar.sources” & “sonar.exclusion” property values will be the list of folders or files which you wants to scan or exclude from scan. The list must be separated by comma(,). If you want to include all files or folders, then just mention Dot(.)

In sample code, I want to exclude migrations, language folders so added in the list. Same I want to scan whole project so mentioned in source as “.”


Run below command to scan your code.


Once scanning completed, it will output scanning with browse URL and if you will access that URL then can see the project dashboard on SonarQube.

Please find image below image for the reference.


That’s it, now you can check all details and bugs in this panel and manage and fix bugs quickly.

Leave a Reply

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