Skip to content

Instantly share code, notes, and snippets.

@abeldantas
Last active March 27, 2024 15:23
Show Gist options
  • Save abeldantas/586b8feeef9a26f36e34e9a605c3ae37 to your computer and use it in GitHub Desktop.
Save abeldantas/586b8feeef9a26f36e34e9a605c3ae37 to your computer and use it in GitHub Desktop.
How to Setup Sonarqube

How to Install SonarQube Community Edition on Linux

1. Install Java & PostgreSQL

sudo apt update
sudo apt install openjdk-17-jre postgresql postgresql-contrib

2. Download SonarQube Community Edition

Download from SonarQube Downloads SonarQube 9.9 LTS:

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

3. Unzip the SonarQube Package

unzip sonarqube-9.9.3.79811.zip

4. Move to a Suitable Directory

sudo mv sonarqube-9.9.3.79811 /opt/sonarqube

5. Download Sonarqube Multi Branch Plugin

Check other versions at Multi Branch Plugin

wget https://github.com/mc1arke/sonarqube-community-branch-plugin/releases/download/1.14.0/sonarqube-community-branch-plugin-1.14.0.jar

6. Move to a Suitable Directory

sudo mv sonarqube-community-branch-plugin-1.14.0.jar /opt/sonarqube/extensions/plugins/

7. Create a SonarQube User

sudo adduser sonarqube
sudo chown -R sonarqube:sonarqube /opt/sonarqube
sudo chmod -R 775 /opt/sonarqube

8. Create PostgreSQL Database and User

Login to PostgreSQL:

sudo -u postgres psql

Create a new database user (e.g., sonar):

CREATE USER sonar WITH ENCRYPTED PASSWORD 'your_password';
CREATE DATABASE sonarqube OWNER sonar;
GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonar;
\q

9. Configure sonar.properties

Navigate to the SonarQube configuration directory:

cd /opt/sonarqube/conf/

Edit sonar.properties:

sudo nano sonar.properties

Uncomment and update the PostgreSQL configuration lines:

sonar.jdbc.username=sonar
sonar.jdbc.password=your_password
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube

For multi-branch plugin:

sonar.web.javaAdditionalOpts=-javaagent:./extensions/plugins/sonarqube-community-branch-plugin-1.14.0.jar=web

sonar.ce.javaAdditionalOpts=-javaagent:./extensions/plugins/sonarqube-community-branch-plugin-1.14.0.jar=ce

wget xarp.pt/sonarqube/

10. Configure Systemd

Create and edit sonarqube.service:

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

Add the following configuration:

[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=forking
User=sonarqube
Group=sonarqube
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
Restart=on-failure
LimitNOFILE=65536
LimitNPROC=4096
TimeoutStartSec=5min

[Install]
WantedBy=multi-user.target

11. Start and Enable the SonarQube Service

sudo systemctl start sonarqube
sudo systemctl enable sonarqube

12. Adjust System Limits

Edit /etc/sysctl.conf and /etc/security/limits.conf to set system limits:

sudo nano /etc/sysctl.conf

Add:

vm.max_map_count=262144

Then:

sudo nano /etc/security/limits.conf

Add:

sonarqube   -   nofile   65536
sonarqube   -   nproc    4096

Apply changes:

sudo sysctl -p

13. Restart the System

sudo reboot

14. Access SonarQube

Open a browser and navigate to:

http://<your_server_ip>:9000

For SSH tunneling, use:

ssh -L 9000:127.0.0.1:9000 <user>@<your_server_ip>

Then access via:

http://127.0.0.1:9000

15. Log into SonarQube

When you first access SonarQube, use the default administrator credentials:

Username: admin
Password: admin

16. Debugging

Check if it's working on the vm

url http://127.0.0.1:9000

Status:

sudo systemctl status sonarqube

The process above relies on a system service. One option to debug if the service keeps failing on start is to run the process manually:

sudo -u sonarqube /opt/sonarqube/bin/linux-x86-64/sonar.sh console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment