How to Install SonarQube on Amazon Linux 2

In this article we are going to cover Install JDK on Amazon Linux 2, How to Install SonarQube on Amazon Linux 2, Install PostgreSQL 13, configure sonarqube and Access sonarqube web interface.

What is SonarQube ?

SonarQube is an open-source platform for continuous inspection of code quality written in java language.

SonarQube is one of popular Open-Source static code analysis tool. It will support static code analysis for many technologies like Java, Python, C#, PL/SQL, .NET, Shell Scripting.

Features of SonarQube ?

  • Open Source tool to check code coverage
  • Compatible with various programming languages.
  • Detects redundancy in code, unit test cases, integration test cases and Generates reports after testing.
  • Gives comparison between the latest report and historical report.
  • Easily integrates with build tools like- Ant, Maven, Gradle, Eclipse, etc.

Prerequisites

  • Amazon Linux 2 instance with sudo previleges
  • JDK 8+ or higher version
  • Postgres SQL

update the system packages on amazon linux 2

sudo yum update

Install the vim, wget and curl on Amazon Linux 2

sudo yum install vim wget curl -y

Increase the vm.max_map_count kernal ,file discriptor and ulimit for current session at runtime.

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

To Increase the vm.max_map_count kernal ,file discriptor and ulimit permanently .

Open the below config file and Insert the below value as shown below,

 sudo nano /etc/security/limits.conf
sonarqube   -   nofile   65536
sonarqube   -   nproc    4096

OR

If you are using systemd to manage the sonarqube services then add below value in sonarqube unit file under [service] section.

[Service]
...
LimitNOFILE=65536
LimitNPROC=4096
...

#1: Install JDK 11 on Amazon Linux 2

Install openjdk 11 on Amazon Linux 2 using below command

sudo yum install java-11-openjdk-devel -y

To check java version on Amazon Linux 2

java -version

#2: Install and Configure PostgreSQL 13 on Amazon Linux 2 for SonarQube

Enable EPEL repository on Amazon Linux 2 using below command

sudo amazon-linux-extras install epel

Add the postgresql 13 repo in Amazon Linux 2

sudo tee /etc/yum.repos.d/pgdg.repo<<EOF
[pgdg13]
name=PostgreSQL 13 for RHEL/CentOS 7 - x86_64
baseurl=https://download.postgresql.org/pub/repos/yum/13/redhat/rhel-7-x86_64
enabled=1
gpgcheck=0
EOF

Now let’s Install PostgreSQL 13 on Amazon Linux using below command

sudo yum install postgresql13 postgresql13-server

Now let’s initialize and start postgresql database service

sudo /usr/pgsql-13/bin/postgresql-13-setup initdb

Now lets enable the PostgreSQL service on Amazon Linux 2 with below commands.

sudo systemctl enable --now postgresql-13

To check PostgreSQL service on Amazon Linux 2

sudo systemctl status postgresql-13

Output:

postgresql-13.service - PostgreSQL 13 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-04-30 10:23:53 UTC; 19s ago
     Docs: https://www.postgresql.org/docs/13/static/
  Process: 724 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 729 (postmaster)

Change the password for the default PostgreSQL user.

 sudo passwd postgres

Switch to the postgres user.

su - postgres

Create a new user by typing:

createuser sonar

Switch to the PostgreSQL shell.

psql

Set a password for the newly created user for SonarQube database.

ALTER USER sonar WITH ENCRYPTED password 'DevOpsHint@123';

Create a new database for PostgreSQL database by running:

CREATE DATABASE sonarqube OWNER sonar;

grant all privileges to sonar user on sonarqube Database.

grant all privileges on DATABASE sonarqube to sonar;

Exit from the psql shell:

\q

Switch back to the sudo user by running the exit command.

exit

#3: Install SonarQube on Amazon Linux 2

Download sonaqube installer files archive To download latest version of visit SonarQube download page.

 cd /tmp
 sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.9.5.zip

Output:

https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.9.5.zip
Resolving binaries.sonarsource.com (binaries.sonarsource.com)... 91.134.125.245
Connecting to binaries.sonarsource.com (binaries.sonarsource.com)|91.134.125.245|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 208441221 (199M) [application/zip]
Saving to: ‘sonarqube-7.9.5.zip’

100%[==========================================================================>] 208,441,221 12.6MB/s   in 16s

2021-04-30 10:51:00 (12.1 MB/s) - ‘sonarqube-7.9.5.zip’ saved [208441221/208441221]

Unzip the sonarqube setup to /opt directory

sudo unzip sonarqube-*.zip -d /opt
sudo unzip sonarqube-7.9.5.zip -d /opt

Move extracted setup to /opt/sonarqube directory

 sudo mv /opt/sonarqube-7.9.5 /opt/sonarqube

#4: Configure SonarQube on Amazon Linux 2

We can’t run Sonarqube as a root user , if you run using root user it stops automatically. We have found solution on this to create saparate group and user to run sonarqube.

#4.1. Create Group and User:

Create a group as sonar

 sudo groupadd sonar

Now add the user with directory access

 sudo useradd -c "user to run SonarQube" -d /opt/sonarqube -g sonar sonar 
 sudo chown -R sonar:sonar /opt/sonarqube

Open the SonarQube configuration file using your favorite text editor.

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

Find the following lines.

#sonar.jdbc.username=
#sonar.jdbc.password=

Uncomment and Type the PostgreSQL Database username and password which we have created in above steps and add the postgres connection string.

#--------------------------------------------------------------------------------------------------

# DATABASE

#

# IMPORTANT:

# - The embedded H2 database is used by default. It is recommended for tests but not for

#   production use. Supported databases are Oracle, PostgreSQL and Microsoft SQLServer.

# - Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products.

# User credentials.

# Permissions to create tables, indices and triggers must be granted to JDBC user.

# The schema must be created first.

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

Edit the sonar script file and uncomment RUN_AS_USER and change it to RUN_AS_USER=sonar

 sudo nano /opt/sonarqube/bin/linux-x86-64/sonar.sh
# If specified, the Wrapper will be run as the specified user.

# IMPORTANT - Make sure that the user has the required privileges to write

#  the PID file and wrapper.log files.  Failure to be able to write the log

#  file will cause the Wrapper to exit without any way to write out an error

#  message.

# NOTE - This will set the user which is used to run the Wrapper as well as

#  the JVM and is not useful in situations where a privileged resource or

#  port needs to be allocated prior to the user being changed.

 RUN_AS_USER=sonar

Type CTRL+X to save and close the file.

#4.2. Start SonarQube:

Now to start SonarQube we need to do following: Switch to sonar user

 sudo su sonar

Move to the script directory

 cd /opt/sonarqube/bin/linux-x86-64/

Run the script to start SonarQube

./sonar.sh start

Output:

Starting SonarQube...

Started SonarQube

We can also add this in service and can run as a service.

#4.3. Check SonarQube Running Status:

To check if sonaqube is running enter below command,

./sonar.sh status

Output:

sonar@fosstechnix:~/bin/linux-x86-64$ ./sonar.sh status

SonarQube is running (9490).

#4.4. SonarQube Logs:

To check sonarqube logs, navigate to /opt/sonarqube/logs/sonar.log directory

 tail /opt/sonarqube/logs/sonar.log

Output:

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/h2/h2-1.3.176.jar org.sonar.ce.app.CeServer /opt/sonarqube/temp/sq-process15059956114837198848properties
 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is up
 INFO  app[][o.s.a.SchedulerImpl] SonarQube is up

using about output you will see that sonaqube is up and running successfully.

#5: Configure Systemd service for SonarQube

First stop the SonarQube service as we started manually using above steps. Navigate to the SonarQube installed path

 cd /opt/sonarqube/bin/linux-x86-64/

Run the script to start SonarQube

./sonar.sh stop

Create a systemd service file for SonarQube to run as System Startup.

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

Add the below 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

User=sonar
Group=sonar
Restart=always

LimitNOFILE=65536
LimitNPROC=4096

[Install]
WantedBy=multi-user.target

Save and close the file.

Now stop the sonarqube script earlier we started to run using as daemon. Start the Sonarqube daemon by running:

 sudo systemctl start sonar

Enable the SonarQube service to automatically  at boot time System Startup.

 sudo systemctl enable sonar

check if the sonarqube service is running,

 sudo systemctl status sonar

Successfully, We have covered How to Install SonarQube on Amazon Linux 2.

#6: Access SonarQube

To access the SonarQube using browser type server IP followed by port 9000.

http://server_IP:9000 OR http://localhost:9000

Rename the server_IP or localhost with your IP-address.

sonarqube web interface on amazon linux 2

Login to SonarQube with default administrator username and password is admin.

sonarqube default credentials admin admin

Now you have successfully logged into sonarqube web interface

logged into sonarqube

If you want to change IP address , adding multiple IP address and change the default change in SonarQube Properties as shown below

sudo nano /opt/sonarqube/conf/sonar.properties
# Binding IP address. For servers with more than one IP address, this property specifies which

# address will be used for listening on the specified ports.

# By default, ports will be used on all IP addresses associated with the server.

sonar.web.host=0.0.0.0


# Web context. When set, it must start with forward slash (for example /sonarqube).

# The default value is root context (empty value).

#sonar.web.context=

# TCP port for incoming HTTP connections. Default value is 9000.

sonar.web.port=9000

Conclusion:

In this article, We have performed ,How to Download and How to Install SonarQube on Amazon Linux 2 with Configure Sonarqube, Creating Systemd Service and Troubleshooting sonarqube

Related Articles:

About DevOps Hint

DevOps Hint Founded in 2020 Community Site where you can find about How to Guides, Articles and Troubleshooting Tips for DevOps, SRE, Sysadmins and Developers.

1 thought on “How to Install SonarQube on Amazon Linux 2”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link