How to Set Up SonarQube Server on Linux Local System
SonarQube is an open-source static code analysis tool that helps developers manage code quality and consistency. It supports over 25 programming languages, including Java, C#, C++, Python, JavaScript, and more. Some advanced features are available under a commercial license.
Why Use SonarQube?
SonarQube performs various static code checks, including:
- Detecting potential bugs and security vulnerabilities
- Identifying over-complexity in the code
- Enforcing coding best practices
- Detecting code duplication
- Integrating with Jenkins for automated testing
With its powerful features, such as code smells detection, execution path analysis, and branch analysis, SonarQube is a must-have for maintaining clean, efficient, and secure code.
Steps to Set Up SonarQube on a Linux Local System
1. Install and Configure PostgreSQL
- Add the PostgreSQL repository:
- Install PostgreSQL:
sudo apt-get -y install postgresql postgresql-contrib - Start and enable PostgreSQL:
sudo systemctl start postgresql
sudo systemctl enable postgresql - Set a password for the
postgres
user and create a new database user:
sudo passwd postgres
su – postgres
createuser sonar - Create the SonarQube database:
psql
CREATE DATABASE sonar OWNER sonar;
ALTER USER sonar WITH ENCRYPTED PASSWORD ‘P@ssword’;
\q
2. Download and Configure SonarQube
- Download the latest SonarQube version:
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.7.0.41497.zip - Install unzip and extract SonarQube:
sudo apt-get -y install unzip
sudo unzip sonarqube-8.7.0.41497.zip -d /opt
sudo mv /opt/sonarqube-8.7.0.41497 /opt/sonarqube - Update permissions:
sudo chown -R administrator:administrator /opt/sonarqube/ - Configure the database in the
sonar.properties
file: sonar.jdbc.username=sonar
sonar.jdbc.password=P@ssword
sonar.jdbc.url=jdbc:postgresql://localhost/sonar
sonar.web.javaAdditionalOpts=-server
3. Configure Systemd Service
- Create a service file for SonarQube:
sudo nano /etc/systemd/system/sonar.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=root
Group=root
Restart=always[Install]
WantedBy=multi-user.target - Start and enable the service:sudo systemctl start sonar
sudo systemctl enable sonar
4. Install and Configure Nginx
- Install Nginx: sudo apt-get install nginx -y
- Create an Nginx configuration file:
sudo nano /etc/nginx/sites-enabled/sonarqube.conf - Add the following configuration:
server {
listen 9000;
server_name sonarqube.local;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;
}
} - Restart Nginx: sudo systemctl restart nginx
Accessing SonarQube
Once the setup is complete, access SonarQube at http://localhost:9000
. Use the default admin credentials to log in and change the password. Now, you can set up projects, scan code, and analyze results for better quality and security.
Also read – Secure data on server without installing Server side language