Install SonarQube on Ubuntu 20.04 LTS Server – Linux Shout – H2S Media

SonarQube is an open-source program to analyze the code quality, formerly it was known as Sonar. Here we will let you know the commands and steps to install SonarQube on Ubuntu 20.04/18.04 LTs server  

This program can find the security vulnerabilities in more than 20 programming languages along with auto analyzing of code quality to detect code bugs and smells. It also offers reports on duplicated code, coding standards, unit tests, code coverage, code complexity, comments, bugs, and security vulnerabilities.

1. Run Ubuntu system update

The first thing we should do before installing any software on Linux using command terminal is running of update command, thus run:

sudo apt update

2. Install Java OpenJDK

Java is one of the requirements to install and set up SonarQube on Ubuntu 20.04 or 18.04 and its based operating systems.

sudo apt install openjdk-11-jdk

Increase the Virtual memory

sudo sysctl -w vm.max_map_count=524288
sudo sysctl -w fs.file-max=131072
ulimit -n 131072
ulimit -u 8192

Reboot your system once…

3. Create a Dedicated user for Sonarqube

The latest version of Sonar cannot run under root user, thus we will create a new user to access only Sonarqube installation.

Add user

sudo useradd sonarh2s

Set password for the created user

sudo passwd sonarh2s

Note: you can change sonarh2s with whatever username and password you want to set.

3. Install PostgreSQL Database

Ubuntu’s base repository doesn’t have the latest version of PostgreSQL thus to get the latest one, we have to add its repo manually. Here is the command to do that.

Add GPG key:

wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O- | sudo apt-key add -

Add repo: 

echo "deb [arch=amd64] http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list

Run system update

sudo apt update

Install PostgreSQL 13

sudo apt install postgresql-13

You can check the status of  its service using 

sudo systemctl status postgresql

4. Create a database for Sonar

1. Once the installation is completed, let’s create a PostgreSQL database for Sonarqube but before that set password:

sudo passwd postgres

2. Switch to postgres user. Use the password you have set above.

su - postgres

3. Now, create a  new user that will access the database we will create for Sonarqube.

createuser sonaruser

Note: Change sonaruser in the above command with whatever you want to use.

4. Switch to the PostgreSQL shell.

psql

5. To secure a newly created user, set a password for the same using the below syntax:

ALTER USER sonaruser WITH ENCRYPTED password 'yourpassword';

Note: change the bold items with whatever you want to use.

6. Create a new database for PostgreSQL database by running:

CREATE DATABASE sonardb OWNER sonaruser;

Note: You can use the DB name as per your choice and also don’t forget to replace the user in the above command with the one you have created.

7. Exit from the psql shell:

q

8. Get back to your system user

exit

5. Download and Setup SonarQube on Ubuntu 20.04/18.04

While writing this article the latest version of the Sonarqube was v-9.0.1 available to download. However, you can directly visit the official website to get the latest version. You can also visit the download page and copy the link to download with wget command, as we have done here:

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.0.1.46107.zip

Extract and move to /opt directory:

sudo apt -y install unzip
sudo unzip sonarqube-*.zip -d /opt
sudo mv /opt/sonarqube-* /opt/sonarqube

Note: If you have downloaded the file using the browser then you have to first switch to the Downloads directory before running the above commands

Configure Database for Sonar

1. Open the configuration file:

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

2. Now, add the following lines:

As shown in the screenshot, copy-paste the following lines. After that change the bold values:

sonar.jdbc.username=sonaruser
sonar.jdbc.password=yourpassword
sonar.jdbc.url=jdbc:postgresql://localhost/sonardb

Just replace these values with what you have used while creating a database on Postgresql for Sonarqube.

command explanation:

sonaruser – is a database username
yourpassword – is the database password
sonardb– is the database name we have created

To save and exit the file- press Ctrl+X and then type- Y and hit the Enter key.

 

6. Create SonarQube Systemd service file

By default, there will be no service file for Sonarqube to start it in the background and with system boot. Hence, we have to create one manually. Here is the way:

sudo nano /etc/systemd/system/sonar.service

Copy-paste the following lines:

[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
LimitNOFILE=131072
LimitNPROC=8192
User=sonarh2s
Group=sonarh2s
Restart=on-failure
[Install]
WantedBy=multi-user.target

Note: Replace the value of User and Group with the username that you have created at the beginning of the article for Sonarqube.

Save the file- press Ctrl+X then type- Y and hit the Enter key.

Reload the daemon:

sudo systemctl daemon-reload

Then start and enable the service

sudo systemctl enable sonar sudo systemctl start sonar 

Now, check whether the create Sonarqueb service running or not 

sudo systemctl status sonar

[optional] Alternatively, you can also use the below commands to start, stop, and check status:

sudo -Hu sonar /opt/sonarqube/bin/linux-x86-64/sonar.sh status
sudo -Hu sonar /opt/sonarqube/bin/linux-x86-64/sonar.sh start
sudo -Hu sonar /opt/sonarqube/bin/linux-x86-64/sonar.sh stop

To get the console output to know what is happening while starting the Sonarqube server you can use:

sudo -Hu sonar /opt/sonarqube/bin/linux-x86-64/sonar.sh console

This will be helpful in resolving some errors.

7. Allow Sonarqube port in Ubuntu 20.04 firewall

To access the web interface of Sonarqube you have to open its default 9000 port in your Ubuntu system’s firewall:

sudo ufw allows 9000/tcp

8. Access Sonarqube Web interface and log in with default admin username

Finally, open any browser that can access the IP address or domain of the server where you have installed Sonarqube. And point it to-

http://server-ip-addres:9000
or http://you-somain.com:9000

Note: Replace server-ip-addres with your server/desktop IP address or domain name.

Once you see the login screen, use the default Sonarqube username and password that is admin.

when it asks you to change the old password, do that.

Other Articles:

Steps for Installing Postgresql in Ubuntu 20.04 LTS
How to install Debian Linux server on Windows 10 without Virtualbox
Use RDP on Linux mint to access Windows 11/10/7
How to access Ubuntu or Mint via RDP from Windows 7/10/11 to