This is a recipe for installing SonarQube as a local service on Windows for use with the Windows Linux Subsystem- Version 2 (WSL2).
Assume that Docker Desktop has been installed and is running under Windows. For details on how to turn on WSL2 and integrate Microsoft Visual Code (as an interface to WSL2) consider the following link:
To install the SonarQube image and launch the container for the first time:
# Note: the docker network is required if you choose to use a docker sonar-scanner (discussed later), it is optional otherwise.
docker network create sonar_network
# To pull the official SonarQube Docker image, run the following commands.
# Setting up volumes is not necessary but useful later on if you want to
# install a real database (vs. the simple file-based one that comes with
# sonarqube).
docker volume create sonarqube_data
docker volume create sonarqube_logs
docker volume create sonarqube_extensions
docker run -d \
--name sonarqube \
-p 9000:9000 \
-v sonarqube_data:/opt/sonarqube/data \
-v sonarqube_logs:/opt/sonarqube/logs \
-v sonarqube_extensions:/opt/sonarqube/extensions \
sonarqube:latest
# Run this command to confirm the setup was successful.
docker inspect sonarqube
For more information on the docker run command line: docker run | Docker Docs.
For more information on the dockerhub sonarqube image.
Once the SonarQube container is running, you will need to log into your account page at:
http://localhost:9000/account
Use admin:admin (these are the initial default credentials) - you will be asked to change these.
Select Create a Local Project.
Define the Project Display Name and the Project Key.
Select the baseline for new code for this project and Create the project:
Select the Analysis Method:
Analyze your project, and generate a TOKEN (keep track of that token). Select:
You will then need to run the SonarQube scanner and analyze your project to finalize its SonarQube setup. There are two ways to install and run the sonar-scanner, either as a local download or using a Docker container.
Notes to install the command line Scanner for Linux (to run on WLS2):
Download and unzip the scanner (see the SonarQube Run analysis page).
If there are any issues, look at the logs using:
docker logs sonarqube
In your .bashrc, define the following:
export INSTALL_DIRECTORY=/home/.../sonar-scanner-5.0.1.3006-linux
export PATH=$PATH:$INSTALL_DIRECTORY/bin
Create the sonar-scanner.properties, an example configuration that I used:
Test your scanner setup by running:
sonar-scanner -h
To run the scanner:
sonar-scanner -X \
-Dsonar.token=TOKEN
When run, the sonar-scanner must be able to find the sonar-scanner.properties.
Alternatively, you can run the scanner using Docker. Set-up notes below.
docker run --rm \
-e SONAR_HOST_URL="http://sonarqube:9000" \
-e SONAR_SCANNER_OPTS="-Dsonar.projectKey=TestSonarqube" \
-e SONAR_LOGIN="TOKEN" \
-v "/home/ncombs/Dev/Sonarqube_a_experiment/src":/usr/src \
--network sonar_network \
sonarsource/sonar-scanner-cli