Install SonarQube on Ubuntu

Install SonarQube on Ubuntu

SonarQube is a powerful open-source tool designed to enhance your code quality analysis and reporting. It identifies potential bugs, vulnerabilities, and maintainability issues in your source code, helping you maintain a high standard of code quality. In this guide, I'll walk you through the steps to install and configure SonarQube on Ubuntu.

Step 1 - Update Your System

Before we begin, it's crucial to ensure your Ubuntu is up to date. Run the following commands:

sudo apt update &&
sudo apt upgrade -y

Step 2 - Install and Configure PostgreSQL

We'll need a PostgreSQL database for SonarQube. Follow these steps to set it up.

Step 2.1 - Create the file repository configuration

Add the PostgreSQL repository configuration to the system:

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

Step 2.2 - Import the repository signing key

Now we're importing the repository's GPG signing key for package verification:

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

Step 2.3 - Update the package lists

This command refreshes the package lists to include the PostgreSQL repository:

sudo apt update

Step 2.4 - Install the latest version of PostgreSQL

In this step, we install PostgreSQL and its additional components.

sudo apt install postgresql postgresql-contrib -y

Step 2.5 - Start and enable the PostgreSQL server

We need to start the PostgreSQL server and ensure that it automatically starts at boot.

sudo systemctl start postgresql &&
sudo systemctl enable postgresql

Step 2.6 - Change the password for the default PostgreSQL user

This command allows you to change the password for the default PostgreSQL user, 'postgres.'

sudo passwd postgres

Step 2.7 - Switch to the Postgres user

Finally, we switch to the Postgres user to perform database-related tasks.

su - postgres

Step 3 - Create a User and Database for SonarQube in PostgreSQL:

To set up the PostgreSQL user and database for SonarQube, follow these steps.

Step 3.1 - Access the PostgreSQL Command-Line Interface (CLI)

On your terminal, log in to the PostgreSQL command-line interface using the following command:

psql

You should now be in the PostgreSQL interactive shell.

Step 3.2 - Create the "sonar" User:

CREATE ROLE sonar;

Step 3.3 - Set a Password for the "sonar" User:

To secure the "sonar" user, set a password for it using the following SQL command:

ALTER USER sonar WITH ENCRYPTED PASSWORD 'P@ssword';

Step 3.4 - Create a New Database for SonarQube:

Create a new database specifically for SonarQube with the "sonar" user as the owner:

CREATE DATABASE sonar OWNER sonar;

Step 3.5 - Exit from the PostgreSQL Shell:

To exit the PostgreSQL shell, type:

\q

Step 3.6 - Switch Back to the sudo User:

To switch back to your user, use the following command:

exit

Step 4 - Prepare the System and Install SonarQube

Before installing SonarQube, we need to ensure that the system is up to date and has the necessary dependencies. Follow these steps:

Step 4.1 - Update and upgrade the system

Ensure that your system is up to date with the latest packages and upgrades:

sudo apt update
sudo apt upgrade -y

Step 4.2 - Install Java 17

SonarQube requires Java to run. Install OpenJDK 17, which is a compatible Java version:

sudo apt install openjdk-17-jre -y

Step 4.3 - Install unzip

The unzip utility is needed to extract the SonarQube installation files:

sudo apt install unzip -y

Step 4.4 - Download SonarQube

Download the SonarQube installation package:

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

Step 4.5 - Unzip SonarQube

sudo unzip sonarqube-10.2.1.78527.zip

Step 4.6 - Remove the downloaded zip file

After extracting the contents, we remove the zip file to save space:

sudo rm sonarqube-10.2.1.78527.zip

Step 4.7 - Move SonarQube to the /opt directory and set permissions

We need to move the SonarQube installation directory to /opt and set the appropriate ownership.

sudo mv ./sonarqube-10.2.1.78527 /opt/sonarqube/
sudo chown -R sonarqube:sonarqube /opt/sonarqube/

Step 4.8 - Configure SonarQube

Let's configure SonarQube by editing the SonarQube configuration file. Open sonar.properties with your favorite text editor (nano FTW):

sudo nano /opt/sonarqube/conf/sonar.properties

Find the following lines:

#sonar.jdbc.username=
#sonar.jdbc.password=

Uncomment these lines and provide the PostgreSQL username and password of the database that we created earlier. It should look like:

sonar.jdbc.username=sonar
sonar.jdbc.password=P@ssword

Next, find:

#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

Uncomment the line, save the file, and exit from the editor. (This line originally is going to have more data, change it to exactly as I showed here.)

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

Step 4.9 - Configure SonarQube Server Mode

To configure SonarQube to listen on all network interfaces, search for these lines and edit them to look exactly like that:

sonar.web.javaAdditionalOpts=-server
sonar.web.host=0.0.0.0

Step 4.10 - Add or update the vm.max_map_count parameter in /etc/sysctl.conf

echo "vm.max_map_count=262144" | sudo tee -a /etc/sysctl.conf

This step configures a kernel parameter required for Elasticsearch, which is used by SonarQube.

Step 4.11 - Apply the changes

sudo sysctl -p

Apply the kernel parameter changes.

Step 5: Set Up SonarQube as a System Service

To ensure that SonarQube starts automatically and behaves as a system service, we'll create a Systemd unit file. Follow these steps:

Step 5.1 - Create the SonarQube Systemd Unit File

Open a text editor for creating the unit file:

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

Step 5.2 - Add the Following Configuration to the Unit File

This configuration sets up SonarQube as a service, defines its behavior, and specifies the startup and shutdown commands.

[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=root
Group=root
Restart=always

[Install]
WantedBy=multi-user.target

Step 5.3 - Start and Enable SonarQube Service

To start the SonarQube service, use the following command:

sudo systemctl start sonarqube

To ensure that SonarQube automatically starts at boot time, enable the service:

sudo systemctl enable sonarqube

Step 5.4 - Verify Service Status

To check if the SonarQube service is running and active, execute:

sudo systemctl status sonarqube

These steps set up SonarQube as a system service, making it convenient for automatic startup and management.

Step 6 - Configure Nginx for SonarQube (Optional)

Now that we have SonarQube up and running, let's set up Nginx to act as a reverse proxy and handle incoming traffic. Follow these steps.

Step 6.1 - Create a New Nginx Configuration File

Begin by creating a new Nginx configuration file for the SonarQube site:

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

Step 6.2 - Add the Following Nginx Configuration:

server {
    listen      80; # You can adjust the port if needed
    server_name YOUR-DOMAIN-HERE; # Replace with your domain name

    access_log  /var/log/nginx/sonarqube.access.log;
    error_log   /var/log/nginx/sonarqube.error.log;

    proxy_buffers 16 64k;
    proxy_buffer_size 128k;

    location / {
        proxy_pass  http://127.0.0.1:9000; # The SonarQube server address and port
        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;
    }
}

Ensure that you replace YOUR-DOMAIN-HERE with your actual domain name or server IP address. You can also adjust the port as needed.

Step 6.3 - Verify Nginx Configuration:

Before proceeding, ensure that your Nginx configuration file has no syntax errors:

sudo nginx -t

If you encounter any errors, correct them and run the command again.

Step 6.4 - Restart Nginx:

Once the configuration is error-free, restart Nginx to apply the changes:

sudo service nginx restart

Nginx will be configured as a reverse proxy to route incoming traffic to your SonarQube instance, making it accessible via your specified domain or IP address.

Step 7 - Accessing SonarQube

With everything set up, it's time to access your SonarQube installation. Follow these steps.

Step 7.1 - If You've Installed SonarQube on a Server

Navigate to your server's domain name or public IP address using your web browser:

http://server_domain_name_or_IP

Replace server_domain_name_or_IP with the actual domain name or IP address of your server.

Step 7.2 - If You've Installed SonarQube Locally:

If you've set up SonarQube on your local system, you can access it using the following URL:

http://127.0.0.1:9000

This URL will take you to the SonarQube web interface.

Congratulations! You've successfully set up SonarQube for code quality analysis and reporting.

Step 8 - Conclusion

In this tutorial, we've walked through the process of installing and configuring SonarQube on your system. SonarQube is a powerful tool for scanning and analyzing your source code, helping you identify potential bugs, vulnerabilities, and maintainability issues.

To further explore SonarQube's capabilities and features, you can refer to the SonarQube Official Documentation.

With SonarQube in place, you can now enhance the quality of your code and ensure the reliability and security of your software projects. Happy coding!