Skip to content

SonarQube Installation on Ubuntu

Overview

This guide provides comprehensive instructions for installing and configuring SonarQube on Ubuntu systems. SonarQube is an open-source platform for continuous inspection of code quality, performing automatic reviews with static analysis to detect bugs, code smells, and security vulnerabilities.

Important Disclaimer

This is a technical guide for installing and configuring SonarQube on Ubuntu. The deployment involves billable cloud resources (GCP/AWS). Exercise extreme caution when following these instructions. The author and SKYDEVOPS assume no responsibility for any issues, damages, or costs incurred from following this guide. You are fully responsible for your deployment and its consequences.

Architecture Overview

This deployment includes:

  • Cloud Platform: Google Cloud Platform (GCP) compute instance
  • Database: PostgreSQL for SonarQube data persistence
  • Web Server: Nginx as reverse proxy
  • DNS Management: CloudFlare for DNS configuration
  • Security: VPN pool for firewall configurations on GCP

Prerequisites

Before starting the installation, ensure you have:

  • Ubuntu 16.04 LTS or newer server instance
  • Root or sudo access to the server
  • Minimum 2GB RAM (4GB recommended)
  • Minimum 2 CPU cores
  • At least 10GB free disk space
  • Active internet connection
  • Valid domain name for DNS configuration

Installation Steps

Step 1: System Update

First, update the package cache and upgrade existing packages to ensure your system has the latest security patches and updates.

sudo apt-get update
sudo apt-get -y upgrade

Best Practice

Always perform system updates before installing new software to avoid dependency conflicts and security vulnerabilities.

Step 2: Install Java Development Kit

SonarQube requires Java to run. Install Oracle Java 8 JDK using the following commands:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt install oracle-java8-installer

Java Version

SonarQube 7.6 requires Java 8 or Java 11. For newer SonarQube versions, check the official compatibility matrix for supported Java versions.

Verify Java Installation:

java -version

Step 3: Install PostgreSQL Database

SonarQube requires a database backend. This guide uses PostgreSQL, which is the recommended database for production environments.

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
sudo apt-get -y install postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresql

Verify PostgreSQL Status:

sudo systemctl status postgresql

Database Started

PostgreSQL should now be running and enabled to start automatically on system boot.

Step 4: Configure PostgreSQL User

Set a secure password for the default PostgreSQL system user:

sudo passwd postgres

Security

Choose a strong password and store it securely. This password is for the system user, not the database user.

Step 5: Create SonarQube Database User

Create a dedicated database user for SonarQube with appropriate permissions:

su - postgres
createuser sonar

Switch to the PostgreSQL prompt and configure the user:

psql

Execute the following SQL commands:

ALTER USER sonar WITH ENCRYPTED password 'StrongPassword';
CREATE DATABASE sonar OWNER sonar;
GRANT ALL PRIVILEGES ON DATABASE sonar TO sonar;
\q

Critical: Change Default Password

Replace StrongPassword with a strong, unique password. Never use default or weak passwords in production environments.

Exit the postgres user session:

exit

Step 6: Download and Install SonarQube

Download SonarQube and extract it to the appropriate directory:

cd /tmp
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-7.6.zip
sudo apt install unzip
sudo unzip sonarqube-7.6.zip -d /opt
sudo mv /opt/sonarqube-7.6 /opt/sonarqube

Version Information

This guide uses SonarQube 7.6. For the latest version, visit the SonarQube downloads page and update the download URL accordingly.

Configure SonarQube Database Connection:

Edit the SonarQube configuration file:

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

Add or uncomment the following lines:

sonar.jdbc.username=sonar
sonar.jdbc.password=StrongPassword
sonar.jdbc.url=jdbc:postgresql://localhost/sonar

Configuration Location

The sonar.properties file contains all SonarQube configuration options. Review this file to customize additional settings such as web server port, log locations, and compute engine settings.

Step 7: Create SonarQube System Service

Create a systemd service file to manage SonarQube as a system service:

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

Add the following configuration:

[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

Security Consideration

Running SonarQube as root is not recommended for production. Create a dedicated sonar user with limited privileges for better security. Modify the User and Group directives accordingly.

Reload systemd daemon:

sudo systemctl daemon-reload

Step 8: Start SonarQube Service

Enable and start the SonarQube service:

sudo systemctl start sonar
sudo systemctl enable sonar
sudo systemctl status sonar

Service Status

The service may take 1-2 minutes to fully start. Check the logs at /opt/sonarqube/logs/ if you encounter issues.

Monitor SonarQube Startup:

tail -f /opt/sonarqube/logs/sonar.log

Step 9: Configure Nginx Reverse Proxy

Install and configure Nginx as a reverse proxy to access SonarQube via a domain name:

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

Add the following Nginx configuration:

server {
    listen 80;
    server_name sonarqube.domain.com;

    location / {
        proxy_pass http://127.0.0.1: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;
    }
}

SSL/TLS Configuration

For production environments, configure SSL/TLS certificates using Let's Encrypt or your certificate provider. Never expose SonarQube over HTTP in production.

Enable the site and restart Nginx:

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

Step 10: Access SonarQube Web Interface

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

http://sonarqube.domain.com

Default Credentials:

  • Username: admin
  • Password: admin

Change Default Credentials Immediately

The default credentials are publicly known. Change the admin password immediately after first login. Navigate to Administration → Security → Users to update the password.

Post-Installation Configuration

Initial Setup Tasks

  1. Change Admin Password: Navigate to Administration → Security → Users
  2. Configure Email: Set up SMTP settings for email notifications
  3. Install Language Plugins: Install analyzers for your programming languages
  4. Create Projects: Set up your first project for code analysis
  5. Configure Quality Gates: Define quality standards for your projects
  6. Set Up Authentication: Configure LDAP, SAML, or OAuth if needed

Firewall Configuration

If you're using UFW firewall, allow necessary ports:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 9000/tcp  # Only if direct access needed
sudo ufw reload

System Tuning

For optimal performance, adjust system limits:

sudo nano /etc/sysctl.conf

Add the following lines:

vm.max_map_count=262144
fs.file-max=65536

Apply changes:

sudo sysctl -p

Troubleshooting

Common Issues

SonarQube won't start

Check the logs at /opt/sonarqube/logs/ for error messages. Common issues include:

  • Insufficient memory (increase heap size in sonar.properties)
  • Port 9000 already in use
  • Database connection failures
  • Java version incompatibility
Database connection errors

Verify PostgreSQL is running and credentials are correct:

sudo systemctl status postgresql
psql -U sonar -d sonar -h localhost
Web interface not accessible
  • Check if SonarQube service is running: sudo systemctl status sonar
  • Verify Nginx configuration: sudo nginx -t
  • Check firewall rules: sudo ufw status
  • Review Nginx logs: sudo tail -f /var/log/nginx/error.log

Maintenance

Regular Maintenance Tasks

Update SonarQube:

# Backup before update
sudo systemctl stop sonar
sudo cp -r /opt/sonarqube /opt/sonarqube.backup
# Download and extract new version
# Copy configuration and data

Database Backup:

sudo -u postgres pg_dump sonar > sonar_backup_$(date +%Y%m%d).sql

Log Rotation:

Configure log rotation to prevent disk space issues:

sudo nano /etc/logrotate.d/sonarqube

Additional Resources

Conclusion

You now have a working SonarQube installation on Ubuntu. Remember to regularly update your installation, monitor system resources, and follow security best practices to maintain a healthy code quality platform.


Document Information:

  • Last Updated: October 2025
  • SonarQube Version: 7.6 (Update as needed)
  • Ubuntu Version: 16.04 LTS (Compatible with 18.04, 20.04, 22.04)
  • Original Creation Date: October 31, 2017