Advanced
Searching inInstall guides
How to search
  • Paste an error message in quotes, e.g. "permission denied", to find the fix table that explains it.
  • Choose an area and a part of the guide (install, first settings, daily use, security, fixes) to go straight to the right step.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Guide 47SonarQube · Security

Install and set up SonarQube

SonarQube checks your source code for bugs, security problems (SAST), secrets, code smells, duplication and test coverage — in 30+ languages — and stops a merge with a quality gate when new code is not good enough. This guide runs SonarQube Community Build with PostgreSQL in Docker Compose (the easiest and most common way), sets the Linux kernel limits it needs, secures the first login, puts it behind HTTPS, and connects it to CI with sonar-scanner and Maven.

  • 9 Lessons
  • 14 Commands

$ run as a normal user# run as root (or with sudo)

47.1

Know it before you install

The server (web UI, compute engine and an embedded Elasticsearch) stores results in PostgreSQL. A scanner runs in your CI job, analyses the code and sends the report to the server. A quality profile is the rule set; a quality gate is the pass/fail condition (for example "no new critical issues, coverage on new code ≥ 80 %").

ItemValue
EditionsCommunity Build (free), Developer/Enterprise (branches, PR decoration, more languages)
Imagesonarqube:community (+ postgres:16)
DataDocker volumes for data, extensions, logs + the PostgreSQL volume
Port9000/tcp (web UI and API)
Scannerssonar-scanner (CLI), Maven/Gradle plugins, .NET scanner, CI actions
Kernel needsvm.max_map_count ≥ 524288, fs.file-max ≥ 131072

What you need: Docker Engine (see the Docker guide), 4 GB RAM or more, 2 CPU.

47.2

Prepare the host (RHEL and Ubuntu)

The embedded Elasticsearch needs these kernel settings, or SonarQube stops at start:

cat > /etc/sysctl.d/99-sonarqube.conf <<'EOF'
vm.max_map_count = 524288
fs.file-max = 131072
EOF
sysctl --system
mkdir -p ~/sonarqube && cd ~/sonarqube
printf 'POSTGRES_PASSWORD=%s\n' "$(openssl rand -hex 24)" > .env
chmod 600 .env

47.3

Install with Docker Compose

~/sonarqube/compose.yaml
services:
  db:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_USER: sonar
      POSTGRES_DB: sonarqube
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - pgdata:/var/lib/postgresql/data
 
  sonarqube:
    image: sonarqube:community
    restart: unless-stopped
    depends_on: [db]
    ports:
      - "127.0.0.1:9000:9000"
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonarqube
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: ${POSTGRES_PASSWORD}
    ulimits:
      nofile: { soft: 131072, hard: 131072 }
      nproc: 8192
    volumes:
      - sonar_data:/opt/sonarqube/data
      - sonar_extensions:/opt/sonarqube/extensions
      - sonar_logs:/opt/sonarqube/logs
 
volumes:
  pgdata:
  sonar_data:
  sonar_extensions:
  sonar_logs:
docker compose up -d
docker compose logs -f sonarqube
… SonarQube is operational

Pin a version tag (for example sonarqube:25.10.0.114319-community) in production and update on purpose.

47.4

First configuration: login and safe settings

Open http://localhost:9000 through an SSH tunnel (or Nginx — below). Log in as admin / admin; SonarQube makes you change the password at once.

WhereSettingWhy
Administration → SecurityForce user authentication = on (default)No code or results visible without login.
Administration → Security → UsersPersonal accounts or SSO (SAML/GitHub/GitLab)No shared admin.
Administration → General → Server base URLhttps://sonar.example.comCorrect links in emails and CI.
My Account → SecurityMake tokens for CI (project analysis tokens)Scanners never use passwords.
Quality GatesUse "Sonar way" or your own gate on new codeThe merge stops on new problems; old debt is fixed step by step.

Behind Nginx with HTTPS:

/etc/nginx/conf.d/sonar.example.com.conf
server {
    listen 443 ssl;
    server_name sonar.example.com;
    ssl_certificate     /etc/letsencrypt/live/sonar.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/sonar.example.com/privkey.pem;
    client_max_body_size 50m;
    location / {
        proxy_pass http://127.0.0.1:9000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
    }
}

47.5

Analyse a project from CI

Create the project in the UI (Create project → Manually), get its key and a token. Then in the repository:

sonar-project.properties
sonar.projectKey=shop-api
sonar.sources=src
sonar.tests=tests
sonar.python.coverage.reportPaths=coverage.xml
sonar.qualitygate.wait=true
docker run --rm -e SONAR_HOST_URL=https://sonar.example.com -e SONAR_TOKEN="$SONAR_TOKEN" -v "$PWD":/usr/src sonarsource/sonar-scanner-cli
mvn -B verify sonar:sonar -Dsonar.host.url=https://sonar.example.com -Dsonar.token="$SONAR_TOKEN" -Dsonar.projectKey=shop-java

With sonar.qualitygate.wait=true the scanner waits for the result and fails the CI job when the quality gate fails. Store SONAR_TOKEN as a masked CI secret.

Real uses at work:

  • A quality gate on every pull request (with Developer edition: comments in the PR).
  • Find security hotspots and secrets in code before release.
  • Track coverage and technical debt per team.

47.6

Make it safe

Security checklist:

  • Change the admin password at once; personal accounts or SSO; Force user authentication on.
  • Port 9000 only on 127.0.0.1; HTTPS through Nginx.
  • Tokens per project for CI, stored as CI secrets; revoke unused tokens.
  • Database password in .env (mode 600), never in Git.
  • Back up PostgreSQL (pg_dump) — it holds all results and settings.
  • Update SonarQube regularly (new rules and security fixes); read the upgrade notes.

Commands in this lesson

47.7

Update and remove

docker compose exec db pg_dump -U sonar sonarqube | gzip > /backup/sonar-$(date +%F).sql.gz
docker compose pull
docker compose up -d
docker compose down

After an upgrade, open https://sonar.example.com/setup if SonarQube asks for a database migration.

47.8

Fix common problems

ProblemCauseFix
Stops at start: max virtual memory areas vm.max_map_count [65530] is too lowKernel setting.vm.max_map_count = 524288 (see above), sysctl --system.
Can not connect to databaseWrong JDBC URL or password, DB not ready.Check .env and SONAR_JDBC_*; docker compose logs db.
Scanner: Not authorized. Please check the user tokenWrong or expired token, or missing rights on the project.Make a new token; check project permissions.
Scanner: Project not foundWrong sonar.projectKey.Use the key shown in the project.
Quality gate wait times outThe background task is slow or failed.Administration → Projects → Background Tasks; more RAM.
Out of memory / very slowToo little RAM for big projects.More RAM; SONAR_CE_JAVAOPTS / SONAR_WEB_JAVAOPTS heap settings.
Links point to localhost:9000Server base URL not set.Set it in Administration → General.

47.9

Next steps

1What makes the CI job fail when the quality gate fails?

sonar.qualitygate.wait=true — the scanner waits for the gate result and exits with an error.

2SonarQube stops right after start with a vm.max_map_count error. Why?

Its embedded Elasticsearch needs vm.max_map_count of at least 524288 on the host.

All commands in this chapter