How to Use SonarQube on Ubuntu 22.04 LTS

Updated on October 31, 2022
How to Use SonarQube on Ubuntu 22.04 LTS header image


SonarQube is an open-source tool for code quality analysis. It can scan source code for potential bugs and vulnerabilities and generates a report which allows you to identify issues. It scans up to 30 programming languages.

SonarQube has two parts: a scanner application on the local machine to scan the code and a server application for keeping records.

This guide explains how you can install and use SonarQube on a Ubuntu 22.04 server.


1. Configure Firewall

SonarQube web tool needs HTTP and HTTPS ports to work.

Open them using the Uncomplicated Firewall (UFW).

$ sudo ufw allow http
$ sudo ufw allow https

Check the firewall status.

$ sudo ufw status

2. Install OpenJDK

Install OpenJDK 11.

$ sudo apt install openjdk-11-jdk

3. Install PostgreSQL

Import the PostgreSQL repository key.

$ curl | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/ >/dev/null

Add the PostgreSQL repository.

$ sudo sh -c 'echo "deb $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Update the system repository list.

$ sudo apt update

Install PostgreSQL 14.

$ sudo apt install postgresql postgresql-contrib

Check the status of the PostgreSQL service.

$ sudo systemctl status postgresql

4. Configure PostgreSQL

Log in to the PostgreSQL shell.

$ sudo -u postgres psql

Create the sonaruser role.

postgres=# CREATE ROLE sonaruser WITH LOGIN ENCRYPTED PASSWORD 'your_password';

Create the sonarqube database.

postgres=# CREATE DATABASE sonarqube;

Grant all privileges on the sonarqube database to the sonaruser role.

postgres=# GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonaruser;

Exit the shell.

postgres=# \q

Return to your default user account.

$ exit

5. Install Sonarqube

Copy the URL of the latest version of the community edition from the SonarQube downloads page.

Download SonarQube using the URL copied above.

$ wget

Unzip the downloaded archive.

$ unzip -q

Move the files to the /opt/sonarqube directory.

$ sudo mv sonarqube- /opt/sonarqube

Delete the downloaded archive.

$ rm

6. Create SonarQube User

Create a system user along with the group for SonarQube.

$ sudo adduser --system --no-create-home --group --disabled-login sonarqube

Give Sonar user permissions to the /opt/sonarqube directory.

$ sudo chown sonarqube:sonarqube /opt/sonarqube -R

7. Configure SonarQube Server

Open the SonarQube configuration file for editing.

$ sudo nano /opt/sonarqube/conf/

Find the following lines.


Uncomment them by removing the hash in front of them and adding the database credentials created in step 4.


Find the following line.


Uncomment it and replace the existing value with the following.


Find the following lines.


Configure the following settings, so SonarQube listens to localhost only because Nginx handles the external connections.


Save the file by pressing Ctrl+X, then Y.

Increase the virtual memory on the system for Elasticsearch to function. Open the sysctl.conf file for editing.

$ sudo nano /etc/sysctl.conf

Paste the following lines at the end of the file.


Save the file by pressing Ctrl+X, then Y.

Create the file /etc/security/limits.d/99-sonarqube.conf and open it for editing.

$ sudo nano /etc/security/limits.d/99-sonarqube.conf

Paste the following lines to increase the file descriptors and threads that the sonarqube user can open.

sonarqube   -   nofile   131072
sonarqube   -   nproc    8192

Save the file by pressing Ctrl+X, then Y.

Reboot the system to apply the changes.

$ sudo reboot

8. Setup Sonar Service

Create the systemd service file for Sonar and open it for editing.

$ sudo nano /etc/systemd/system/sonarqube.service

Paste the following code in it.

Description=SonarQube service


ExecStart=/opt/sonarqube/bin/linux-x86-64/ start
ExecStop=/opt/sonarqube/bin/linux-x86-64/ stop




Save the file by pressing Ctrl+X, then Y.

Start the SonarQube service.

$ sudo systemctl start sonarqube

Check the status of the service.

$ sudo systemctl status sonarqube

Enable the service to start automatically at boot.

$ sudo systemctl enable sonarqube

Verify if the Sonarqube server is functioning properly.

$ curl

Look for the following text in the HTML response.

    window.baseUrl = '';
    window.serverStatus = 'UP';
    window.instance = 'SonarQube';
    window.official = true;

This confirms everything is working fine.

9. Install Nginx

Install dependencies required to install Nginx.

$ sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y

Import Nginx's GPG signing key.

$ curl | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

Add a repository for Nginx's stable version.

$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg arch=amd64] \ `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list

Update the system repository list.

$ sudo apt update

Install Nginx.

$ sudo apt install nginx

Start the Nginx server.

$ sudo systemctl start nginx

10. Install SSL

Issue the following commands to ensure that you have the latest version of snapd required to install Certbot.

$ sudo snap install core
$ sudo snap refresh core

Install Certbot.

$ sudo snap install --classic certbot

Create a symlink for Certbot to the /usr/bin directory.

$ sudo ln -s /snap/bin/certbot /usr/bin/certbot

Issue the SSL Certificate.

$ sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m -d

Generate a Diffie-Hellman group certificate.

$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096

Do a dry run of the SSL renewal process to ensure it works.

$ sudo certbot renew --dry-run

11. Configure Nginx

Open the file nginx.conf for editing.

$ sudo nano /etc/nginx/nginx.conf

Find the line include /etc/nginx/conf.d/*.conf; and paste the following code below it.

server_names_hash_bucket_size  64;

Save the file by pressing Ctrl+X, then Y.

Create the Sonar configuration file for Nginx and open it for editing.

$ sudo nano /etc/nginx/conf.d/sonar.conf

Paste the following code in it.

# Redirect HTTP to HTTPS
server {
    listen 80 default_server;
    return 301 https://$server_name$request_uri;

server {
    listen 443 ssl http2;

    http2_push_preload on; # Enable HTTP/2 Server Push

    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;
    ssl_trusted_certificate /etc/letsencrypt/live/;
    ssl_session_timeout 1d;
    ssl_protocols TLSv1.2 TLSv1.3;

    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;

    access_log /var/log/nginx/sonarqube.access.log main;
    error_log  /var/log/nginx/sonarqube.error.log;

    location / {
        proxy_set_header Connection "";
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;

Save the file by pressing Ctrl+X, then Y.

Verify Nginx configuration syntax.

$ sudo nginx -t

Restart the Nginx service.

$ sudo systemctl restart nginx

12. Secure SonarQube

Visit the URL and log in using the username and password admin.

Change your password on the next page.

Click the Administration tab, select Security from the list, and click the Users drop-down option.

SonarQube Users Menu

To improve security, create another user to use for scanning code by clicking the Create User button.

SonarQube Create User Popup

Click the button in the Tokens column against the newly created user.

Click the Update Tokens button, enter the token name, and click the Generate button to create a new token for the user. Copy and save the token.

SonarQube User Token Dialog

SonarQube comes with the ability to encrypt settings and passwords. Visit Administration >> Configuration >> Encryption and click the Generate Secret Key button to generate a unique secret key. Copy the saved key to use later.

SonarQube Secret Key Page

Open the Sonar configuration file for editing.

$ sudo /opt/sonarqube/conf/

Enter the following line at the end of the file.


Save the file by pressing Ctrl+X, then Y.

Create the Sonar secret key file and open it for editing.

$ sudo nano /opt/sonarqube/conf/sonar-secret.txt

Paste your secret key into it. Save the file by pressing Ctrl+X, then Y.

Restrict the secret file to the sonarqube user.

$ sudo chown sonarqube:sonarqube /opt/sonarqube/conf/sonar-secret.txt

Restart the SonarQube server.

$ sudo systemctl restart sonarqube

Visit the Administration >> Configuration >> Encryption section again and fill in your database password. Press the Encrypt button to generate the encrypted password.

SonarQube Encrypt Properties Page

Replace the actual password in the file with the encrypted version, and restart the server. Repeat the process with any other property you want to encrypt.

13. Install SonarQube's Code Scanner

SonarQube provides various scanners depending on the programming language. Install the Command line version of the Sonarscanner.

Download the scanner.

$ wget

Extract the archive.

$ sudo unzip

Move the directory to /opt/sonarscanner.

$ sudo mv sonar-scanner- /opt/sonarscanner

Switch to the directory.

$ cd /opt/sonarscanner

Open the file for editing.

$ sudo nano conf/

Find the following line and un-comment it.

Change its value and replace it with the server URL.

Save the file by pressing Ctrl+X, then Y.

Make the scanner binary file executable.

$ sudo chmod +x bin/sonar-scanner

Create a symbolic link to the binary to make it accessible from anywhere.

$ sudo ln -s /opt/sonarscanner/bin/sonar-scanner /usr/local/bin/sonar-scanner

14. Scan SonarQube Example Projects

You can test the scanner by running it on SonarQube example projects.

Create a new directory for project testing and switch to it.

$ mkdir ~/sonar-example-test && cd ~/sonar-example-test

Download the example project.

$ wget

Extract the project files.

$ unzip

Switch to the example project directory.

$ cd sonar-scanning-examples-master/sonarqube-scanner

Run the scanner on the code. Pass the token you created before.

$ sonar-scanner -D sonar.login=<YourLoginToken>

You get the following output after the scan is complete.

INFO: Analysis total time: 20.621 s
INFO: ------------------------------------------------------------------------
INFO: ------------------------------------------------------------------------
INFO: Total time: 39.678s
INFO: Final Memory: 27M/94M
INFO: ------------------------------------------------------------------------

Visit the SonarQube dashboard to view the project report.

SonarQube Project Report Detail

15. Scan Your Code

Transfer the project to your server.

Switch to your project's root directory.

$ cd ~/myproject

Create and open the SonarQube configuration file.

$ nano

Define a project key for your project. The chosen key should be unique for your SonarQube instance.

# Unique ID for the project

Enter the project name and version to show up in the SonarQube dashboard.

sonar.projectName=First Project
sonar.projectDescription=My First Project

Enter the location of the project files. The location is relative to the directory in which the configuration file is present.


Enter the location of the files you don't want to scan.


Set the level of logs produced by the scanner. You can skip the property if you want to use the default INFO log level.


If you are hosting the project on your server, paste the following line to disable checking for a Source Code Management (SCM) provider.


Save the file by pressing Ctrl+X, then Y.

Run the code scanner by passing your login token.

$ sonar-scanner -D sonar.login=<YourLoginToken>


You have successfully installed and used SonarQube on your Ubuntu 22.04 server. For details, you can check out the following resources.