How to install SONARQUBE COMMUNITY BUILD on Ubuntu

Step-by-Step Guide to Installing SonarQube Community Edition on Ubuntu

Learn how to easily install SonarQube Community Edition on Ubuntu with our comprehensive step-by-step guide. Enhance your code quality with SonarQube today!

Here’s a comprehensive guide to install SonarQube Community Edition on Ubuntu:

Prerequisites

  • Ubuntu server with at least 2GB RAM and one vCPU core
  • Root or sudo privileges
  • Java 17 (minimum requirement for SonarQube 10)

Installation Steps

1. Install Java

First, verify if Java is installed:

java -version

Install OpenJDK 17:

sudo apt -y install openjdk-17-jre

2. Configure Firewall

sudo ufw allow http
sudo ufw allow https
sudo ufw status

3. Download and Install SonarQube

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-10.1.0.73491.zip
sudo apt -y install unzip
sudo unzip sonarqube-*.zip -d /opt && sudo mv /opt/sonarqube* /opt/sonarqube

4. Create SonarQube User

sudo adduser --system --no-create-home --group --disabled-login sonarqube
sudo chown sonarqube:sonarqube /opt/sonarqube -R

5. Create Systemd Service

Create service file:

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

Add the following content:

[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=sonarqube
Group=sonarqube
PermissionsStartOnly=true
Restart=always
StandardOutput=syslog
LimitNOFILE=131072
LimitNPROC=8192
TimeoutStartSec=5
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

6. Start SonarQube

sudo systemctl start sonarqube
sudo systemctl enable sonarqube
sudo systemctl status sonarqube

Accessing SonarQube

Once installation is complete, access SonarQube through your web browser:

The web interface runs on port 9000 by default. You may need to wait a few minutes after starting the service for the web interface to become available.

how to point the sonarqube server http://localhost:9000 to Nginx Site

Here’s how to configure Nginx as a reverse proxy for SonarQube running on localhost:9000:

Nginx Configuration Steps

1. Create Nginx Configuration File

Create a new configuration file for your SonarQube site:

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

2. Basic HTTP Configuration

Add this basic configuration for HTTP:

server {
    listen 80;
    server_name your_domain.com;

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

    location / {
        proxy_pass http://localhost:9000;
        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 $scheme;
        proxy_http_version 1.1;
        proxy_request_buffering off;
    }
}

3. HTTPS Configuration (Recommended)

For secure HTTPS configuration:

server {
    listen 443 ssl;
    server_name your_domain.com;

    ssl_certificate /path/to/your/certificate.crt;
    ssl_certificate_key /path/to/your/certificate.key;
    
    access_log /var/log/nginx/sonarqube.access.log;
    error_log /var/log/nginx/sonarqube.error.log;

    location / {
        proxy_pass http://localhost:9000;
        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 $scheme;
        proxy_http_version 1.1;
        proxy_request_buffering off;
    }
}

4. Enable the Configuration

sudo ln -s /etc/nginx/sites-available/sonarqube /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

5. SonarQube Configuration

Modify SonarQube’s configuration to only accept local connections:

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

Add or modify these lines:

sonar.web.host=127.0.0.1
sonar.web.port=9000

6. Restart Services

sudo systemctl restart sonarqube
sudo systemctl restart nginx

After completing these steps, you should be able to access SonarQube through your domain instead of localhost:9000. Make sure your firewall allows traffic on ports 80 and 443.

How to use sonarqube to scan static HTML/ CSS/ JS file

Based on the search results, here’s how to scan static HTML, CSS, and JavaScript files with SonarQube:

Prerequisites

  • Node.js installed (recommended version 18 or later)
  • SonarQube server running
  • SonarScanner installed

Configuration Steps

1. Node.js Setup

Ensure Node.js is available in your PATH, or specify its location using:

sonar.nodejs.executable=/path/to/node

2. Project Properties

Create a sonar-project.properties file in your project root:

sonar.projectKey=your-project-key
sonar.projectName=Your Project Name
sonar.sources=.
sonar.sourceEncoding=UTF-8

# JavaScript specific settings
sonar.javascript.node.maxspace=4096
sonar.javascript.detectBundles=false

# File patterns
sonar.inclusions=**/*.html,**/*.css,**/*.js

3. Remove Conflicting Plugins

If you have any community plugins for CSS analysis installed on your SonarQube instance, remove them as they may conflict with the built-in analyzer.

4. Supported Features

SonarQube can analyze:

  • CSS, SCSS, Less files
  • Style tags inside HTML and PHP files
  • JavaScript/ECMAScript (versions 3, 5, 2015-2020)
  • HTML files with embedded JavaScript and CSS

5. Running the Analysis

Execute the scan using SonarScanner:

sonar-scanner \
  -Dsonar.host.url=http://your-sonarqube-server:9000 \
  -Dsonar.login=your-token

Handling Exclusions

To exclude specific directories or files:

# Exclude specific directories
sonar.exclusions=dist/**/*,node_modules/**/*,vendor/**/*

# JavaScript-specific exclusions
sonar.javascript.exclusions=external/**/*,generated/**/*

By default, SonarQube excludes files from common dependency directories like node_modules, bower_components, dist, vendor, and external.

Troubleshooting

If the analysis is slow or unresponsive:

  1. Increase memory allocation using sonar.javascript.node.maxspace=4096 or 8192 for larger projects
  2. Check logs for any error messages
  3. Ensure Node.js is properly installed and accessible
  4. Verify that no CSS analysis plugins are conflicting with the built-in analyzer

How to use sonarqube community build to scan another site that only have static HTML/ CSS/ JS file

Here’s how to scan an external website with static HTML/CSS/JS files using SonarQube Community Edition:

Setup Scanner

1. Create Project Configuration

Create a sonar-project.properties file in your project directory:

sonar.projectKey=website-analysis
sonar.projectName=Website Static Analysis
sonar.sourceEncoding=UTF-8

# Define source directory
sonar.sources=.

# Include only web files
sonar.inclusions=**/*.html,**/*.css,**/*.js

# Exclude unnecessary files/directories
sonar.exclusions=**/node_modules/**/*,**/dist/**/*,**/vendor/**/*

# JavaScript specific settings
sonar.javascript.node.maxspace=4096
sonar.javascript.detectBundles=false

2. Download Website Content

You’ll need to download the static files locally first:

wget -r -np -k -p http://your-target-website.com/

Analysis Configuration

1. Scanner Properties

Add these specific configurations for web content:

# Set file encoding
sonar.sourceEncoding=UTF-8

# CSS analysis
sonar.css.file.suffixes=.css,.less,.scss

# JavaScript analysis
sonar.javascript.exclusions=external/**/*,generated/**/*

2. Running the Analysis

Execute the scan from the directory containing the downloaded files:

sonar-scanner \
  -Dsonar.host.url=http://your-sonarqube-server:9000 \
  -Dsonar.login=your-authentication-token

Best Practices

  • Focus on Relevant Content: Only scan directories containing actual source code
  • Exclude Generated Content: Use exclusion patterns for minified files and generated code
  • Memory Management: If scanning large sites, increase memory allocation using sonar.javascript.node.maxspace=4096
  • Performance Optimization: For better performance, exclude unnecessary files and directories
  • File Types: Ensure all relevant file extensions are included in the analysis

Remember that scanning external websites should be done in compliance with the website’s terms of service and robot exclusion standards.


How to install sonar-scanner

Based on the search results, here’s a complete guide to install sonar-scanner on Ubuntu:

Installation Steps

1. Download SonarScanner

wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.8.0.2856-linux.zip

2. Extract and Install

# Unzip the downloaded package
sudo unzip sonar-scanner-cli-4.8.0.2856-linux.zip

# Move to opt directory
sudo mv sonar-scanner-cli-4.8.0.2856-linux /opt/sonar-scanner

3. Configure PATH (Choose one method)

Method 1: Create Symbolic Link (Recommended)

sudo ln -s /opt/sonar-scanner/bin/sonar-scanner /usr/local/bin/sonar-scanner

Method 2: Add to PATH Variable

echo 'export PATH="/opt/sonar-scanner/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

4. Configure SonarScanner

Edit the sonar-scanner properties file:

sudo nano /opt/sonar-scanner/conf/sonar-scanner.properties

Add or modify these lines:

#Configure the server URL
sonar.host.url=http://localhost:9000

5. Verify Installation

sonar-scanner -v

If the installation is successful, you should see the sonar-scanner version information. You can now use the sonar-scanner command from any directory to perform code analysis.

Troubleshooting

If you encounter any issues:

  • Verify that the binary directory exists at /opt/sonar-scanner/bin
  • Check file permissions: ls -l /opt/sonar-scanner/bin/sonar-scanner
  • Make the scanner executable if needed: chmod +x /opt/sonar-scanner/bin/sonar-scanner
  • Ensure you have sourced your .bashrc file or opened a new terminal after making PATH changes