SonarQube Setup Guide

 Setting up SonarQube for scanning your workspace and making it accessible over a network involves several steps. Here's a detailed guide:

Prerequisites

  1. Hardware Requirements: Ensure you have a machine with sufficient resources.

    • Minimum: 2 GB RAM, 1 CPU
    • Recommended: 4 GB RAM, 2 CPUs
  2. Software Requirements:

    • Java (Oracle JRE 11 or OpenJDK 11)
    • Database (PostgreSQL, MySQL, Oracle, or MS SQL Server)
  3. SonarQube Download: Download the latest version of SonarQube from SonarQube Downloads.

Step-by-Step Setup

1. Install Java

Ensure Java is installed and the JAVA_HOME environment variable is set.

# For Ubuntu
sudo apt update
sudo apt install openjdk-11-jdk

# Verify installation
java -version

2. Install and Configure the Database

Install PostgreSQL (or any other supported database).

# For Ubuntu
sudo apt update
sudo apt install postgresql postgresql-contrib

# Switch to the postgres user
sudo -i -u postgres

# Create a database user and a database
createuser sonar
createdb sonarqube -O sonar

# Set a password for the sonar user
psql
ALTER USER sonar WITH ENCRYPTED PASSWORD 'your_password';
\q

# Modify pg_hba.conf to allow password authentication
sudo nano /etc/postgresql/12/main/pg_hba.conf
# Change 'peer' to 'md5' for local connections

# Restart PostgreSQL service
sudo systemctl restart postgresql

3. Download and Configure SonarQube

Extract the SonarQube archive to your desired directory.

# Download and extract SonarQube
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-<version>.zip
unzip sonarqube-<version>.zip
mv sonarqube-<version> /opt/sonarqube

# Edit SonarQube configuration
sudo nano /opt/sonarqube/conf/sonar.properties

# Set database connection details
sonar.jdbc.username=sonar
sonar.jdbc.password=your_password
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube

# Set web server host and port (optional)
sonar.web.host=0.0.0.0
sonar.web.port=9000

4. Start SonarQube

Start SonarQube manually or set it up as a service.


# Start SonarQube manually
/opt/sonarqube/bin/linux-x86-64/sonar.sh start

# Check SonarQube logs
tail -f /opt/sonarqube/logs/sonar.log

To configure SonarQube as a service:

# Create a systemd service file
sudo nano /etc/systemd/system/sonarqube.service

# Add the following content
[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=simple
User=your_user
Group=your_group
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
Restart=always

[Install]
WantedBy=multi-user.target

# Reload systemd, enable and start SonarQube service
sudo systemctl daemon-reload
sudo systemctl enable sonarqube.service
sudo systemctl start sonarqube.service

5. Access SonarQube

Open a web browser and navigate to http://your_server_ip:9000. The default credentials are:

  • Username: admin
  • Password: admin

6. Configure SonarQube for Your Projects

  1. Create a Project:

    • Log in to SonarQube.
    • Go to "Projects" > "Create Project".
  2. Generate a Token:

    • Go to "My Account" > "Security".
    • Generate a token for authentication.
  3. Configure SonarQube Scanner:

  4. Run the Scanner:

    • In your project directory, create a sonar-project.properties file:

      sonar.projectKey=my_project
      sonar.host.url=http://your_server_ip:9000
      sonar.login=your_token

      # Optional settings
      sonar.projectName=My Project
      sonar.projectVersion=1.0
      sonar.sources=.
    • Run the scanner:

      sonar-scanner

7. Make SonarQube Accessible Over the Network

Ensure that your firewall allows traffic on port 9000.

# For Ubuntu
sudo ufw allow 9000/tcp
sudo ufw enable

8. Accessing SonarQube Remotely

Users on the same network can access SonarQube by navigating to http://your_server_ip:9000 in their web browsers.

Summary

  • Install and configure Java and the database.
  • Download and set up SonarQube.
  • Start SonarQube and make it accessible over the network.
  • Configure and run the SonarQube Scanner on your projects.

This setup provides a centralized SonarQube instance accessible to your team, enabling continuous code quality checks.

Comments