Latest
Sonar 4 min read

Install SonarQube on Ubuntu Step by Step

Install SonarQube on Ubuntu Step by Step
Sonar · November 30, 2026

Version note: this guide uses SonarQube Server 2026.1 LTA documentation. Choose a supported patched release and check its requirements. These deployment commands were not tested on a live host.

What this guide covers

SonarQube is a server that stores code-analysis results. This guide installs it directly on Linux, uses PostgreSQL to store its data, and uses Nginx to provide an HTTPS address. Docker is not used.

Before you start

Use a fresh Ubuntu Server 24.04 host with sudo access. This is a new installation, not an upgrade guide. Do not initialize or replace an existing database.

You also need:

  • A SonarQube Server ZIP and the license required by your edition. Community Build is a separate product with its own instructions.
  • JDK 21 and PostgreSQL 14–18 for the documented 2026.1 release. Recheck these requirements when choosing another release.
  • At least the vendor’s small-install starting point of 2 CPU cores, 4 GB RAM and 30 GB disk, plus space and memory for PostgreSQL and Nginx. Larger projects need more.
  • A real DNS name pointing to this host and a trusted certificate for it. Replace sonar.example.com below.

All shell commands run on the SonarQube host. Keep ports 9000 and 5432 private; only Nginx needs public web access.

Step 1: Install the required packages

Install Java, PostgreSQL and Nginx:

sudo apt update
sudo apt install openjdk-21-jdk-headless postgresql postgresql-contrib \
  nginx unzip fontconfig libfreetype6 curl
java -version
psql --version
sudo systemctl enable --now postgresql

Confirm java -version selects Java 21 and psql --version is in the supported range. PostgreSQL should be active after the last command.

Step 2: Create the database and login

Open PostgreSQL as its administrator:

sudo -u postgres psql

At the postgres=# prompt, run:

SET password_encryption = 'scram-sha-256';
CREATE ROLE sonar LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION;
CREATE DATABASE sonar OWNER sonar ENCODING 'UTF8' TEMPLATE template0;
REVOKE ALL ON DATABASE sonar FROM PUBLIC;
GRANT CONNECT, TEMPORARY ON DATABASE sonar TO sonar;
SHOW config_file;
SHOW hba_file;
SHOW listen_addresses;
SHOW port;

Still at that prompt, type \password sonar, enter a new strong password twice, then type \q to return to the shell. Save this password securely; Step 6 uses it. The sonar account owns this database but is not a PostgreSQL administrator.

Step 3: Allow local password connections

The previous queries print the actual configuration-file paths. Use sudoedit to edit the reported postgresql.conf. Set listen_addresses = '127.0.0.1' and port = 5432.

In the reported pg_hba.conf, add this line before broader host rules:

host sonar sonar 127.0.0.1/32 scram-sha-256

This allows only the sonar login to reach the sonar database on the local IPv4 address. scram-sha-256 is PostgreSQL’s password-authentication method. Do not replace the whole file or use trust.

Step 4: Test the database before continuing

Return to the shell and run:

sudo -u postgres psql -c "SELECT line_number, error FROM pg_hba_file_rules WHERE error IS NOT NULL;"
sudo systemctl restart postgresql
sudo systemctl is-active postgresql
psql -h 127.0.0.1 -p 5432 -U sonar -d sonar -W \
  -c 'SELECT current_user, current_database(), inet_server_addr();'
sudo ss -ltnp | grep ':5432'

The first query should return no configuration errors. PostgreSQL should report active. Enter the password from Step 2 when prompted. The connection query should return user sonar, database sonar and address 127.0.0.1. Do not continue if any check fails.

Step 5: Set Linux limits and extract SonarQube

SonarQube’s search process needs enough file handles and memory mappings. This sets the documented minimum values without lowering larger existing values:

MAP_COUNT=$(sysctl -n vm.max_map_count)
FILE_MAX=$(sysctl -n fs.file-max)
[ "$MAP_COUNT" -ge 524288 ] || MAP_COUNT=524288
[ "$FILE_MAX" -ge 131072 ] || FILE_MAX=131072
printf 'vm.max_map_count=%s\nfs.file-max=%s\n' "$MAP_COUNT" "$FILE_MAX" \
  | sudo tee /etc/sysctl.d/99-sonarqube.conf
sudo sysctl --system
sysctl vm.max_map_count fs.file-max

Check that the final values are at least 524288 and 131072 respectively.

Download your chosen supported ZIP from the official SonarSource download page, compare its SHA-256 checksum with the vendor’s published value, and keep it on this host. Then run:

read -r -p 'Verified ZIP path: ' SQ_ZIP
sudo unzip "$SQ_ZIP" -d /opt
read -r -p 'Extracted absolute directory, such as /opt/sonarqube-VERSION: ' SQ_DIR
sudo ln -s "$SQ_DIR" /opt/sonarqube
sudo useradd --system --user-group --home-dir /opt/sonarqube \
  --shell /usr/sbin/nologin sonarqube
sudo install -d -o sonarqube -g sonarqube -m 0750 \
  /var/sonarqube/data /var/sonarqube/temp
sudo chown -R sonarqube:sonarqube "$SQ_DIR"
sudo chmod 0750 "$SQ_DIR"

Enter the ZIP path and the exact extracted directory when asked. Stop if /opt/sonarqube or the sonarqube user already exists. The new account runs SonarQube without root access.

Step 6: Configure the database connection

Open the file:

sudoedit /opt/sonarqube/conf/sonar.properties

Set these properties once, replacing the password placeholder with the password from Step 2:

sonar.jdbc.username=sonar
sonar.jdbc.password=REPLACE_WITH_DATABASE_SECRET
sonar.jdbc.url=jdbc:postgresql://127.0.0.1:5432/sonar
sonar.web.host=127.0.0.1
sonar.web.port=9000
sonar.path.data=/var/sonarqube/data
sonar.path.temp=/var/sonarqube/temp

sonar.web.host=127.0.0.1 keeps the application port private. Nginx will provide public access. If a password contains special characters, follow Java properties-file escaping rules. Protect the file:

sudo chown sonarqube:sonarqube /opt/sonarqube/conf/sonar.properties
sudo chmod 0600 /opt/sonarqube/conf/sonar.properties

Step 7: Create and start the service

systemd starts the application at boot and restarts it after a failure. First run ls /opt/sonarqube/lib/sonar-application-*.jar.

Create /etc/systemd/system/sonarqube.service with sudoedit. Replace INSTALLED_VERSION below with the version in that JAR filename:

[Unit]
Description=SonarQube Server
Wants=network-online.target
After=network-online.target postgresql.service

[Service]
Type=simple
User=sonarqube
Group=sonarqube
WorkingDirectory=/opt/sonarqube
ExecStart=/usr/bin/java -Xms32m -Xmx32m -Djava.net.preferIPv4Stack=true -jar /opt/sonarqube/lib/sonar-application-INSTALLED_VERSION.jar
LimitNOFILE=131072
LimitNPROC=8192
Restart=on-failure
SuccessExitStatus=143
UMask=0027

[Install]
WantedBy=multi-user.target

Then start the service:

sudo systemctl daemon-reload
sudo systemctl enable --now sonarqube
sudo systemctl status sonarqube --no-pager
sudo systemctl show sonarqube -p LimitNOFILE -p LimitNPROC
sudo journalctl -u sonarqube -n 80 --no-pager
curl --fail http://127.0.0.1:9000/api/system/status

Wait until the API reports UP. If it does not, read the journal and /opt/sonarqube/logs/sonar.log, web.log and es.log. Fix database, permission or limit errors before setting up public access.

Step 8: Configure Nginx for HTTPS

A reverse proxy accepts browser HTTPS requests and forwards them to SonarQube’s local port. Install your certificate and private key at the paths below, or update the paths to match your certificate setup. Restrict private-key permissions.

Save this configuration as /etc/nginx/sites-available/sonarqube. Replace both occurrences of sonar.example.com with your real DNS name:

server {
    listen 80;
    server_name sonar.example.com;
    return 301 https://$host$request_uri;
}
server {
    listen 443 ssl;
    server_name sonar.example.com;
    ssl_certificate /etc/sonarqube-tls/fullchain.pem;
    ssl_certificate_key /etc/sonarqube-tls/privkey.pem;
    client_max_body_size 100m;
    location / {
        proxy_pass http://127.0.0.1:9000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_read_timeout 300s;
    }
}

Enable the site and test Nginx before reloading it:

sudo ln -s /etc/nginx/sites-available/sonarqube /etc/nginx/sites-enabled/sonarqube
sudo nginx -t
sudo systemctl enable --now nginx
sudo systemctl reload nginx
sudo ufw status verbose

If UFW is active, preserve your SSH rule, then allow 443/tcp. Allow 80/tcp only if you need the redirect or certificate validation. Check cloud firewall rules too. Do not enable a previously disabled firewall remotely without reviewing access.

Step 9: Complete the first login

From another machine, open https://sonar.example.com using your actual hostname. The browser must accept the certificate without warnings. Sign in with the initial credentials documented for your release and change them immediately. Set the Server base URL to the HTTPS address.

Create a project and project analysis token, then follow the first-analysis guide. Confirm the server completes the analysis. An active Linux service by itself does not prove that analysis works.

Step 10: Create a backup

The PostgreSQL database contains the project history. Create a protected backup:

sudo install -d -o postgres -g postgres -m 0700 /var/backups/sonarqube
sudo -u postgres pg_dump -Fc -d sonar -f /var/backups/sonarqube/sonar-before-change.dump
sudo -u postgres pg_restore --list /var/backups/sonarqube/sonar-before-change.dump

Use a new backup filename on later runs. Copy backups to protected off-host storage and keep configuration, plugins and license information securely. Test restoring into a separate empty database, never over the live database. A readable archive listing is not a restore test.

Check the result

Before using the installation, confirm HTTPS works from the scanner machine, an analysis completes, services recover after a planned reboot, and private ports remain inaccessible from outside. Arrange certificate renewal and tested database recovery. Before upgrading, read the supported upgrade path and take a compatible backup: installing an older ZIP does not undo a database upgrade.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.