How To Install SonarQube on Ubuntu 22.04 LTS
In this tutorial, we will show you how to install SonarQube on Ubuntu 22.04 LTS. For those of you who didn’t know, SonarQube or formerly Sonar is an open-source platform for static code analysis and code security. It can find security vulnerabilities in more than 20 programming languages along with auto-analyzing code quality to detect code bugs and smells. SonarQube also provides reports such as duplicate code, coding standards, code complexity, and security recommendation.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of SonarQube on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.
Prerequisites
- A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install SonarQube on Ubuntu 22.04 LTS Jammy Jellyfish
Step 1. First, make sure that all your system packages are up-to-date by running the following apt
commands in the terminal.
sudo apt update sudo apt upgrade sudo apt install wget apt-transport-https gnupg2 software-properties-common
Step 2. Installing Java OpenJDK.
Now run the following command below to install Java 11 to your Ubuntu system:
sudo apt install openjdk-11-jdk
Once the installation is complete, you can verify it by checking the Java version:
java -version
Step 3. Installing PostgreSQL Database.
By default, PostgreSQL is available on Ubuntu 22.04 base repository. Now run the following command below to install the latest version of PostgreSQL to your system:
sudo apt install postgresql postgresql-contrib
Once successfully installed, enable PostgreSQL (to start automatically upon system boot), start, and verify the status using the commands below:
sudo systemctl enable postgresql sudo systemctl start postgresql sudo systemctl status postgresql
Next, create a new database and user for the SonarQube via the PostgreSQL shell:
sudo -u postgres psql
Run the following PostgreSQL queries to create a new database and user for SnonarQube:
CREATE USER sonarqube WITH PASSWORD 'your-strong-passwd'; CREATE DATABASE sonarqube OWNER sonarqube; GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonarqube;
After that, we create a new user for SonarQube, and set up custom kernel parameters:
sudo useradd -b /opt/sonarqube -s /bin/bash sonarqube
Next, open the file /etc/sysctl.conf
using your favorite text editor:
sudo nano /etc/sysctl.conf
Add the following configuration:
vm.max_map_count=524288 fs.file-max=131072
Save and close the file, then run the sysctl
the command below to apply new changes:
sudo sysctl --system
Step 4. Installing SonarQube on Ubuntu 22.04.
By default, the SonarQube is not available on Ubuntu 22.04 base repository. Now run the following command below to download the latest version of SonarQube from the official page to your system:
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.6.1.59531.zip
Next, extract the downloaded file:
unzip sonarqube-9.6.1.59531.zip mv sonarqube-9.6.1.59531 /opt/sonarqube
We will need to change some folders permissions:
sudo chown -R sonarqube:sonarqube /opt/sonarqube
Step 5. Configure SonarQube.
First, open the SonarQube configuration file ‘/opt/sonarqube/conf/sonar.properties
‘Use your favorite text editor:
nano /opt/sonarqube/conf/sonar.properties
Add the following configuration:
sonar.jdbc.username=sonarqube sonar.jdbc.password=Your-Strong-Passwd sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube
Uncomment the following configuration:
sonar.search.javaOpts=-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError sonar.web.host=127.0.0.1 sonar.web.port=9000 sonar.web.javaAdditionalOpts=-server sonar.log.level=INFO sonar.path.logs=logs
Step 6. Configure SonarQube Systemd Service.
Let’s now create a systemd
file, so the SonarQube service can be controlled. Create the file with the command:
sudo nano /etc/systemd/system/sonarqube.service
Add the following file:
[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
Save and close the file, then reload the systemd
manager by using the following command:
sudo systemctl daemon-reload sudo systemctl start sonarqube.service sudo systemctl enable sonarqube.service
Step 7. Configure Firewall.
Now we set up an Uncomplicated Firewall (UFW) with Apache to allow public access on default web ports 9000:
sudo ufw allow OpenSSH sudo ufw allow 9000/tcp sudo ufw enable
Step 8. Accessing SonarQube Web Interface.
Once successfully installed, open your web browser and access the SonarQube installation wizard using the URL http://your-server-ip-address:9000
. Input the default username and password admin/admin and click Login. You will be redirected to the following page:
Congratulations! You have successfully installed SonarQube. Thanks for using this tutorial for installing the SonarQube on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the official SonarQube website.