There is a new version of this tutorial available for Ubuntu 22.04 (Jammy Jellyfish).

SonarQube is a free and open-source and web-based platform used to analyze the source code quality. It is written in java and can analyze and manage code of more than 20 programming languages, including c/c++, PL/SQL, Cobol, etc through plugins. It can be integrated with other database servers like MSSQL, PostgreSQL, Oracle, and MySQL. It continuously inspects and shows the health of an application. It has a lot of plugins that help you to extend its functionality. In this tutorial, we will show you how to install SonarQube on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A valid domain name pointed with your server IP.
  • A root password is configured the server.

Getting Started

First, you will need to update your system packages to the latest version. You can update all of them with the following command:

apt-get update -y

After updating all the packages, you will need to Increase the vm.max_map_count kernal ,file discriptor and ulimit in your system. You can do it with the following commands:

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

Once you are done, you can proceed to the next step.

Install Java

SonarQube is a Java based application. So Java must be installed in your system. If not installed, you can install it with the following command:

apt-get install default-jdk unzip gnupg2 -y

Once the Java is installed, verfiy the Java version using the following command:

java --version

You should get the following output:

openjdk 11.0.9.1 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

Once you are finished, you can proceed to the next step.

Install and Configure PostgreSQL Server

SonarQube uses PostgreSQL as a database backend. So you will need to install it in your server. By default, the latest version of PostgreSQL is not available in the Ubuntu 20.04 default repository. So you will need to add the PostgreSQL repository to your system.

First, add the PostgreSQL repository with the following command:

sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

Next, add the GPG key with the following command:

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

Next, update the repository and install the PostgreSQL server with the following command:

apt-get update -y
apt-get -y install postgresql postgresql-contrib

Once installed, start the PostgreSQL service and enable it to start at system reboot with the following command:

systemctl start postgresql
systemctl enable postgresql

Next, set the PostgreSQL password with the following command:

passwd postgres

You will need to ask for a new password as shown below:

New password: 
Retype new password: 
passwd: password updated successfully

Next, switch the user to postgres and create a new user for sonar with the following command:

su - postgres
createuser sonar

Next, lgin to PostgreSQL shell with the following command:

[email protected]:~$ psql

Once login, you should get the following output:

psql (13.1 (Ubuntu 13.1-1.pgdg20.04+1))
Type "help" for help.

Next, create a user and database with the following command:

postgres=# ALTER USER sonar WITH ENCRYPTED password 'sonar';
postgres=# CREATE DATABASE sonarqube OWNER sonar;

Next, grant all the privileges to the Sonar database with the following command:

postgres=# grant all privileges on DATABASE sonarqube to sonar;

Next, exit from the postgres shell and user with the following command:

postgres=# \q
[email protected]:~$ exit

Once you are finished, you can proceed to the next step.

Install and Configure SonarQube

First, download the latest version of SonarQube from its official website with the following command:

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

Once the download is completed, unzip the downloaded file with the following command:

unzip sonarqube-7.9.3.zip

Next, move the extracted directory to the /opt with the following command:

mv sonarqube-7.9.3 /opt/sonarqube

Next, create a separate user for SonarQube with the following command:

adduser sonar

Next, change the ownership of the /opt/sonarqube directory to sonar with the following command:

chown -R sonar:sonar /opt/sonarqube

Next, you will need to edit the SonarQube configuration file and define sonar username, password, web host and PostgreSQL url.

nano /opt/sonarqube/conf/sonar.properties

Change the following lines with your sonar username, password and PostgreSQL URL:

sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube
sonar.web.host=0.0.0.0

Next, edit the sonar.sh script and define the RUN_AS user:

nano /opt/sonarqube/bin/linux-x86-64/sonar.sh

Change the following line:

RUN_AS_USER=sonar

Save and close the file when you are finished.

Create a Systemd Service File for SonarQube

Next, you will need to create a systemd service file to manage the SonarQube service. You can create it with the following command:

nano /etc/systemd/system/sonar.service

Add the following lines:

[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
LimitNOFILE=65536
LimitNPROC=4096
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 close the file then reload the systemd daemon to apply the configuration changes:

systemctl daemon-reload

Next, start the SonarQube service and enable it to start at system reboot with the following command:

systemctl start sonar
systemctl enable sonar

Next, verify the status of the SonarQube service with the following command:

systemctl status sonar

You should get the following output:

? sonar.service - SonarQube service
     Loaded: loaded (/etc/systemd/system/sonar.service; disabled; vendor preset: enabled)
     Active: active (running) since Mon 2020-11-16 16:28:41 UTC; 5s ago
    Process: 79015 ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start (code=exited, status=0/SUCCESS)
   Main PID: 79081 (wrapper)
      Tasks: 46 (limit: 4691)
     Memory: 725.8M
     CGroup: /system.slice/sonar.service
             ??79081 /opt/sonarqube/bin/linux-x86-64/./wrapper /opt/sonarqube/bin/linux-x86-64/../../conf/wrapper.conf wrapper.syslog.ident=So>
             ??79085 java -Dsonar.wrapped=true -Djava.awt.headless=true -Xms8m -Xmx32m -Djava.library.path=./lib -classpath ../../lib/jsw/wrap>
             ??79125 /usr/lib/jvm/java-11-openjdk-amd64/bin/java -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInit>

Nov 16 16:28:40 sonar.example.com systemd[1]: Starting SonarQube service...
Nov 16 16:28:40 sonar.example.com sonar.sh[79015]: Starting SonarQube...
Nov 16 16:28:41 sonar.example.com sonar.sh[79015]: Started SonarQube.
Nov 16 16:28:41 sonar.example.com systemd[1]: Started SonarQube service.

At this point, SonarQube is started and listening on port 9000. You can check it with the following command:

ss -antpl | grep 9000

You should get the following output:

LISTEN   0        25                          *:9000                   *:*       users:(("java",pid=139294,fd=121))                                             

If you have any issue with SonarQube, you can check the SonarQube logs with the following command:

tail -f /opt/sonarqube/logs/sonar.log

You should get the following output:

2020.11.16 17:04:24 INFO  app[][o.s.a.ProcessLauncherImpl] Launch process[[key='ce', ipcIndex=3, logFilenamePrefix=ce]] from [/opt/sonarqube]: /usr/lib/jvm/java-11-openjdk-amd64/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/opt/sonarqube/temp --add-opens=java.base/java.util=ALL-UNNAMED -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -Dhttp.nonProxyHosts=localhost|127.*|[::1] -cp ./lib/common/*:/opt/sonarqube/lib/jdbc/postgresql/postgresql-42.2.5.jar org.sonar.ce.app.CeServer /opt/sonarqube/temp/sq-process10447466834580828864properties
2020.11.16 17:04:30 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is up
2020.11.16 17:04:30 INFO  app[][o.s.a.SchedulerImpl] SonarQube is up

Once you are finished, you can proceed to the next step.

Install and Configure Nginx

Next, you will need to install and configure Nginx as a reverse proxy for SonarQube. First, install the Nginx server with the following command:

apt-get install nginx -y

Once the Nginx is installed, create a ne wNginx virtual host configuration file for SonarQube:

nano /etc/nginx/conf.d/sonar.conf

Add the following lines:

upstream sonar_backend {
  server 127.0.0.1:9000;
}

server {
    listen 80;
    server_name sonar.example.com;

    location / {
        proxy_pass http://sonar_backend/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forward-Proto http;
        proxy_set_header X-Nginx-Proxy true;

        proxy_redirect off;
    }
}

Save and close the file then verify the Nginx for any syntax error with the following command:

nginx -t

You should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, check the status of the Nginx service with the following command:

systemctl status nginx

You should see the following output:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2020-11-16 17:04:16 UTC; 4min 3s ago
       Docs: man:nginx(8)
    Process: 140017 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 140028 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 140035 (nginx)
      Tasks: 3 (limit: 4691)
     Memory: 4.6M
     CGroup: /system.slice/nginx.service
             ??140035 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??140037 nginx: worker process
             ??140038 nginx: worker process

Nov 16 17:04:16 kolab.example.com systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 16 17:04:16 kolab.example.com systemd[1]: Started A high performance web server and a reverse proxy server.

Once you are finished, you can proceed to the next step.

Access SonarQube Web UI

Now, open your web browser and access the SonarQube using the URL http://sonar.example.com. You will be redirected to the following page:

Now, click on the Log in button. You should see the following page:

Provide default username as admin and the password as admin then click on the Login button. You should see the SonarQube default dashboard in the following screen:

Conclusion

Congratulations! you have successfully installed and configured SonarQube with Nginx as a reverse proxy on Ubuntu 20.04. I hope you can now deploy it easily in the development environment. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

0 Comment(s)

Add comment