Run SonarQube Locally .Net
Tags: dotnet, csharp, programming, codequality
Published: 2021-05-10 07:26:39

#### .NET CODE ANALYSIS

#### How to run SonarQube locally for .Net solutions

The article describes how to analyze C# code quality using the SonarQube command line.

### Prerequisites

1. Download the SonarQube free **[Community Edition](https://www.sonarqube.org/downloads/)**

2. SonarQube scanners require version 8/11 of the JVM, and the SonarQube requires version 11

3. **[Install PostgreSQL](https://www.postgresql.org/)** for demonstration, but it also supports MSSQL and Oracle databases.

4. For the .Net Core 2+ version, download SonarQube [Scanner](https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/5.2.1.31210/sonar-scanner-msbuild-5.2.1.31210-netcoreapp2.0.zip).

5. For the .Net Framework version, download SonarQube [Scanner](https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/5.2.1.31210/sonar-scanner-msbuild-5.2.1.31210-net46.zip).

### Getting Started

Following steps to configure & run C# code analysis using SonarQube for .Net Core applications.

Unzip folders(prerequisites in points 1 and 4) into a directory.

#### Configure User

Follow the below commands to configure a database & user in the PostgresSQL database.

```
#Create DBCREATE DATABASE sonar;

#Create a user in DB with a passwordCREATE USER sonar WITH PASSWORD '<password>';

#Grant, all permissions to that userGRANT ALL PRIVILEGES ON DATABASE sonar TO sonar;
```

#### Update SonarQube Configuration

Go to SonarQube Community edition unzipped folder, open the config file in the following path “ **..\conf\sonar.properties.** ”

- Uncomment and add the username and password created above in the config file.

- Add JDBC Postgres URL as shown in the screenshot below.

```
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonar
```

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d05393-5fd2-48e4-91a3-85df010ba09b_800x285.png)](https://cdn.substack.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d05393-5fd2-48e4-91a3-85df010ba09b_800x285.png)

After configuration, now SonarQube will use PostgresSQL to save reports or logs locally.

#### Run SonarQube

Go to SonarQube Community edition unzipped folder, run the following bat file “..\sonarqube-8.8.0.42792\bin\windows-x86–64\StartSonar.bat” in “Admin” mode.

Once the SonarQube server is UP, a client application will be available on the browser at the following URL

```
http://localhost:9000
```

#### Project Creation SonarQube

1. Add a new project.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F282d75a2-65b9-49ab-a427-4845986db23d_800x74.png)](https://cdn.substack.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F282d75a2-65b9-49ab-a427-4845986db23d_800x74.png)

2. Add project details as shown below

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0a9b0452-ae12-42e3-b1a8-4302d5b018ed_800x329.png)](https://cdn.substack.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0a9b0452-ae12-42e3-b1a8-4302d5b018ed_800x329.png)

3. Create a unique token

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6eb039d9-d2f2-4d6d-876b-02899824cf8c_800x513.png)](https://cdn.substack.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6eb039d9-d2f2-4d6d-876b-02899824cf8c_800x513.png)

#### Analyze Github Repo

The .Net Core repository will be analyzed in the article demonstration.

**[ssukhpinder/QRCodeExample](https://github.com/ssukhpinder/QRCodeExample "https://github.com/ssukhpinder/QRCodeExample")**[  
](https://github.com/ssukhpinder/QRCodeExample "https://github.com/ssukhpinder/QRCodeExample")_[How to generate QR code in .Net Core API. Contribute to ssukhpinder/QRCodeExample development by creating an account on…](https://github.com/ssukhpinder/QRCodeExample "https://github.com/ssukhpinder/QRCodeExample")_[github.com](https://github.com/ssukhpinder/QRCodeExample "https://github.com/ssukhpinder/QRCodeExample")

As a prerequisite, analysis requires a sonar scanner tool installed globally using the following command:

```
dotnet tool install --global dotnet-sonarscanner
```

#### Execute the Scanner (.Net Core)

Running a SonarQube analysis is straightforward. Need to execute the following commands at the root of your solution.

```
dotnet sonarscanner begin /k:"demo" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="ce4c4c9c407be98e8150822abbfe017b8c576848"

dotnet build

dotnet sonarscanner end /d:sonar.login="ce4c4c9c407be98e8150822abbfe017b8c576848"
```

#### Execute the Scanner (.Net Framework)

Running a SonarQube analysis is straightforward. Need to execute the following commands at the root of your solution.

```
SonarScanner.MSBuild.exe begin /k:"demo" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="43ca541c658f2f5f856e1b2a707c122b21c02ac1"

MsBuild.exe /t:Rebuild

SonarScanner.MSBuild.exe end /d:sonar.login="43ca541c658f2f5f856e1b2a707c122b21c02ac1"
```

#### Sample Report

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac84b18-fb2c-403c-bd1e-3c539f4a5dce_800x399.png)](https://cdn.substack.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac84b18-fb2c-403c-bd1e-3c539f4a5dce_800x399.png)

Thank you for reading and hope you liked the article. Follow me on [LinkedIn](https://www.linkedin.com/in/sukhpinder-singh-532284a2/) [Instagram](https://www.instagram.com/sukhpindersukh/) [Facebook](https://www.facebook.com/sukhpinder.singh.52/) [Twitter](https://twitter.com/sukhsukhpinder)

#### **Stay tuned on C#**

[C Sharp Programming](https://medium.com/c-sharp-progarmming "https://medium.com/c-sharp-progarmming")