Sonarqube Installation on Ubuntu

To run SonarQube, we need to have Java installed on the node

  • Let's install OpenJDK 17, Login to the node with the root access (this depends on which user you would like to install Java on. If you have multiple users and have multiple Java versions that need to be run then installing it on the non-root user makes sense!!). However, I don't need multiple Java versions on my node. So, I am going ahead with the root user

However, SonarQube cannot be run as root on Unix-based systems, so create a dedicated user account for SonarQube if necessary.

The SonarQube server requires Java version 17, here we are installing SonarQube Version 9.9.2.

Always check the requirements before installation, the requirements are provided at https://docs.sonarsource.com/sonarqube/latest/requirements/prerequisites-and-overview/

  1. To create a new user, use the useradd command followed by the desired username. For example, to create a user named "newuser," use the following command:

     sudo useradd <username>
    

    The sudo command is used to run the useradd command with superuser privileges.

  2. Set a password for the new user with the passwd command:

     sudo passwd <username>
    

    You'll be prompted to enter and confirm the new user's password.

    1. If you are creating a user on the cloud VMs, there may be a chance that it won't let you switch to that user by throwing the error message "This account is currently not available".

      The error message "This account is currently not available" typically indicates that the user account you're trying to switch to does not have a valid login shell or that the shell is set to /usr/sbin/nologin, which is a security measure to prevent users from logging in interactively.

    2. To resolve this issue, you can change the user's login shell to a valid shell that allows interactive login, such as /bin/bash. Here's how you can change the shell for the "<username>" user:

      • Manually create the /home/<user folder>

          sudo mkdir /home/<username> 
          sudo chown <username>:<groupname> /home/sonarqube
        
      • Change the login shell for the "<username>" user using the chsh command. For example, to change the shell to /bin/bash, you can run:

          sudo chsh -s /bin/bash <username>
        
      • Replace /bin/bash with the path to a valid shell on your system if needed.

      • Now, try to switch to the "<username>" user again using the su command:

          su - <username>
        

        This should allow you to switch to the "<username>" user with an interactive shell. If you encounter any issues, make sure that the specified shell exists on your system and is correctly configured in the /etc/passwd file for the "<username>" user.

sudo apt-get install openjdk-17-jdk -y

From now onwards, please use the non-root user for all the postgresSQL and SonarQube Installation

  • First, let's add the PostgreSQL repository.
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ lsb_release -cs-pgdg main" /etc/apt/sources.list.d/pgdg.list'

Now let's add the PostgreSQL signing key

wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
  • Install PostgreSQL
sudo apt install postgresql postgresql-contrib -y
  • Enable the database server to start automatically on reboot.
sudo systemctl enable postgresql
  • Start the database server.
sudo systemctl start postgresql
  • change the password of the postgres user
sudo passwd postgres

Please remember the password or note it down somewhere for future use.

Switch to the postgres user.

su - postgres

create a user in the postgres database

createuser sonarqube

Log in to PostgreSQL.

psql

Now, we need to create a password for the user sonarqube such as "Pass_Word" (generally it is good to have a complex password)

ALTER USER sonarqube WITH ENCRYPTED password 'Pass_Word';

Create a sonarqube database and set the owner to sonarqube .

CREATE DATABASE sonarqube OWNER sonar;

Grant all the privileges on the sonarqube database to the sonarqube user.

GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonarqube ;

Exit PostgreSQL.

\q

now return to the root user or the non-root user used at the start of the installation

 exit

or

su - <username>

Download and Install SonarQube Install the zip utility, which is needed to unzip the SonarQube files.

sudo apt-get install zip -y

So, now to find the latest distribution available go to https://binaries.sonarsource.com/Distribution/sonarqube/ and scroll down to check which is the latest zip available copy the link and perform the following steps

Navigate to the /opt folder

cd /opt

Download the SonarQube distribution files.

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

Unzip the downloaded file.

sudo unzip sonarqube-9.9.2.77730.zip

Move the unzipped files to /opt/sonarqube directory

sudo mv sonarqube-9.6.1.59531 sonarqube 
sudo mv sonarqube /opt/

Add SonarQube Group and the user that was used in the entire process till now and the group for SonarQube. So, let's Create a sonar group.

sudo groupadd sonar

Create a sonar user and set /opt/sonarqube as the home directory.

sudo useradd -d /opt/sonarqube -g <groupname> <username>

if the user is already added then move to the next step, Grant the <username> user access to the /opt/sonarqube directory.

sudo chown <username>:<groupname> /opt/sonarqube -R
  • Configure SonarQube Edit the SonarQube configuration file.
sudo nano /opt/sonarqube/conf/sonar.properties

Find the following lines:

#sonar.jdbc.username=

#sonar.jdbc.password=

Uncomment the lines, and add the database user and password you created in Step2.

sonar.jdbc.username=sonarqube

sonar.jdbc.password=Pass_Word

also add the sonar.jdbc.url as well , as shown below

sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube

Save and exit the file.

Setup Systemd service, Create a systemd service file to start SonarQube at system boot.

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

Paste the following lines to the 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=sonar
Group=sonar
Restart=always

LimitNOFILE=65536
LimitNPROC=4096

[Install]
WantedBy=multi-user.target

Save and exit the file.

Enable the SonarQube service to run at system startup

Enable the process to run automatically as soon as the vm restarts
sudo systemctl enable sonar
Start the SonarQube service.
sudo systemctl start sonar
Check the service status
sudo systemctl status sonar
  • SonarQube uses Elasticsearch to save its data in a special directory on your system called as MMap FS directory. To make this work correctly, we need to adjust a few settings in the system to match SonarQube's needs

Edit the sysctl configuration file.

sudo nano /etc/sysctl.conf

Add the following lines.

vm.max_map_count=262144
fs.file-max=65536
ulimit -n 65536
ulimit -u 4096

Save and exit the file, Reboot the system to apply the changes.

sudo reboot

Now we can access SonarQube in the web browser at your server's IP address on port 9000. For example:

http://serverip:9000

As soon as the Sonarqube loads , it displays as username and password , by default the details are

username:admin

password:admin

SonarQube will prompt you to change your password

Update the password and you are ready!!