
SonarQube is a open-source platform designed to continuously inspect and analyze code quality. Supporting over 30 programming languages, it helps developers identify bugs, code smells, and security vulnerabilities early in the development cycle. With detailed reports and actionable insights, SonarQube promotes clean, maintainable code and enhances overall software quality. Running SonarQube on Ubuntu 22.04 LTS, a stable, secure and long-term support release, provides a reliable and efficient environment for maintaining high standards of code quality in both individual and team-based projects.
SonarQube has two parts: a scanner application on the local machine to scan the code and a server application for keeping records.
In this guide, you'll learn how to install, configure, and use SonarQube on Ubuntu 22.04 LTS to efficiently analyze and improve your codebase. If you're using a different version of Ubuntu, you may also find these articles helpful: How to Install SonarQube on Ubuntu 24.04 and Install SonarQube on Ubuntu 20.04 LTS.
When installing SonarQube on Ubuntu, make sure to configure the firewall to allow access to port 9000. If you're using a reverse proxy, also open ports 80 and 443 for HTTP and HTTPS traffic.
Open them using the Uncomplicated Firewall (UFW).
Check the firewall status.
Install OpenJDK 11.
Import the PostgreSQL repository key.
Add the PostgreSQL repository.
Update the system repository list.
Install PostgreSQL 14.
Check the status of the PostgreSQL service.
Log in to the PostgreSQL shell.
Create the sonaruser role.
Create the sonarqube database.
Grant all privileges on the sonarqube database to the sonaruser role.
Exit the shell.
Return to your default user account.
Copy the URL of the latest version of the community edition from the SonarQube downloads page.
Download SonarQube for Ubuntu 22.04 using the URL copied above.
Unzip the downloaded archive.
Move the files to the /opt/sonarqube directory.
Delete the downloaded archive.
Create a system user along with the group for SonarQube.
Give Sonar user permissions to the /opt/sonarqube directory.
Open the SonarQube configuration file for editing.
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.
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.
Paste the following lines to increase the file descriptors and threads that the sonarqube user can open.
Save the file by pressing Ctrl+X, then Y.
Reboot the system to apply the changes.
Create the systemd service file for Sonar and open it for editing.
Paste the following code in it.
Save the file by pressing Ctrl+X, then Y.
Start the SonarQube service.
Check the status of the service.
Enable the service to start automatically at boot.
Verify if the Sonarqube server is functioning properly.
Look for the following text in the HTML response.
This confirms everything is working fine.
Install dependencies required to install Nginx.
Import Nginx's GPG signing key.
Add a repository for Nginx's stable version.
Update the system repository list.
Install Nginx.
Start the Nginx server.
Issue the following commands to ensure that you have the latest version of snapd required to install Certbot.
Install Certbot.
Create a symlink for Certbot to the /usr/bin directory.
Issue the SSL Certificate.
Generate a Diffie-Hellman group certificate.
Do a dry run of the SSL renewal process to ensure it works.
Open the file nginx.conf for editing.
Find the line include /etc/nginx/conf.d/*.conf; and paste the following code below it.
Save the file by pressing Ctrl+X, then Y.
Create the Sonar configuration file for Nginx and open it for editing.
Paste the following code in it.
Save the file by pressing Ctrl+X, then Y.
Verify Nginx configuration syntax.
Restart the Nginx service.
Visit the URL https://sonarqube.example.com 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.
To improve security, create another user to use for scanning code by clicking the Create User button.
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 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.
Open the Sonar configuration file for editing.
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.
Paste your secret key into it. Save the file by pressing Ctrl+X, then Y.
Restrict the secret file to the sonarqube user.
Restart the SonarQube server.
Visit the Administration >> Configuration >> Encryption section again and fill in your database password. Press the Encrypt button to generate the encrypted password.
Replace the actual password in the sonar.properties file with the encrypted version, and restart the server. Repeat the process with any other property you want to encrypt.
SonarQube provides various scanners depending on the programming language. Install the Command line version of the Sonarscanner.
Download the scanner.
Extract the archive.
Move the directory to /opt/sonarscanner.
Switch to the directory.
Open the sonar-scanner.properties file for editing.
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.
Create a symbolic link to the binary to make it accessible from anywhere.
You can test the scanner by running it on SonarQube example projects.
Create a new directory for project testing and switch to it.
Download the example project.
Extract the project files.
Switch to the example project directory.
Run the scanner on the code. Pass the token you created before.
You get the following output after the scan is complete.
Visit the SonarQube dashboard to view the project report.
Transfer the project to your server.
Switch to your project's root directory.
Create and open the SonarQube configuration file.
Define a project key for your project. The chosen key should be unique for your SonarQube instance.
Enter the project name and version to show up in the SonarQube dashboard.
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.
You have successfully installed and used SonarQube on your Ubuntu 22.04 server. For details, you can check out the following resources.
0 Comments
Be the first to comment and share your perspective with the community.