Installing SonarQube on Ubuntu 22.04
First, install some packages that will be needed for SonarQube.
sudo apt install curl gnupg software-properties-common apt-transport-https lsb-release -y
Next install OpenJDK-11.
sudo apt install openjdk-11-jdk -y
You can confirm that java has been installed by running:
java -version
Increase the virtual memory for the current session at runtime. In case of errors setting the limits, log in as root and retry.
sudo sysctl -w vm.max_map_count=524288
sudo sysct -w fs.file-max=131072
ulimit -n 131072
ulimit -u 8192
To make these changes permanent, create the following file:
sudo vi /etc/security/limits.d/99-sonarqube.conf
Add the following lines:
sonarqube - nofile 131072
sonarqube - nproc 8192
Reboot your machine.
reboot
After rebooting, create a new user that will access SonarQube as it won’t run if you are a root user. We will create a user called sonar who will be added to group sonar.
sudo adduser –system –no-create-home –group –disabled-login sonar
Now it is time to install the PostgreSQL database that SonarQube will use. Add its GPG key via:
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg –dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null
Add the PostgreSQL repository to your Ubuntu machine.
echo “deb [arch=amd64] http://apt.postgresql.org/pub/repos/apt/ jammy-pgdg main” | sudo tee /etc/apt/sources.list.d/postgresql.list
Update system packages and then install PostgreSQL 13.
sudo apt update; sudo apt install postgresql-13 -y
Confirm that PostgreSQL is enabled and running.
sudo systemctl status postgresql
PostgreSQL will automatically create its own user and group of postgres, but no password is set. You can set it by running the following command and inputting the password twice.
sudo passwd postgres
Once you have successfully updated the password, log in as user postgres using the password you just created.
su - postgres
Create a new user called sonaruser (or whatever name you want to give the user).
createuser sonaruser
Access the PostgreSQL shell.
psql
Set password for that user.
ALTER USER sonaruser WITH ENCRYPTED password ‘<your_password_here>’;
Create a database called sonarqube (you can name it anything you like) and make the user you created its owner. Grant all privileges to that user. Once done,, you can safely exit the PostgreSQL shell. You will then be taken back to the terminal where you are logged in as user postgres. Log out of it to return to your own user account.
CREATE DATABASE sonarqube OWNER sonaruser;
GRANT ALL PRIVILEGES on DATABASE sonarqube TO sonaruser;
\q
exit
Go to https://www.sonarsource.com/products/sonarqube/downloads/ and click on “Download”.
Scroll down until you find “Community Edition”, right click the download button for that edition and copy the link address. At the time of writing this, SonarQube version 9.8 is available so my ZIP file is 9.8.0.63668.zip.
In your terminal, use wget to download the ZIP file to your local machine by pasting the link you copied as its argument.
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.8.0.63668.zip
Using the unzip package (download it if you don’t already have it using sudo apt install unzip -y
), extract the contents of the zipped folder to the /opt/ directory.
sudo unzip sonarqube-9.8.0.63668.zip -d /opt/
Rename the unzipped folder to sonarqube.
sudo mv /opt/sonarqube-9.8.0.63668/ /opt/sonarqube
Make the sonar user (or the user you created right before downloading PostgreSQL) the owner of this directory.
sudo chown sonar:sonar /opt/sonarqube -R
Open the sonar.properties file contained in the conf subdirectory of this folder.
sudo vi /opt/sonarqube/conf/sonar.properties
Look for the following two lines, uncomment them, and set their values as follows:
sonar.jdbc.username=sonaruser (or the user you created for your PostgreSQL database)
sonar.jdbc.password=<your_password_here>
Right below these two lines, add the following:
- sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube
Find and uncomment the following lines and configure their values accordingly:
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
Create a systemd service file for SonarQube.
sudo vi /etc/systemd/system/sonar.service
Copy and paste the following, making sure to set the correct values for User and Group if you chose a different name:
[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=on-failure
LimitNOFILE=65536
LimitNPROC=4096
[Install]
WantedBy=multi-user.target
Reload the daemon then enable sonar and start it.
sudo systemctl daemon-reload
sudo systemctl enable sonar
sudo systemctl start sonar
Confirm that SonarQube is enabled and running.
sudo systemctl status sonar
Open /etc/sysctl.conf with your preferred text editor and add the following lines at the bottom:
vm.max_map_count=524288
fs.file-max=131072
ulimit -n 131072
ulimit -n 8192
Reboot your system.
reboot
Once your system is back up, access localhost:9000 on your browser and wait for SonarQube to start up. Once that happens, log in with username admin and password admin.
Once you are logged in, you will be prompted to update your password.
In your best hacker voice, softly whisper to yourself, “I’m in”, and then go find a tutorial on creating SonarQube projects