How to Install and Configure SonarQube on Debian 11/10

Install and configure SonarQube on Debian 11/10 and prepare yourself an error-free clean coding environment that can be seamlessly invoked. It will help ease the coding task by detecting bugs and other vulnerabilities in your code.

Developed by SonarSource, SonarQube is a web-based tool that works as a source code quality manager. Written in the Java language, SonarQube is known to enhance coding abilities by assuring a safe environment promisingly. In the following guide, I’ll help you learn how to install and configure SonarQube on Debian 11/10 with no issues whatsoever.

How to Install and Configure SonarQube on Debian 11/10

Having discussed the essential aspects of SonarQube, it is time to learn the best ways to install and configure SonarQube on Debian 11/10. The following guide is structured in the most user-friendly manner to ensure you seamlessly get along irrespective of how technically sound you’re.

Step 1: Updating System Repositories

Before installing SonarQube on Debian 11/10, updating your system repositories is always a good practice. Here is how you can do it:

Launch the Terminal by using the Ctrl+Alt+T key combination

Invoke the following command:

$ sudo apt update
update system repositories

Step 2: Install the Required Tools

Run the following command and install all the necessary tools that will help make the operations around SonarQube super-smooth.

$ sudo apt install net-tools wget unzip vim curl
Install and Configure SonarQube on Debian 11/10

Step 3: Bring the Necessary Edits

Once you’ve got all the tools ready, the next thing to do is edit the file located at /etc/sysctl.conf according to the SonarQube requirements.

$ sudo vim /etc/sysctl.conf 
edit files

Add the following lines:

vm.max_map_count=262144
fs.file-max=65536

Finally, reload the sysctl configurations by invoking the command alongside --system flag.

$ sudo sysctl --system
Getting the debian system ready for SonarQube

Step 4: Install Java 11 on Your System

In case you don’t know, the SonarQube utility is entirely written in Java. For that reason, to install and configure SonarQube on Debian 11/10, it is crucial to have a properly functioning Java. 

$ sudo apt install openjdk-11-jdk

Step 5: Install PostgreSQL database on Debian 11/10

After you’ve got the Java OpenSDK installed on your system, it is time for PostgreSQL. To install PostgreSQL on your Debian 11/10 system, first, download the GPG key for PostgreSQL using the wget command together with the -q flag and the designated link.

$ wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
install postgresql on Debian 11/10

Once done, add the PostgreSQL repository in the following way:

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

Make sure to update your APT package index one more time and install PostgreSQL using the following commands:

$ sudo apt update
getting the system updated
$ sudo apt install postgresql postgresql-contrib

As soon as the installation concludes, start the service by default. Simply check the status as follows:

$ systemctl status postgresql

In case you find that the service isn’t running, start it by invoking the following command:

$ sudo systemctl start postgresql

Now, enable the PostgreSQL to start on system boot:

$ sudo systemctl enable postgresql

Step 6: Creating the PostgreSQL Database for SonarQube Utility

Here, you’ll learn to configure the database for the SonarQube. Before that, assign a password for the desired PostgreSQL user.

$ sudo passwd postgres

After setting the preferred password, switch to the user by running the following command:

$ su - postgres
set user password

You can then create a new user for the SonarQube utility by running the following command:

$ createuser sonar

Use the psql command and switch to the PostgreSQL shell

$ psql

Next, you’ll need to create a password for the user you’ve created above:

ALTER USER sonar WITH ENCRYPTED PASSWORD 'DesiredPassW0Rd';

Build a secured database and assign the required privileges to the created user.

CREATE DATABASE sonarqube OWNER sonar;
GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonar; \q

Step 7: Install and Configure SonarQube on Debian 11/10

Having concluded the database configuration part for SonarQube, you’re now ready to download, install and manage the SonarQube binary file. 

Downloading SonarQube

Getting yourself the latest version of SonarQube is pretty easy. Just make sure you do it from the official SonarQube downloads page. Once you find it, use the wget command:

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

Extracting the SonarQube Archive

Post download, get the SonarQube archive extracted.

$ unzip sonarqube-*.zip

Soon after, move the extracted file to the following directory

/opt/

Run the following command for that:

$ sudo mv sonarqube-*/ /opt/sonarqube

Creating a System User Account for the SonarQube Utility

It is crucial to keep in mind that, SonarQube should not be run as root. With that being said, you’ll need to create a non-admin account for working with SonarQube. The process is simple; just run the following command.

$ sudo useradd -M -d /opt/sonarqube/ -r -s /bin/bash sonarqube

Once created, change the ownership of the directory /opt/sonarqube and assign it to the created user by employing the chown command.

$ sudo chown -R sonarqube: /opt/sonarqube

Configuring the SonarQube

You’re now partially aware of how to install and configure SonarQube on Debian 11/10 as we’ve successfully downloaded and installed the utility. The next task is to configure it. 

Start by setting the database connection details in accordance with the created PostgreSQL database.

$ sudo vim /opt/sonarqube/conf/sonar.properties

Now inside the file, edit the lines as below:

Install and Configure SonarQube on Debian 11/10

Save this file and continue.

Step 8: Create a SonarQube Systemd Service File

During the time I’m articulating this tutorial, there is no way to manage the SonarQube service. For that reason, the need for creating a systemd service is essential. 

Run the following command:

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

In the file, add the following lines:

[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

Starting SonarQube

As soon as the edits are done, close the file and start the SonarQube as below:

$ sudo systemctl daemon-reload
$ sudo systemctl start sonarqube

Enabling the SonarQube Service to Run on Boot

Run the following command:

$ sudo systemctl enable sonarqube

Checking the Status of the SonarQube Service

Invoke the following command:

$ systemctl status sonarqube

Step 9: Accessing the SonarQube Web Interface

The SonarQube Web interface is now ready to be accessed by using the URL http://server-ip:[Port]. Right on the login page, use admin as the username and the password. On getting successfully logged in, alter the default password.

You’ll then get access to the SonarQube page. And that’s how you install and configure SonarQube on Debian 11/10. 

If this guide helped you, please share it.

Leave a Reply
Related Posts