Install SonarQube on Ubuntu 22.04 (PostgreSQL + Java 17)

Based on this guide by dmancloud. Requirements: Ubuntu 22.04 or newer, at least 2 vCPU and 4 GB RAM.

Note: this uses SonarQube 9.9 LTS. Check the downloads page for the current LTA version and the Java version it needs.

Install PostgreSQL 15

sudo apt update && sudo apt upgrade -y
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 -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo tee /etc/apt/trusted.gpg.d/pgdg.asc &>/dev/null
sudo apt update
sudo apt-get -y install postgresql postgresql-contrib
sudo systemctl enable postgresql

Create the database

sudo -u postgres createuser sonar
sudo -u postgres psql
ALTER USER sonar WITH ENCRYPTED PASSWORD 'change_me';
CREATE DATABASE sonarqube OWNER sonar;
GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonar;
\q

Install Java 17 (Temurin)

sudo apt install -y wget apt-transport-https
sudo mkdir -p /etc/apt/keyrings
wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee /etc/apt/keyrings/adoptium.asc
echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | sudo tee /etc/apt/sources.list.d/adoptium.list
sudo apt update
sudo apt install -y temurin-17-jdk
java --version

Raise system limits

Add to the end of /etc/security/limits.conf:

sonarqube   -   nofile   65536
sonarqube   -   nproc    4096

Add to the end of /etc/sysctl.conf and reboot:

vm.max_map_count = 262144

Install SonarQube

sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.9.0.65466.zip
sudo apt install -y unzip
sudo unzip sonarqube-9.9.0.65466.zip -d /opt
sudo mv /opt/sonarqube-9.9.0.65466 /opt/sonarqube
sudo groupadd sonar
sudo useradd -c "user to run SonarQube" -d /opt/sonarqube -g sonar sonar
sudo chown -R sonar:sonar /opt/sonarqube

Set the database connection in /opt/sonarqube/conf/sonar.properties:

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

systemd service

Create /etc/systemd/system/sonar.service:

[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=sonar
Group=sonar
Restart=always
LimitNOFILE=65536
LimitNPROC=4096

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now sonar
sudo systemctl status sonar
sudo tail -f /opt/sonarqube/logs/sonar.log

Open http://<IP>:9000. The default login is admin / admin and it asks you to change it.

Written by Daniel Ruiz Peláez, Systems & Infrastructure Engineer (Linux, VMware, Proxmox, Active Directory, networking and security). These are notes from real problems I have solved.

← Back to all posts