SonarQube is an open-source platform for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities. This tutorial will guide you through the steps to install and configure SonarQube Community Edition on a Linux system.
A fresh Ubuntu 22.04 dedicated server A user with sudo privileges Java 11 or 17 installed (SonarQube requires a specific version of Java) For this tutorial, we have used cloud server with the configuration of 2vCPU, 4GB RAM, 80GB SSD. It should have at least 2GB of RAM 1 CPU core and 8-30 GB free space.
First, ensure your system is up-to-date:
$ sudo apt update
$ sudo apt upgrade -y
SonarQube requires Java 11 or 17. We will install OpenJDK 17.
$ sudo apt install openjdk-17-jdk -y
Verify the installation:
$ java -version
$ sudo apt install curl ca-certificates
$ sudo install -d /usr/share/postgresql-common/pgdg
$ sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
$ sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
$ sudo apt update
$ sudo apt install postgresql-15 -y
Now, let's configure PostgreSQL
Switch to the PostgreSQL user:
$sudo -i -u postgres
$ createuser sonar
$ createdb sonar -O sonar
$ psql
$ ALTER USER sonar WITH ENCRYPTED PASSWORD 'your_password';
$ \q
$ exit
Try to download the latest version
$ wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-10.5.1.90531.zip
$ sudo apt update
$ sudo apt install unzip
$ unzip sonarqube-10.5.1.90531.zip
$ sudo mv sonarqube-10.5.1.90531 /opt/sonarqube
$ sudo adduser --system --no-create-home --group --disabled-login sonarqube
$ sudo chown -R sonarqube:sonarqube /opt/sonarqube
Now, let's configure SonarQube
Edit the SonarQube configuration file:
**$ sudo vi /opt/sonarqube/conf/sonar.properties
Add the below lines sonar.jdbc.username=sonar sonar.jdbc.password=your_password (Add your password) sonar.jdbc.url=jdbc:postgresql://localhost/sonar
Create a new service file for SonarQube
$ sudo vi /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 Restart=always
LimitNOFILE=65536 LimitNPROC=4096
[Install] WantedBy=multi-user.target
$ sudo systemctl daemon-reload $ sudo systemctl start sonarqube $ sudo systemctl enable sonarqube
Check the current limit:
ulimit -n
It should be at least 65536. To increase it, add the following to /etc/security/limits.conf
$ sudo vi /etc/security/limits.conf
Add the following lines
sonarqube - nofile 65536 sonarqube - nproc 4096
$ sudo sysctl -w vm.max_map_count=262144
To make this change permanent, add it to /etc/sysctl.conf $ sudo vi /etc/sysctl.conf
Add the following line $ vm.max_map_count=262144
Apply the changes $ sudo sysctl -p
We need to add ports in firewall
$ ufw allow 9000/tcp $ ufw allow 80/tcp $ ufw allow 443/tcp $ ufw reload
Install Nginx
$ sudo apt install nginx -y
$ sudo nano /etc/nginx/sites-available/sonarqube.example.com
Note: Replace sonarqube.example.com with your domain name/public IP Add the following content
server { listen 80; server_name 192.168.90.90;
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;
}
}
Note: 192.168.90.90 - It's my public IP
Enable the new configuration
$ sudo ln -s /etc/nginx/sites-available/sonarqube.example.com /etc/nginx/sites-enabled/
Test the Nginx configuration and restart Nginx $ sudo nginx -t $ sudo systemctl restart nginx
Open your web browser and go to https://your_domain_or_ip. You should see the SonarQube login page. The default credentials are
Username: admin Password: admin
Upon first login, you will be prompted to change the default password.