
SonarQube is an open-source and self-hosted static code analysis platform for analyzing code to identify bugs, code smells and security vulnerabilities. SonarQube supports multiple programming languages and provides automated code reviews to maintain high code quality standards. It's based on Java, allowing you to deploy it on different operating systems, including containerized platforms like Docker or Kubernetes.
This article explains how to install and use SonarQube on Rocky Linux 9. You will install all required dependencies and configure SonarQube to analyze code in your projects.
Before you begin, you need to:
4 GB RAM.sonarqube.example.com.SonarQube server requires the Java Runtime Environment (JRE) and Java Development Kit (JDK) to run on a server. The SonarQube long-term active (LTA) version 2025. It requires Java 17 or 21 to run. Follow the steps below to install OpenJDK 17 required by SonarQube on Rocky Linux 9.
Update the DNF package index.
Install the OpenJDK 17 package.
Verify the installed Java version.
Output:
If the installed Java version is below 17, run the following command to update it.
SonarQube uses Elasticsearch for search and indexing, which requires a non-root system user. Follow the steps below to create a dedicated sonarqube user for SonarQube.
Create a new sonarqube system user.
The above command creates a dedicated sonarqube user you will use to run and configure SonarQube.
SonarQube supports multiple database engines including PostgreSQL which offers advanced SQL functions. Follow the steps below to install PostgreSQL if it's not installed and create a new database to use with SonarQube.
Install the PostgreSQL database server packages.
Initialize the PostgreSQL database server.
Enable the PostgreSQL database server to start at boot.
Start the PostgreSQL database server.
Log in to your PostgreSQL database server with the default postgres user.
Change the default postgres user password and enter a new strong password when prompted.
Create a sonardb database for SonarQube.
Create a new sonaruser PostgreSQL user. Replace <YOUR_PASSWORD> with a strong password.
Grant the sonaruser full privileges to the sonardb database.
Quit the PostgreSQL console.
Open the /var/lib/pgsql/data/pg_hba.conf file using a text editor such as nano to modify the PostgreSQL configuration.
Change peer to trust and ident to md5 to enable password-based authentication.
Save and close the file.
Restart the PostgreSQL service to apply the configuration changes.
SonarQube is not available as a standalone package in the default DNF package repository sources. Follow the steps below to download and install the latest SonarQube binary on Rocky Linux 9.
Install Unzip to extract files from the SonarQube archive.
Create a dedicated sonarqube directory in a system-wide location such as /opt.
Switch to the /tmp directory to store temporary files.
Visit the SonarQube downloads page, verify the latest LTA version, and download it using Wget. For example, run the following command to download the 25.1.0.102122 SonarQube archive.
Extract files from the downloaded archive using Unzip.
Move all the extracted files to the /opt/sonarqube directory.
Follow the steps below to install the SonarScanner CLI on Rocky Linux 9.
Create the sonarscanner directory in /opt.
Check your working directory and verify that it's /tmp.
Visit the SonarScanner CLI releases page and verify the latest version to download using Wget. For example, run the following command to download the 7.0.2 SonarScanner archive.
Extract files from the downloaded archive using Unzip.
Move the extracted files to the /opt/sonarscanner directory.
Open the sonar-scanner.properties configuration file.
Find the sonar.host.url property and set its value to http://127.0.0.1.
Save and close the configuration file.
Enable execute permissions for the sonar-scanner binary.
Link the sonar-scanner binary to the /usr/local/bin directory to enable it as a system-wide command.
Verify installed SonarScanner version.
Your output should be similar to the one below.
Follow the steps below to configure SonarQube to run on your Rocky Linux 9 server.
Open the /opt/sonarqube/conf/sonar.properties to modify the SonarQube configuration properties.
Add the following configurations at the end of the sonar.properties file. Replace sonaruser, sonardb, and <YOUR_SONAR_USER_PASSWORD> with the actual PostgreSQL database details you created earlier.
Save and close the file.
The above SonarQube properties configure PostgreSQL, the default address SonarQube should listen for incoming requests 0.0.0.0, java options, and the default storage path.
Grant the sonarqube user ownership privileges to the /opt/sonarqube directory.
Change the permissions mode to 775 to grant the /opt/sonarqube directory owner full privileges to all SonarQube files.
Open the /etc/sysctl.conf configuration file to update the memory map limit to the required SonarQube count for memory-mapped files.
Add the following memory configurations to the /etc/sysctl.conf file.
Save and close the file.
The above configuration updates the memory map limit to the required SonarQube count for memory-mapped files.
Reload the kernel parameters to apply the configuration changes.
The SonarQube directory includes a /bin/linux-x86-64/sonar.sh startup script you can use to start or stop the SonarQube server. Follow the steps below to create a system service for SonarQube to start, stop, or restart the script.
Change the /opt/sonarqube SELinux content to usr_t to enable the sonaruser to execute scripts in the directory.
Restore the /opt/sonarqube SELinux context to apply the changes.
Create a new sonarqube.service service file.
Add the following contents to the sonarqube.service file.
Save and close the file.
The above service configuration uses the /opt/sonarqube/bin/linux-x86-64/sonar.sh script to start and stop SonarQube with the sonarqube user, allowing you to manage the application's runtime processes.
Reload Systemd to apply the service configuration changes.
Enable the SonarQube service to start at boot.
Start the SonarQube service.
View the SonarQube service status and verify that it's running.
Output:
The SonarQube web management interface is available and active on the localhost port 9000 after installation. Follow the steps below to configure Apache as a reverse proxy to securely forward all incoming connections to the SonarQube port 9000 using HTTP or HTTPS.
Install the Apache webserver if its not installed.
Enable Apache to automatically at boot.
Start the Apache webserver.
Verify that the Apache mod_proxy, mod_proxy_http, and mod_proxy_balancer modules are enabled.
Your output should be similar to the one below.
Follow the steps below to enable the modules if they are not active.
Open the httpd.conf Apache configuration file to enable any missing modules.
Uncomment the appropiate directives to load the necessary modules:
Save and close the file.
Create a new sonarqube.conf virtual host configuration file in the /etc/httpd/conf.d/ directory to use with SonarQube.
Add the following contents to the sonarqube.conf file. Replace sonarqube.example.com with your actual domain.
Save and close the file.
Test the Apache configuration for errors.
Output:
Restart the Apache webserver to apply the configuration changes.
Allow Apache to make outgoing network connections.
Allow connections to the HTTP port 80 through the default firewall configuration.
Reload Firewalld to apply the configuration changes.
Apache accepts HTTP connections for forwarding to the SonarQube localhost port 9000. HTTP uses plain text by default without any encryption. Follow the steps below to generate trusted SSL certificates to secure SonarQube with encrypted HTTPS connections between a client's browser and the webserver.
Install the Certbot Let's Encrypt client for Apache to generate SSL certificates.
Generate a new SSL certificate using your virtual host domain. Replace sonarqube.example.com with your actual domain and admin@sonarqube.example.com with your active email address.
Allow HTTPS connections through the firewall.
Reload Firewalld to apply the configuration changes.
SELinux is active on Rocky Linux 9 by default and prevents other system applications to serve content from the /opt/sonarqube directory. Follow the steps below to configure SELinux and enable Apache to serve content from SonarQube directory.
Check the SELinux status and verify that the current mode is enforcing.
Output:
Check the /opt/sonarqube SELinux context and verify that its unconfined_u:object_r:usr_t to enable the sonarqube user to access the directory files.
Output:
Verify that the SELinux boolean httpd_can_network_connect is enabled for Apache.
Output:
Restart the Apache service.
Restart the SonarQube service.
Follow the steps below to access SonarQube using its web management interface on your Rocky Linux 9 server.
Access your sonarqube.example.com domain in a web browser such as Chrome to open the SonarQube dashboard.
Log in to SonarQube using the default admin username, and admin password credentials.
Change the default password for the admin account when prompted.
Click Administration within the SonarQube interface, select Security from the list of options, and click Users.
Click Create User to set up a new user for code scanning.
Click the options in the Tokens column within the new user's row.
Enter a new token name, set its expiry period, and click Generate.
Copy the generated token to use in your code analysis tasks.
Follow the steps below to clone and scan example projects from the official SonarQube repository to test the installation on your Rocky Linux 9 server.
Navigate to your user's home directory.
Create a new projects directory.
Navigate to the projects directory.
Clone the sonar-scanning-examples repository using Git.
Navigate to the downloaded sonar-scanner project directory.
Scan the code in the directory using SonarScanner. Replace <YOUR_SONAR_TOKEN> with the actual user token you generated earlier.
Your output should be similar to the one below when successful.
Access the /projects path in your web browser.
Click the Example of SonarScanner Usage project.
Explore the scanned project report to verify the code's security, maintainability, code coverage, and duplication issues.
You have installed and configured SonarQube on Rocky Linux 9 to perform code analysis in projects. You scanned the example SonarQube project files using the SonarScanner and access the web management interface using your domain. For more information and configuration samples, visit the SonarQube Documentation.
0 Comments
Be the first to comment and share your perspective with the community.