Log inSign up

SonarQube code quality server with PostgreSQL

Raise vm.max_map_count to 524288 and the open file limit, then start the sonarqube:community image together with a PostgreSQL container from one Compose file. Publish port 9000 only on localhost, put an HTTPS proxy in front, change the default admin password at first login and run analyses with the scanner and a project token.

Topic: Developer tools and CI · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What does SonarQube need from the server?

SonarQube runs three Java processes (web server, compute engine and an embedded Elasticsearch), so it needs at least 4 GB of memory, two or more vCPUs and a kernel tuned for Elasticsearch. Plan for:

  • Memory: 4 GB as a minimum for small teams; 8 GB keeps analysis of larger code bases from stalling the UI.
  • Disk: NVMe storage helps, since the search index writes heavily during analysis.
  • Database: PostgreSQL. The embedded H2 database exists only for evaluation and cannot be upgraded or migrated later.
  • Docker: install Docker Engine and the Compose plugin as described in the Docker Engine tutorial, or start from the Docker image.

The free edition is called SonarQube Community Build. It analyses the main branch of each project; branch and pull request analysis belong to the paid editions.

Which kernel settings does SonarQube require?

Set vm.max_map_count to at least 524288 and fs.file-max to at least 131072 on the host, because the container shares the host kernel and Elasticsearch refuses to start with the default of 65530.

sudo tee /etc/sysctl.d/99-sonarqube.conf >/dev/null <<'EOF'
vm.max_map_count=524288
fs.file-max=131072
EOF
sudo sysctl --system
sysctl vm.max_map_count fs.file-max

The file in /etc/sysctl.d keeps the values after a reboot. The per process limits (open files and processes) are set for the container in the Compose file below, so you do not need to edit limits.conf for a Docker setup.

What goes into the SonarQube compose.yaml?

Create a project folder with an .env file for the database password and a compose.yaml with both services. Named volumes keep data, plugins and logs across container upgrades.

mkdir -p ~/sonarqube && cd ~/sonarqube
echo "PG_PASS=$(openssl rand -hex 24)" > .env
chmod 600 .env
cat > compose.yaml <<'EOF'
services:
  sonarqube:
    image: sonarqube:community
    depends_on:
      - db
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: ${PG_PASS}
    volumes:
      - sq_data:/opt/sonarqube/data
      - sq_extensions:/opt/sonarqube/extensions
      - sq_logs:/opt/sonarqube/logs
    ports:
      - "127.0.0.1:9000:9000"
    ulimits:
      nofile:
        soft: 131072
        hard: 131072
      nproc: 8192
    restart: unless-stopped
  db:
    image: postgres:17
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: ${PG_PASS}
      POSTGRES_DB: sonar
    volumes:
      - pg_data:/var/lib/postgresql/data
    restart: unless-stopped
volumes:
  sq_data:
  sq_extensions:
  sq_logs:
  pg_data:
EOF

Binding port 9000 to 127.0.0.1 matters: Docker writes its own packet filter rules, so a published port on all interfaces would bypass ufw. Check the supported PostgreSQL versions in the requirements page of the release you run before changing the database tag.

How do you start SonarQube and log in?

Start the stack and follow the log until the server reports that it is operational; the first start takes a minute or two while the schema is created.

cd ~/sonarqube
docker compose up -d
docker compose logs -f sonarqube    # wait for "SonarQube is operational", then Ctrl+C
curl -s http://127.0.0.1:9000/api/system/status

The status call should return "status":"UP" together with the version number. Open the UI through an SSH tunnel (ssh -L 9000:127.0.0.1:9000 deploy@198.51.100.24) or through the proxy from the next step, log in as admin with the password admin and set a new password when prompted. Under Administration, Configuration, General fill in the Server base URL with the public HTTPS address.

How do you add HTTPS and close the firewall?

Proxy a host name such as sonar.example.net to 127.0.0.1:9000 with Caddy or Nginx, and leave only SSH and the web ports open.

sonar.example.net {
    reverse_proxy 127.0.0.1:9000
}

Caddy setup is explained in Caddy HTTPS. If you prefer Nginx, raise client_max_body_size to around 50m, since analysis reports are uploaded as one request. Firewall rules:

# Ubuntu and Debian
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable

# Rocky and Alma
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload

How do you analyse a project?

Create the project in the UI, generate a token for it and run the scanner from your CI job or workstation. Under My Account, Security you can also create a global analysis token for a CI system that scans many projects. The scanner ships as a container image, so no local Java is needed:

cd ~/src/myapp
docker run --rm \
  -e SONAR_HOST_URL="https://sonar.example.net" \
  -e SONAR_TOKEN="squ_your_token_here" \
  -v "$PWD:/usr/src" \
  sonarsource/sonar-scanner-cli \
  -Dsonar.projectKey=myapp -Dsonar.sources=.

For Maven and Gradle projects use the matching scanner plugin instead, which reads source paths from the build. Store the token as a masked secret in your CI system; a Jenkins job, a GitLab Runner pipeline or a GitHub Actions workflow on a self-hosted runner can all call the same command. The scanner fails the build if you add -Dsonar.qualitygate.wait=true and the quality gate is red.

How do you back up and upgrade SonarQube?

The database holds projects, issues and settings, and the extensions volume holds any plugins you added. Dump the database before every upgrade:

cd ~/sonarqube
docker compose exec -T db pg_dump -U sonar sonar | gzip > ~/sonar-$(date +%F).sql.gz

To upgrade, pull the new image and recreate the container:

docker compose pull
docker compose up -d
docker compose logs -f sonarqube

If the log asks for a database migration, open https://sonar.example.net/setup and start it there. Read the release upgrade notes when you skip several versions, since some jumps need an intermediate stop. Pin a specific version tag instead of community if you want upgrades only when you choose them.

What if SonarQube does not start?

Read the container log first; Elasticsearch errors are the usual cause.

  • max virtual memory areas too low: the sysctl step did not apply; run sysctl vm.max_map_count again.
  • Container exits with code 137: the kernel killed it for lack of memory; move to a plan with more RAM as shown in moving to a larger plan.
  • Cannot connect to database: check that the password in .env matches the one the PostgreSQL volume was created with; changing it later does not alter an existing database.
  • Analysis upload fails with 413: raise the request body limit in the reverse proxy.

Usage monitoring helps you see memory pressure over time.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A2-4small team, a few projects2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
A4-8larger code bases and frequent scans4 vCPU, 8 GB RAM, 180 GB NVMe$28.80/mo$86.40 for 3 months

Questions about SonarQube code quality

Can I install SonarQube without Docker?

Yes. Download the Community Build zip, install a supported Java runtime, create a sonarqube user and a systemd unit, and point sonar.properties at PostgreSQL. The kernel settings are the same. Docker keeps upgrades simpler.

Why does SonarQube refuse to run as root?

Elasticsearch does not start as root. The official image already runs as an unprivileged user; with the zip install create a dedicated account.

Does Community Build analyse pull requests?

No. It analyses one main branch per project. You can still scan every pull request in CI and read the result, but it overwrites the main branch result.

How many lines of code can it handle?

Community Build has no line limit. Memory and CPU decide how fast large projects analyse.

Can I use an existing PostgreSQL server?

Yes. Create a sonar database and role there, remove the db service and point SONAR_JDBC_URL at the host. See the PostgreSQL guide.