Home / How To / How to install SonarQube on Ubuntu 18.04 LTS

How to install SonarQube on Ubuntu 18.04 LTS



SonarQube is an open source platform for continuous program quality inspection. It is written in Java language and supports multiple databases. You can inspect code and check the health of an application for more than 20 programming languages, including Java, C, C ++, C #, PHP and Web languages ​​like JavaScript, HTML and CSS. SonarQube can analyze source code, find security issues, detect errors and display results on web-based dashboard. You can easily integrate SonarQube with Maven, Ant, Gradle, MSBuild, LDAP, Active Directory and GitHub.

In this tutorial, we will read how to install SonarQube on a Ubuntu 18.04 LTS (Bionic Beaver) server.

Requirements

  • A server running Ubuntu 1
    8.04.
  • A non-root user with sudo privileges.

Getting Started

Before you begin, you must update your system with the latest version. You can do this by running the following command:

  sudo apt-get update -y 
sudo apt-get upgrade -y

When your system is updated, restart the system to apply the changes.

Install Java

SonarQube is written in Java language, so you must install Java on your system. First add the Java archive with the following command:

  sudo add-apt repository ppa: webupd8team / java 

Next update the repository and install Java with the following command:

  sudo apt-get update - 

sudo apt "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2 + 13-Ubuntu-1ubuntu0.18.04.3)
OpenJDK 64-bit server World Cup (build 10.0.2 + 13-Ubuntu-1ubuntu0.18.04.3, mixed mode)

Install and configure PostgreSQL

By default, the latest version of PostgreSQL is not available in the default database Ubuntu 18.04. So you must add the PostgreSQL repository to your system.

You can do this with the following command:

  sudo sh-c "echo" deb http://apt.postgresql.org/pub/repos/ apt / `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
wget -q https://www.postgresql.org/media/keys/ACCC4CF8. asc -O- | sudo apt-key add -

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

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

When the installation is complete, check PostgreSQL status with the following command:

  sudo systemctl status postgresql 

Output:

? postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; supplier preset: enabled)
Active: Active (finished) since Sun 2018-12-02 08:49:29 UTC; 4h 30min ago
Process: 1295 ExecStart = / bin / true (code = exited, status = 0 / SUCCESS)
Main PID: 1295 (Code = Completed, Status = 0 / SUCCESS)

Dec 02 08:49:29 ubuntu1804 systemd [1]: Start PostgreSQL RDBMS ...
Dec 02 08:49:29 ubuntu1804 systemd [1]: Started PostgreSQL RDBMS.

Next, change to the postgres user with the following command:

  su-postgres 

Then create a sonar user using the following command:

  createuser sonar 

Next change the PostgreSQL shell with the following command: 19659008] psql

Then enter the password for sonar users and create a sonar database with the following command:

  ALTER USER sonar with ENKRYT password "password"; 
Create and configure SonarQube

Create a SonarQube user first with the following command:

  sudo adduser sonar 

Create DATABASE sonar OWNER sonar

Next, exti from the PostgreSQL shell:

   q 

download the latest version of SonarQube with the following command:

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

When the download is complete, unzip it downloaded file with the following command:

  unzip sonarqube-6.7.6.zip 

Next copy the extracted directory to / select with the following command:

  sudo cp -r sonarqube-6.7.6 / opt / sonarqube 

] Next, give ownership to the sonar user with the following command:

  sudo chown - R sonar: sonar / opt / sonarqube 

Next, you must configure SonarQube to act as a sonar user. You can do this with the following command:

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

Please make the following changes:

 RUN_AS_USER = sonar

Save and close the file. Then open the SonarQube default configuration file and change the database data with the one that we created earlier:

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

Prepare the following:

 sonar.jdbc.username = sonar
sonar.jdbc.password = password
sonar.jdbc.url = jdbc: postgresql: // localhost / sonar
sonar.web.host = 127.0.0.1
sonar.search.javaOpts = -Xms512m -Xmx512m

Save and close the file when done.

Create Systemd Service File for SonarQube

Then you must create a systemed service file to manage the SonarQube service. You can do this with the following command:

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

Add 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

User = sonar
Group = sonar
Restart = Always

[Install]
WantedBy = multi-user.target

Save and close the file when done. Then start the SonarQube service and enable it at start time with the following command:

  sudo systemctl start sonar 
sudo systemctl enable sonar

You can check the status of the SonarQube service with the following command:

 ] sudo systemctl status sonar 

Output:

? sonar.service - SonarQube service
Loaded: loaded (/etc/systemd/system/sonar.service; disabled; provider preset: enabled)
Active: Active (running) since Sun 2018-12-02 13:55:34 UTC; 2min 52s ago
Process: 2339 ExecStart = / opt / sonarqube / bin / linux-x86-64 / sonar.sh start (code = completed, status = 0 / SUCCESS)
Main PID: 2396 (cover)
Tasks: 133 (Limit: 2323)
CGroup: /system.slice/sonar.service
?? 2396 /opt/sonarqube/bin/linux-x86-64/./wrapper/opt/sonarqube/bin/linux-x86-64/../../conf/wrapper.conf wrapper.syslog.ident = SonarQ
?? 2399 java -Dsonar.wrapped = true -Djava.awt.headless = true -Xms8m -Xmx32m -Djava.library.path =. / Lib -classpath ../../lib/jsw/wrapper-
? 2445 / usr / lib / jvm / java-8-oracle / jre / bin / java -XX: + UseConcMarkSweepGC -XX: CMSInitiatingOccupancyFraction = 75 -XX: + UseCMSInitiatingOc
?? 2545 / usr / lib / jvm / java-8-oracle / jre / bin / java -Djava.awt.headless = true -Dfile.encoding = UTF-8 -Djava.io.tmpdir = / opt / sonarqube / temp -
?? 2622 / usr / lib / jvm / java-8-oracle / jer / bin / java -Djava.awt.headless = true -Dfile.encoding = UTF-8 -Djava.io.tmpdir = / opt / sonarqube / temp -

Dec 02 13:55:33 ubuntu1804 systemd [1]: Start SonarQube service ...
Dec 02 13:55:33 ubuntu1804 sonar.sh [2339]: Start SonarQube ...
Dec 02 13:55:34 ubuntu1804 sonar.sh [2339]: Started SonarQube.
Dec 02 13:55:34 ubuntu1804 systemd [1]: Started SonarQube service.

Configure Apache for SonarQube

By default, SonarQube is listening to port 9000. Then you must install and configure Apache as reverse proxy to access SonarQube with port 80.

Install Apache to do this with the following command:

  sudo apt-get install apache2 -y 

Then activate the mod_proxy module with the following command:

  sudo a2enmod proxy 
sudo a2enmod proxy_http

Next, create an Apache virtual host file for SonarQube with the following command :

  sudo nano /etc/apache2/sites-available/sonar.conf

Add the following lines:

 
ServerName example.com
ServerAdmin [email protected]
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:9000/
ProxyPassReverse / http://127.0.0.1:9000/
TransferLog /var/log/apache2/sonarm_access.log
ErrorLog /var/log/apache2/sonar_error.log

Replace example.com with your own domain name. Save and close the file. Then activate SonarQube virtual host file with the following command:

  sudo a2ensite sonar 

Finally, restart Apache and SonarQube service to apply all changes with the following command:

  sudo systemctl restart apache2 
sudo systemctl restart sonar

By default, SonarQube stores its logs on the / opt / sonarqube / logs directory. You can check SonarQube log with the following command:

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

Output:

Close a JVM ...
Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
Copyright 1999-2006 Tanuki Software, Inc. All rights reserved.

2018.12.02 13:55:43 INFO app [][o.s.a.AppFileSystem]   Cleaning or creating temp directory / opt / sonarqube / temp
2018.12.02 13:55:44 INFO app [][o.s.a.es.EsSettings]   Elasticsearch listening on /127.0.0.1:9001
2018.12.02 13:55:45 INFO app [][o.s.a.p.ProcessLauncherImpl]   Startup process [[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/opt/sonarqube/elasticsearch]: / opt / sonarqube / elasticsearch / bin / elasticsearch -Epath.conf = / opt / sonarqube / temp / conf / es
2018.12.02 13:55:45 INFO app [][o.s.a.SchedulerImpl]   Waiting for Elasticsearch to run
2018.12.02 13:55:48 INFO app [][o.e.p.PluginsService]   No modules loaded
2018.12.02 13:55:48 INFO app [][o.e.p.PluginsService]   Uploaded plugin [org.elasticsearch.transport.Netty4Plugin]
2018.12.02 13:56:34 INFO app [][o.s.a.SchedulerImpl]   Process [es] is up
2018.12.02 13:56:34 INFO app [][o.s.a.p.ProcessLauncherImpl]   Startup process [[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/opt/sonarqube]: / usr / lib / jvm / java-8-oracle / jre / bin / java -Djava.awt.headless = true - Dfile.encoding = UTF-8 -Djava.io.tmpdir = /opt / sonarqube / temp -Xmx512m -Xms128m -XX: + HeapDumpOnOutOfMemoryError -cp ./lib/common/*:./lib/server/*:/opt / sonarqube / lib / jdbc / postgresql / postgresql-42.2.1.jar org.sonar.server.app.WebServer / opt / sonarqube / temp / sq-process420500314195865484properties

You can also check SonarQube weblog with the following command:

  sudo tail -f /opt/sonarqube/logs/web.log

Output:

2018.12.02 13:57:03 INFO web [19659096] Install plugin SonarJava / 4.15.0.12310 / 572454b93016ec73a53fe0e07b2ffdc356d21ba9
2018.12.02 13:57:03 INFO web [][o.s.s.p.ServerPluginRepository]   Install plugin SonarPHP / 2.11.0.2485 / 741861a29e5f9a26c6c99c06268facb6c4f4a882
2018.12.02 13:57:03 INFO web [][o.s.s.p.ServerPluginRepository]   Install plugin SonarPython / 1.8.0.1496 / 3fe3bc4d0273a5721ea2fb368dc45b1bb82fede3
2018.12.02 13:57:03 INFO web [][o.s.s.p.ServerPluginRepository]   Install plugin SonarQube :: Plugins :: SCM :: Git / 1.3.0.869 / 4da53e3f9e55f4f2e5796625cb0c5768ed152079
2018.12.02 13:57:03 INFO web [][o.s.s.p.ServerPluginRepository]   Install plugin SonarQube :: Plugins :: SCM :: SVN / 1.6.0.860 / 2111fdbd1dddda4ad6d4ed6486fd0b18c1010d3b
2018.12.02 13:57:03 INFO web [][o.s.s.p.ServerPluginRepository]   Install plugin SonarTS / 1.1.0.1079 / 042c9e65239a47d92d305f9767f730b3cc1e5ed3
2018.12.02 13:57:03 INFO web [][o.s.s.p.ServerPluginRepository]   Install plugin SonarXML / 1.4.3.1027 / 39588245cecf538bb27be4e496ff303b0143d20b
2018.12.02 13:57:07 INFO web [][o.s.s.p.d.m.c.PostgresCharsetHandler]   Verify that database schema supports UTF8
2018.12.02 13:57:09 INFO web [][o.s.s.p.w.MasterServletFilter]   Initializing Servlet Filter [email protected] [pattern=UrlPattern{inclusions=[/api/system/migrate_db/*, ...] Exclusions = [/api/properties*, ...]}]
2018.12.02 13:57:09 INFO web [][o.s.s.a.EmbeddedTomcat]   HTTP connection enabled on port 9000
2018.12.02 13:57:16 INFO web [][o.s.s.p.UpdateCenterClient]   Update Center: https://update.sonarsource.org/update-center.properties (no proxy)

Access SonarQube

SonarQube is now installed and configured. It's time to access it through the browser.

Open your browser and type URL http://example.com. You will be redirected to the following page:

 SonarQube

Here, click Log In the button. You should see the following page:

 Login

Enter the username and password for admin account as admin / admin and click Log In the button. You should see the SonarQube default panel on the following page:

  SonarCube Dashboard

Congratulations! You have successfully installed SonarQube on the Ubuntu 18.04 server. You can now easily perform automatic reviews and check the health of an application with SonarQube.


Source link