
SonarQube is an open-source platform used to continuously inspect code quality and manage code quality. It detects bugs, vulnerabilities, and tracks code quality through static analysis with detailed reports. SonarQube supports multiple programming languages and improves code quality, maintainability, and security by offering actionable insights with two editions, community and enterprise.
This article explains how to Install SonarQube on Ubuntu 24.04. You will install SonarQube and use it to inspect code quality with example projects on your workstation.
Before you begin, you need to:
SonarQube requires a PostgreSQL database to store data. PostgreSQL is available in the default package repositories on Ubuntu. Follow the steps below to install PostgreSQL and create a new database to use with SonarQube.
Install the PostgreSQL if it's not installed on your Ubuntu 24.04 workstation.
Enable the PostgreSQL database server to automatically start at boot.
Output:
Start the PostgreSQL database server.
Log in to the PostgreSQL database server as the postgres user.
Create a new sonaruser PostgreSQL role with a strong password to use with SonarQube. Replace your_password with your desired password.
Create a new sonarqube database.
Grant the sonaruser role full privileges to the sonarqube database.
Switch to the sonarqube database.
Output:
Grant the sonaruser role full privileges to the public schema.
Exit the PostgreSQL database console.
SonarQube is not available in the default package repositories on Ubuntu 24.04 and requires OpenJDK 17 to run. Follow the steps below to download the latest SonarQube release file and install SonarQube.
Update the server's APT package index.
Install OpenJDK 17.
Install Unzip to extract files from the SonarQube archive.
Verify the installed java version.
Your output should be similar to the one below:
Visit the SonarQube releases page and verify the latest version to download. For example, sonarqube-25.2.0.102705.zip.
Download the latest SonarQube archive.
Extract files from the downloaded archive using Unzip.
Move the extracted files to a systemwide directory such as /opt.
Create a dedicated sonarqube system user without login privileges and a home directory.
Grant the sonarqube user full privileges to the /opt/sonarqube directory.
SonarQube uses code scanners depending on the target programming language to scan and analyze code quality. SonarScanner CLI is the default scanner if no specific scanner is specified on your system. Follow the steps below to install the SonarScanner CLI to analyze code on your workstation.
Visit the SonarScanner CLI page and verify the latest version to download. For example, run the following command to download the SonarScanner CLI version 7.0.1
Extract files from the archive depending on the downloaded version.
Move the extracted directory to /opt/sonarscanner.
Open the sonar-scanner.properties configuration file.
Find the following sonar.host.url directive and change the default https://mycompany.com/sonarqube value to 127.0.0.1.
Save and close the file.
The above Sonar Host directive specifies the SonarQube server URL to use while performing code scans.
Enable execute permissions on the sonar-scanner binary.
Link the sonar-scanner binary to the /usr/local/bin directory to enable it as a system-wide command.
View the installed SonarScanner version.
Your output should be similar to the one below.
SonarQube requires specific configurations for optimal performance, including database connections, Java runtime options, system resource limits, and user permissions. Follow the steps below to configure SonarQube to run on your server.
Open the main sonar.properties Sonarqube configuration file.
Add the following configurations at the end of the file. Replace sonaruser and your_password with actual PostgreSQL database user details.
Save and close the file.
The above custom configuration directives enable SonarQube to access the PostgreSQL database, and listen for connections on the TCP port 9000 from all network addresses 0.0.0.0.
Open the sysctl.config configuration file to modify the system memory limits.
Add the following directives at the end of the file.
Save and close the file.
Within the above configuration:
vm.max_map_count=524288: Increases the number of memory maps Elasticsearch can use, allowing it to handle large datasets.fs.file-max=131072: Increases the maximum number of files Elasticsearch can open, allowing it to run efficiently.SonarQube uses Elasticsearch to store indices in a memory-mapped file system. Adjusting the system limits for virtual memory mapping and file handling ensures better stability and performance for SonarQube.
Create a new /etc/security/limits.d/99-sonarqube.conf file to create a resource limits configuration for SonarQube.
Add the following directives to increase the file descriptor and process limits for SonarQube.
Save and close the file.
Within the configuration:
nofile=131072: Increases the number of open file descriptors, allowing SonarQube to handle large workloads.nproc=8192: Raises the process limit to prevent failures under high concurrency.Allow network connections to the SonarQube port 9000.
Reload UFW to apply the firewall configurations.
View the UFW status and verify that below are the only active firewall rules.
Your output should be similar to the one below:
Follow the steps below to set up a new system service for SonarQube to manage the application processes on your server.
Create a new sonarqube.service file.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a new SonarQube system service to monitor and manage the application processes.
Reload systemd to apply the service changes.
Enable SonarQube to start at boot.
Start the SonarQube service.
View the SonarQube service status and verify that it's running.
Your output should be similar to the one below:
Restart the server to apply the SonarQube installation changes.
SonarQube includes a graphical web-based interface for managing code quality, projects, issues, and security. Follow the steps below to access the SonarQube web interface, update the default administrator password and create a dedicated user for code scanning.
Access the SonarQube port 9000 using your server IP or domain name.
Log in to SonarQube with the following credentials when prompted.
adminadmin
Click Administration within the SonarQube interface, select Security from the list of options, and click the Users dropdown option.
Click Create User to add a new user for code scanning.
Click the options symbol in the Tokens column within the new user's row to generate a token.
Enter the token name, select its expiry period, and click Generate.
Copy the generated token in your output to use it with code scanning.
Follow the steps below to test the default SonarQube scanner using an example project.
Switch to your user's home directory.
Create a new sample sonar-example-test project directory.
Switch to the sonar-example-test directory.
Download the SonarQube example project archive.
Extract files from the downloaded archive.
Switch to the examples directory.
Switch to the example sonar-scanner directory.
Scan the code in the entire directory using SonarScanner. Replace user-sonar_token with the user token you generated earlier.
Your output should be similar to the one below:
Visit the SonarQube dashboard to view the scan results and the entire project report.
Follow the steps below to set up your custom code projects to scan using SonarQube on your workstation.
Navigate to your existing project's root directory. For example, myproject.
Create a new sonar-project.properties configuration.
Add the following configurations to the file to define your project settings. Replace the example values with your actual project values.
Save and close the file.
The above project configuration specifies your project settings and a target directory with the code to scan.
Run the SonarScanner CLI to scan the code in your project and access its report in the SonarQube web dashboard.
You have installed and configured SonarQube on an Ubuntu 24.04 workstation. You can install Nginx as a reverse proxy to securely forward all incoming connection requests to the SonarQube port 9000. In addition, you can use the SonarScanner CLI to perform test scans on multiple projects on your workstation and generate detailed reports in the SonarQube web dashboard. For more information and configuration samples, visit the SonarQube Documentation.
0 Comments
Be the first to comment and share your perspective with the community.