How Do You Install SonarQube Scanner on a RHEL Server?

Problem scenario
SonarQube does not work without a launcher installed.  The web UI seems complete, but it is not.  You need a CLI launcher of some sort on the back-end.  You want to use sonar-scanner from the command prompt to analyze some code.  You have installed SonarQube and configured it so the web UI front end allows you to log in.  How do you install the sonar-scanner command line interface tool?

Solution
Prerequisites

i.  You must have SonarQube installed.  If you need directions, see this posting.

ii.  Install unzip if it is not already installed.  To install it, run this command: sudo yum -y install unzip

Procedures
1.  Download the .zip file from SonarQube's website.  (Go to the link.  Click on the link for Linux 64 bit.)

2.  Expand in a directory of your choice.  You may want to move the .zip file to /opt/ and run this command:
sudo unzip sonar-scanner-cli*.zip

3.  Run this command:  ls

4.  Prepare (or draft) a command such as this, but replace "LastPart" with the last portion of the directory name of the new directory created from step #2 as displayed in step #3:  sudo mv sonar-scanner-LastPart sonar-scanner
Run this command once you prepare it.

5.  Go to /opt/sonar-scanner/conf

6.  Modify the sonar-scanner.properties file.  Uncomment out this line and replace "localhost" with the hostname of the server:

#sonar.host.url=http://localhost:9000

Replace 9000 with the port number you used if you used a non-default port number when you configured the sonar.properties file.

7.  In the /opt/sonar-scanner/conf directory, create a sonar-project.properties file.  Place the following content in it:

# "Contint" must be unique in a given SonarQube instance
sonar.projectKey=Contint
# "Contint" is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=Contint
sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=contint

8.  In the /opt/sonar-scanner/conf/ directory create a new directory called contint.  (If you want to have you sonar-scanner command look in a different directory for source code files to analyze, do not create the contint directory.  Modify the sonar-project.properties file so the stanza sonar.sources= stanza points to the location of your choice.)

Create a Python file and Java file in this directory.

Here is an example of a Java program called hello.java:

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World");
    }

}

Here is an example of a Python program called hello.py:

var = 'hello'
print var

9.  Run this command: sudo /opt/sonar-scanner/bin/sonar-scanner -X

10.  Open the web UI for SonarQube and log in.  If you are not using a web server the URL is often http://x.x.x.x:9000/sonar where x.x.x.x is the external IP address of the server.  The default credentials are usually admin / admin.  If you are using a web server (e.g., Apache web server), just go to http://x.x.x.x where x.x.x.x is the external IP address of the server.

11.  Go to "Projects" in the web UI.  You should see one called "Contint" if you followed the above directions (because of step #7).  Click on the hyperlink.

12.  Click on the directory called "contint" as it should be named as such because of step #8.  

13.  You should see the .java and .py files in the GUI now; these files were created in step #8.  You can click on "Issues" near the top to see more about what SonarQube has found with the code.  If you used the examples above, the hello.py file should cause a message about the print statement.  A preferred way of using the print statement is to put parentheses around the variable like this:

print (var)

14.  You are now done.

Leave a comment

Your email address will not be published. Required fields are marked *