How to Install SonarQube on Ubuntu 22.04

SonarQube or formerly Sonar is an open-source platform for static code analysis and code security. It allows you to perform static code analysis and code quality to detect bugs and enhance application security. It also provides reports such as duplicate code, coding standards, code complexity, and security recommendation.

With sonarQube, you can automate static code analysis for 29 programming languages. You can easily integrate SonarQube with your existing CI/CD tools such as Jenkins, Azure DevOps, or IDE such as IntelliJ and Visual Code Studio.

In this guide, you will learn how to install SonarQube static code analysis on Ubuntu 22.04 server. You will also learn how to install PostgreSQL which will be used as the database for SonarQube and the Nginx web server that will be used as the reverse proxy.

Prerequisites

Before starting this guide, you should have the following:

  • An Ubuntu server 22.04 with UFW firewall enabled.
  • A non-root user with sudo/administrator privileges.
  • A domain name pointed to the Ubuntu server IP address.

Installing Java OpenJDK

Your first step here is to install the Java OpenJDK on your Ubuntu system. The SonarQube server required Java OpenJDK v11 to be installed on your Linux machine.

Before start installing packages, run the following apt command to update and refresh your Ubuntu package index repository.

sudo apt update

Then, install the Java OpenJDK v11 using the following apt command. The default OpenJDK version on the latest Ubuntu 22.04 is Java OpenJDK v11.

Input Y when prompted to confirm the installation and press ENTER to proceed.

sudo apt install default-jdk

install java

Once Java OpenJDK is installed, verify the Java version using the following java command. You will see the output of the Java version that is installed on your system.

java -version

check java

Installing PostgreSQL Database System

SonarQube supports multiple database systems such as PostgreSQL, Microsoft SQL Server, and oracle database. For this example, you will use PostgreSQL as the database for your SonarQube installation.

At the time of this writing, the latest version of SonarQube required at least PostgreSQL v9.6. In this guide, you will install PostgreSQL v13 from the official PostgreSQL repository.

First, add the GPG key of the PostgreSQL repository using the following command.

wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

Now, add the PostgreSQL repository for the Ubuntu system using the below command.

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

Then, update and refresh your ubuntu package index.

sudo apt update

add postgresql repository

Now, install the PostgreSQL database v13 via the apt command below. Input Y when prompted to confirm the installation, then press ENTER to proceed.

sudo apt install postgresql-13

install postgresql

Once PostgreSQL is installed, run the following command to verify the 'postgresql' service and to make sure the service is running.

sudo systemctl is-enabled postgresql
sudo systemctl status postgresql

You will see the output of the 'postgresql' service is enabled, which means will be run automatically at system boot. And the current status of the 'postgresql' service is running.

check postgresql

With the PostgreSQL installed on your server, you are ready to set up a new database and user for the SonarQube via the PostgreSQL shell.

Run the following command to log in to the PostgreSQL shell.

sudo -u postgres psql

Now, run the following PostgreSQL queries to create a new database and user for SnonarQube. In this example, you will create the PostgreSQL database and user 'sonarqube'. And be sure to change the password with a strong password.

CREATE USER sonarqube WITH PASSWORD 'Password';
CREATE DATABASE sonarqube OWNER sonarqube;
GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonarqube;

create database

Next, run the following queries to check the list of databases and users on the PostgreSQL server.

\l
\du

If your database and user is created, you will see the database 'sonarqube' on the list of databases, and the user 'sonarqube' on the list of users.

check database and user

Lastly, log out from PostgreSQL using the query below.

\q

Setting up System

To install SonarQube on a Linux system, you must have a dedicated user that will be running SonarQube and some additional configurations such as ulimit and kernel parameters.

Now, you will create a new user for SonarQube, and set up custom kernel parameters via sysctl.conf file, and set up ulimit.

Run the following command to create a new user 'sonarqube' on your system.

sudo useradd -b /opt/sonarqube -s /bin/bash sonarqube

Next, open the file /etc/sysctl.conf using nano editor.

sudo nano /etc/sysctl.conf

Add the following configuration to the bottom of the line. The SonarQube required the kernel parameter vm.max_map_count to be greater than '524288' and the fx.file-max to be greater than '131072'.

vm.max_map_count=524288
fs.file-max=131072

Save the file and exit the editor when you are finished.

Now, run the sysctl command below to apply new changes on the '/etc/sysctl.conf' file.

sudo sysctl --system

In the following output, you can see the new kernel parameters is applied.

apply sysctl

Next, run the following command to set up ulimit for the SonarQube. This will take temporary effects on your system, when the system is rebooted, the ulimits will revert to default.
ulimit -n 131072
ulimit -u 8192

To make ulimit configuration permanently, create a new config file '/etc/security/limits.d/99-sonarqube.conf' using the following command.

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

Add the following configuration to the file.

sonarqube   -   nofile   131072
sonarqube   -   nproc    8192

Save the file and close the editor when you are finished.

Now that you have completed the configuration of your Ubuntu system for SnonarQube installation. You will be downloading the SonarQube package and setting up SonarQube installation in the next step.

Downloading SonarQube Package

The SonarQube can be installed in two different ways, via zip file and Docker image. In this example, you will install the SonarQube via the zip file package that you will download from the official SonarQube download page.

At the time of this writing, the SonarQube latest version v9.6.1, which you will be installing in the following steps.

Before downloading the SonarQube package, run the following apt command to install a basic package such as unzip and wget.

sudo apt install unzip software-properties-common wget

Now, download the SonarQube package via the wget command below.

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.6.1.59531.zip

After the SonarQube is downloaded, you will see the zip file 'sonarqube-9.6.1.59531.zip' on your working directory.

Extract the SonarQube package using the unzip command below. You should get a new directory 'sonarqube-9.6.1.59531' where the SonarQube package is stored.

unzip sonarqube-9.6.1.59531.zip

Move the directory 'sonarqube-9.6.1.59531' to the '/opt/sonarqube' using the below command.

mv sonarqube-9.6.1.59531 /opt/sonarqube

Lastly, change the ownership of the SonarQube installation directory '/opt/sonarqube' to the user 'sonarquba' via the chown command as below.

sudo chown -R sonarqube:sonarqube /opt/sonarqube

Now you have downloaded the SonarQube package to the installation directory '/opt/sonarqube'. Next, you will configure your SonarQube installation, then set up a systemd service file for SonarQube.

download sonarqube

Configuring SonarQube

After downloading the SonarQube package, you will set up the SonarQUbe installation by editing the default config file '/opt/sonarqube/conf/sonar.properties'.

You will add the PostgreSQL database details, set up the max memory heap for the Elasticsearch process, and set up the web host and port for the SonarQube service via the file '/opt/sonarqube/conf/sonar.properties'. And lastly, you will set up SonarQube as a systemd service.

Now, open the SonarQube configuration file '/opt/sonarqube/conf/sonar.properties' using nano editor.

nano /opt/sonarqube/conf/sonar.properties

For the database configuration, uncomment some of the following options and change the default value using your database details.

sonar.jdbc.username=sonarqube
sonar.jdbc.password=Password

sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube

Now, uncomment the following configuration to set up the max heap memory size for the elasticsearch process. In his example, the max heap will be 512 MB.

sonar.search.javaOpts=-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError

Lastly, uncomment and change the following configurations to set up the IP address and port of the SonarQube will be running. Also, the log level will be 'INFO" and stored in the 'logs' directory of the SonarQube installation directory.

sonar.web.host=127.0.0.1
sonar.web.port=9000
sonar.web.javaAdditionalOpts=-server

sonar.log.level=INFO
sonar.path.logs=logs

Save the file and exit the editor when you are finished.

After you have finished the SonarQube configuration. Now, you will set up the systemd service file for SonarQube. This allows you easily to control the SonarQube process by using the systemctl command.

Run the following command to create a new systemd service file '/etc/systemd/system/sonarqube.service'.

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

Add the following configuration to the file.

[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=forking
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
User=sonarqube
Group=sonarqube
Restart=always
LimitNOFILE=65536
LimitNPROC=4096

[Install]
WantedBy=multi-user.target

Save the file and exit the editor when you are done.

Now, reload the systemd manager by using the following command.

sudo systemctl daemon-reload

After that, start and enable the 'sonarqube.service' via the systemctl command below.

sudo systemctl start sonarqube.service
sudo systemctl enable sonarqube.service

setup sonarqube service

Lastly, verify the 'sonarqube.service' status using the following command and make sure its status is running.

sudo systemctl status sonarqube.service

You will see the output in the following screenshot. The 'sonarqube.service' status is running, and it's also enabled, which means it will automatically run at system boot.

check sonarqube service

Now that you have the SonarQube is running as a systemd service, you will install and set up a reverse proxy for the SonarQube that runs on localhost with the default port TCP '9000'.

Running SonarQube with Reverse Proxy

Your SonarQube installation is now running, you can now install the Nginx web server and set up server blocks that will be used as the reverse proxy for SonarQube.

Run the following apt command to install the Nginx web server to your Ubuntu system. Input Y when prompted to confirm the installation and press ENTER to proceed.

sudo apt install nginx

install nginx

Once nginx is installed, verify the nginx service and make sure the service status is running via the systemctl command as below.

sudo systemctl is-enabled nginx
sudo systemctl status nginx

You will see the output of the nginx service is enabled, which means it will run automatically at system boot. And the current status of the nginx service is running.

check nginx

After you have the Nginx web server is running, you will create a new server block configuration that will be used as a reverse proxy for SonarQube.

Create a new server blocks configuration '/etc/nginx/sites-available/sonarqube.conf' using the following command.

sudo nano /etc/nginx/sites-available/sonarqube.conf

Add the following configuration to your file and be sure to change the domain name.

server {

    listen 80;
    server_name sonar.hwdomain.io;
    access_log /var/log/nginx/sonar.access.log;
    error_log /var/log/nginx/sonar.error.log;
    proxy_buffers 16 64k;
    proxy_buffer_size 128k;

    location / {
        proxy_pass http://127.0.0.1:9000;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;
        proxy_set_header Host $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 http;
    }
}

Save the file and exit the editor when you are finished.

Next, activate the server block configuration 'sonarqube.conf' by creating a symlink of that file to the '/etc/nginx/sites-enabled' directory. Then, verify your Nginx configuration files.

sudo ln -s /etc/nginx/sites-available/sonarqube.conf /etc/nginx/sites-enabled/
sudo nginx -t

If you have proper and correct Nginx configuration files, you should see the output message such as "test is successfull".

Lastly, run the following systemctl command below to restart the nginx service and apply the new server block configuration.

sudo systemctl restart nginx

SonarQube Installatioon

After you have finished the reverse proxy configuration for SonarQube, now you can access your SonarQube installation via your domain and set up some basic configuration of SonarQube.

Open your web browser and visit the domain name for your SonarQube installation (i.e: http://sonar.hwdomain.io).

Now you will get the SonarQube login page. Input the default username and password admin/admin and click Login.

login sonarqube

Once you logged in, you will be asked to set up a new password for SnonarQube. Input the old password admin, then input the new strong password and repeat, then click Update.

change password

Now you will be shown the SonarQube user dashboard in the following screenshot.

sonarqube dashboard

Conclusion

In this guide, you have installed SonarQube on the latest Ubuntu 22.04 server. Also, you have completed the basic installation of the PostgreSQL database via the official PostgreSQL repository and installed the Nginx web server. Afterward, you have also configured the reverse proxy for the SOnarQube with Nginx server blocks.

SonarQube is now installed on your Ubuntu server. You can now add the project that you will be analyzing to the SonarQube via the git repository.

Share this page:

0 Comment(s)