SonarQube Setup on Ubuntu 22.04

This guide provides detailed steps to install and configure SonarQube on Ubuntu 22.04, including the installation of PostgreSQL.

Install Prerequisite

sudo apt update
sudo apt upgrade -y

# SonarQube requires Java 11 or 17
sudo apt install openjdk-17-jdk -y
java -version

Install PostgreSQL

  • SonarQube uses PostgreSQL as its database.
  • Please follow the instructions below here.

Configure PostgreSQL

sudo -i -u postgres
createuser sonar
createdb sonar -O sonar
psql
ALTER USER sonar WITH ENCRYPTED PASSWORD 'your_password';
\q
exit

Install SonarQube

  1. Download SonarQube:
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-10.5.1.90531.zip
  1. Extract and move SonarQube:
unzip sonarqube-10.5.1.90531.zip
sudo mv sonarqube-10.5.1.90531 /opt/sonarqube
  1. Create a SonarQube user and change ownership:
sudo adduser --system --no-create-home --group --disabled-login sonarqube
sudo chown -R sonarqube:sonarqube /opt/sonarqube
  1. Configure SonarQube:

Edit the SonarQube configuration file:

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

Uncomment and set the following properties:

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

Create a Systemd Service File

  1. Create the 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
  1. Reload the systemd daemon and start SonarQube:
sudo systemctl daemon-reload
sudo systemctl start sonarqube
sudo systemctl enable sonarqube

Configure System Limits

  1. Check and increase file descriptors limit:
ulimit -n
sudo vi /etc/security/limits.conf

Add:

sonarqube - nofile 65536
sonarqube - nproc 4096
  1. Set virtual memory limits:
sudo sysctl -w vm.max_map_count=262144

# To make the change persistent
sudo vi /etc/sysctl.conf

Add:

vm.max_map_count=262144

Apply changes:

```bash sudo sysctl -p