Setup and Run SonarQube on Mac

Published: Aug 31, 2020
Updated: Jun 21, 2022

Many projects these days have a dedicated SonarQube stage in their CI/CD pipelines. Yet, sometimes it’s useful to run SonarQube locally to shorten the feedback loop of a) change code, b) run scan, c) view results.

SonarQube has 2 main parts: SonarQube Server, which is a server that displays scan results (among other things), and Sonar Scanner, which does the actual scanning.

SonarQube Version 9.4.0 #

Prerequisites #

  1. Java 11 is installed

Setup #

Install SonarQube Server #

mkdir -p ~/bin && \
mkdir -p ~/tmp && \
cd ~/tmp && \
curl -O && \
tar -x -z -f && \
cp -R sonarqube- ~/bin/

Install Sonar Scanner #

mkdir -p ~/bin && \
mkdir -p ~/tmp && \
cd ~/tmp && \
curl -O
tar -x -z -f && \
cp -R sonar-scanner- ~/bin/

Add Shell Helper #

Add the following to your shell config file then restart Terminal:

export SONARQUBE_PATH="${HOME}/bin/sonarqube-"
export SONAR_SCANNER_PATH="${HOME}/bin/sonar-scanner-"

sonar_console() {
  "${SONARQUBE_PATH}" console

sonar_scan() {
  local token="${1}"
  local current_dir="$(basename $(pwd))"
  local current_commit="$(git rev-parse --short HEAD)"

  # Optional: Include lombok
  local lombok_jar_version="1.18.18" # Change this to your version
  local lombok_jar_path="$(find ${HOME}/.gradle/caches -name lombok-${lombok_jar_version}.jar)"

  echo "token=${token}"
  echo "current_dir=${current_dir}"
  echo "current_commit=${current_commit}"

  # Optional: Include lombok
  echo "lombok_jar_version=${lombok_jar_version}"
  echo "lombok_jar_path=${lombok_jar_path}"

  if [[ -z "${token}" ]] ; then
    echo ""
    echo "The token is required as the 1st arg. Exiting"
    return 1

  echo ""

  -Dsonar.login="${token}" \
  -Dsonar.projectKey="${current_dir}" \
  -Dsonar.projectName="${current_dir}" \
  -Dsonar.projectVersion="${current_commit}" \"build/classes" \"${lombok_jar_path}" # Optional: Include lombok

Generate a Token #

  1. Run sonar_console then wait for it to startup
  2. Go to http://localhost:9000
  3. Login with default credentials of admin / admin then reset your password
  4. Go to http://localhost:9000/projects/create?mode=manual
  5. Enter temp for Project display name and Project key
  6. Click Set Up
  7. Go to http://localhost:9000/dashboard?id=temp&selectedTutorial=manual
  8. Enter temp for your token name
  9. Click Generate
  10. Copy the generated token

Usage #

  1. cd the project that you want to run SonarQube on
  2. Optionally, run the unit tests for the repo. You only have to do this if you want SonarQube to report on test coverage. Otherwise, you can skip this step and just address the Code Smells and such reported by SonarQube
  3. Run sonar_scan <TOKEN> to start the scanner. Replace <TOKEN> with your actual token
  4. Once the scan is complete, view the results at http://localhost:9000/projects

SonarQube Version 8.4.2 #

Prerequisites #

Install sonarqube #

  1. Download sonarqube from
  2. Extract the zip file to ~/bin

Install sonar-scanner #

  1. Download sonar-scanner from
  2. Extract the zip file to ~/bin

Add Shell Helper #

Add the following helper functions to your shell config file then restart Terminal


sonar_start() {

sonar_status() {
  ${SONARQUBE_PATH} status

sonar_stop() {

sonar_scan() {
  current_dir=$(basename $(pwd))
  echo "current_dir=${current_dir}"

  current_commit=$(git rev-parse --short HEAD)
  echo "current_commit=${current_commit}"

  # Optional: Include lombok
  lombok_jar_path=$(find ${HOME}/.gradle/caches -name lombok-1.18.10.jar) # Change this to your version
  echo "lombok_jar_path=${lombok_jar_path}"

  echo ""

  -Dsonar.projectKey=${current_dir} \
  -Dsonar.projectName=${current_dir} \
  -Dsonar.projectVersion=${current_commit} \
  -Dsonar.source=. \ \${lombok_jar_path} # Optional: Include lombok

Start sonarqube server #

  1. Start server

  2. Check server status


Run a scan with sonar-scanner #

  1. Navigate to the repo that you want to run a scan on

  2. Run all unit tests so that sonar will report test coverage

  3. Run a scan


View scan results #

  1. Navigate to http://localhost:9000
  2. Click Projects

Notes #