VOOZH about

URL: https://thenewstack.io/how-to-analyze-code-and-find-vulnerabilities-with-sonarqube/

⇱ How to Analyze Code and Find Vulnerabilities with SonarQube - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2021-04-16 10:09:09
How to Analyze Code and Find Vulnerabilities with SonarQube
tutorial,
Security / Software Development

How to Analyze Code and Find Vulnerabilities with SonarQube

Here's a tutorial to analyze code with the SonarCube Security Analysis Platform.
Apr 16th, 2021 10:09am by Jack Wallen
👁 Featued image for: How to Analyze Code and Find Vulnerabilities with SonarQube

SonarQube is a web-based tool that can help developers produce code free from security issues, bugs, vulnerabilities, smells, and general issues. If you’re working on a small project, that might be an easy feat. You could carefully work through your code to find any issues. But when you’re working on a larger project (or numerous smaller projects), you probably don’t have time to comb through every line of code you’ve written.

Back in February, I wrote a piece on installing the SonarQube code analysis platform. This time around, I want to show you how to use that tool, so you can trust the code you’re working with (be it written by you or someone else).

Although you’ve installed a very nice web-based tool, using Sonarqube isn’t nearly as straightforward as you might think. If you dive into the documentation, you might find it to be less than enlightening.

Fear not, I’m going to walk you through the process of scanning the tried and true Hello, World! application (written in Java) with Sonarqube. And because our original installation was on Ubuntu Server 20.04, I’ll be sticking with that platform. If you’re using Sonarqube on a different OS, you’ll need to make the necessary adjustments.

Are you ready?

Let’s do this.

Installing Sonar-scanner

This is where most users would get lost. Before you do anything with Sonarqube, you have to have the sonar-scanner application installed on the machine housing your project. I’m going to make this even easier and install it on the same server hosting Sonarqube. Here’s how you’d do that.

Log into the server hosting Sonarqube and install a few dependencies with the command:

sudo apt-get update && sudo apt-get install unzip wget nodejs -y

Once those dependencies are installed, create a new directory with the command:

mkdir sonarqube

Change into that directory with the command:

cd sonarqube

Download the sonar-scan file:

wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.2.0.1873-linux.zip

Unzip the downloaded file:

unzip sonar-scanner-cli-4.2.0.1873-linux.zip

Finally, move the newly-created folder with the command:

sudo mv sonar-scanner-4.2.0.1873-linux /opt/sonar-scanner

Next, we need to create a sonar-scan configuration file with the command:

sudo nano /opt/sonar-scanner/conf/sonar-scanner.properties

In that file, paste the following:

sonar.host.url=http://SERVER:9000
sonar.sourceEncoding=UTF-8

Where SERVER is the IP address of the hosting server.

Save and close the file.

Now we’ll create another configuration file, one that will set the necessary $PATH variables. Issue the command:

sudo nano /etc/profile.d/sonar-scanner.sh

In that file, paste the following:

#/bin/bash
export PATH="$PATH:/opt/sonar-scanner/bin"

Save and close the file.

Add sonar-scanner to your path with the command:

source /etc/profile.d/sonar-scanner.sh

Verify sonar-scanner is working with the command:

sonar-scanner -v

You should see the version numbers of a few tools. Success! You’re ready to run your first scan.

How to Scan Your Code

Let’s create a Hello, World! application example. Create a new directory with the command:

mkdir java

Change into that folder with the command:

cd java

Create the code file with the command:

nano helloworld.java

In that file, paste the following:

// Your Hello, World! java application

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Save and close the file.

Now, go back to the Sonarqube web interface and create a new project (Figure 1).

👁 Image

Figure 1: Click Create new project to begin the process.

In the resulting window (Figure 2), give the new project a name for both the key and the display.

👁 Image

Figure 2: Naming your new project in Sonarqube.

In the next window (Figure 3), you must generate a token for the project. Give the token a name and click Generate.

👁 Image

Figure 3: Generating a token for the new project.

You will then have to give the token yet another name and click Generate. This will display the token for you. Copy and save that token (as you will need it for later scans).

Click Continue to move on to the next step. In this window (Figure 4), select the build technology for the project (we’ll select Other).

👁 Image

Figure 4: Selecting the build technology for your project.

You will then be prompted for the OS you’re using for the scan. In our case, we’ll select Linux. Once you’ve made your selection, you’ll be presented with the command to be run on the machine with the sonar-scanner command (Figure 5). Move back to the terminal window and paste that command into the window.

👁 Image

Figure 5: Sonarqube presents the command you use for the scan.

Run the scan from within your project directory and it will do its thing. After a bit (depending on how large your project is) it will finish and the results of the scan will appear in the Sonarqube web GUI (Figure 6).

👁 Image

Figure 6: The results of our scan show a pretty clean project.

Understand, this was a simple Hello, World! example. If your project is larger, it will take considerably longer to scan and your results might not come up as production-ready. So go through the Sonarqube report and address any issues it reports.

This is a great way to make sure your code is as clean and issue-free as possible. Don’t depend on yourself to take on this task alone. With just a few extra steps, you can empower yourself with a platform that can do the job faster and more reliably.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Sonar.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.