---
title: "Definitive Getting Started Guide for SonarQube on Linux"
description: "Learn how to use SonarQube on Linux to verify both code quality and code security in this ATA Learning tutorial!"
canonical: "https://adamtheautomator.com/sonarqube/"
---

# Definitive Getting Started Guide for SonarQube on Linux

> Learn how to use SonarQube on Linux to verify both code quality and code security in this ATA Learning tutorial!

Source: https://adamtheautomator.com/sonarqube/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Definitive Getting Started Guide for SonarQube on Linux](https://adamtheautomator.com/wp-content/uploads/2022/05/Definitive-Getting-Started-Guide-for-SonarQube-on-Linux.jpg)

# Definitive Getting Started Guide for SonarQube on Linux

[![](https://secure.gravatar.com/avatar/2beb65fca997135120ed98dc6a2e57dcdf1a7d7d2f5ff687b5d91dc7ccd7a6b5?s=192&d=mm&r=g)Sagar](https://adamtheautomator.com/author/shanky-mendiratta/)1 June 20227 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[SonarQube](/tag/sonarqube/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Installing Java Version 11 on Ubuntu](#installing-java-version-11-on-ubuntu)
*   [Installing SonarQube on Ubuntu](#installing-sonarqube-on-ubuntu)
*   [Setting up a Database for SonarQube](#setting-up-a-database-for-sonarqube)
*   [Configuring SonarQube](#configuring-sonarqube)
*   [Accessing SonarQube and Scanning Code](#accessing-sonarqube-and-scanning-code)
*   [Conclusion](#conclusion)

Securing the code you’re writing or pushing into your repository is crucial. But how do you keep your code safe and have no vulnerabilities? Luckily, [SonarQube](https://www.sonarqube.org/) can help with code quality and code security.

SonarQube is an open-source tool that allows you to scan your source code to look for potential bugs, vulnerabilities, and maintainability issues. And in this tutorial, you’ll learn how to install the SonarQube on Ubuntu and verify code quality by testing a sample code.

Empower yourself with SonarQube for code quality analysis and reporting today!

## Prerequisites

This tutorial will be a step-by-step demonstration. To follow along, be sure you have an Ubuntu 14.04.4 LTS Linux machine or greater. This tutorial uses an [Ubuntu](https://adamtheautomator.com/install-ubuntu/) 20.04.4 LTS machine.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

## Installing Java Version 11 on Ubuntu

Before you dive into the installation of SonarQube, you need Java to be present on the system, as SonarQube is based on the Java language. You can use multiple Java implementations to work with Jenkins, but [OpenJDK](https://openjdk.java.net/) is the most popular, which you will install and use in this tutorial. Let’s install Java!

1\. Log in to your Ubuntu machine using your favorite SSH client.

2\. Next, run the [apt update](https://linux.die.net/man/8/apt-get) command below to update your system packages.

```bash
sudo apt update
```

![Updating System Packages](https://adamtheautomator.com/wp-content/uploads/2022/05/image-651.png)

Updating System Packages

3\. Once updated, run the following commands to create a directory named _~/install\_sonarqube\_demo_ and switch to that directory.

```bash
mkdir ~/install_sonarqube_demo
cd ~/install_sonarqube_demo
```

4\. Now, run the [apt install](https://manpages.ubuntu.com/manpages/xenial/man8/apt.8.html) command below to install Java on your machine. The below command installs the Open JDK 11 version.

```bash
# Installing Java Version: Java SE 11 (LTS)
sudo apt install default-jdk 
```

![Installing Java Version: Java SE 11 (LTS)](https://adamtheautomator.com/wp-content/uploads/2022/05/image-652.png)

Installing Java Version: Java SE 11 (LTS)

5\. Finally, run the below command to check the version of installed Java on your machine. Doing so ensures that you’ve successfully installed Java.

```bash
java -version
```

![Checking the Installed Java Version](https://adamtheautomator.com/wp-content/uploads/2022/05/image-653.png)

Checking the Installed Java Version

## Installing SonarQube on Ubuntu

Now that you have Java installed on your Ubuntu machine, you’re ready to install SonarQube on the same machine. Installing SonarQube is similar to installing other packages.

SonarQube works on different operating systems, such as Windows and Linux. But for this demo, you’ll run SonarQube on an Ubuntu server.

1\. Run the following commands to create a dedicated user with a password you’ll use to install SonarQube. You can define your own user, but this tutorial’s choice is sonaruser.

```bash
sudo useradd sonaruser
sudo passwd sonaruser
```

![Adding the SonarQube User and Password ](https://adamtheautomator.com/wp-content/uploads/2022/05/image-654.png)

Adding the SonarQube User and Password

2\. Next, run the wget command below to [download the SonarQube software](https://www.sonarqube.org/success-download-community-edition/) to your system. Your version of SonarQube may be different than the one demonstrated here.

```bash
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.4.0.54424.zip
```

![Downloading the SonarQube Zip file ](https://adamtheautomator.com/wp-content/uploads/2022/05/image-655.png)

Downloading the SonarQube Zip file

Related:[Install Python Wget and Automate your File Downloads](https://adamtheautomator.com/python-wget/)

3\. Run each command below to install the unzip package and unzip the SonarQube zip file on your machine (sonarqube-9.4.0.54424.zip).

```bash
# Install the unzip package
sudo apt -y install unzip
# Unzip the SonarQube zip file
sudo unzip sonarqube-9.4.0.54424.zip
```

![Unzipping the SonarQube Package](https://adamtheautomator.com/wp-content/uploads/2022/05/image-656.png)

Unzipping the SonarQube Package

Related:[Learn How to Zip and Unzip Files in Linux and be a Zip Master](https://adamtheautomator.com/unzip-files-in-linux/)

4\. Lastly, run the [chown](https://linuxize.com/post/linux-chown-command/) command below to make sure the user and the group of the SonarQube directory is the user (sonaruser) you created in step one.

Be sure to change the home directory (/home/ubuntu/) with your own.

```bash
sudo chown -R sonaruser:sonaruser /home/ubuntu/install_sonarqube_demo/sonarqube-9.4.0.54424
```

Related:[A Windows Guy in a Linux World: Users and File Permission](https://adamtheautomator.com/linux-file-permissions/)

## Setting up a Database for SonarQube

You’ve successfully installed SonarQube on your machine, but to configure the SonarQube, you’ll need a database to store the data. How? For this tutorial, you’ll install the Postgres database and create a database for SonarQube.

1\. Run the wget command below to add the Postgres public key (apt-key) to your system.

Adding the public key allows the Postgres site to download the Postgres official repository or any required packages.

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

![Adding the Postgres Public Key to the System](https://adamtheautomator.com/wp-content/uploads/2022/05/image-657.png)

Adding the Postgres Public Key to the System

2\. Next, run the following commands to add the Postgres official repository to your system and update your system.

```bash
echo "deb [arch=amd64] http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list
sudo apt update
```

![Adding the Postgres Official Repository and Updating the System Packages](https://adamtheautomator.com/wp-content/uploads/2022/05/image-658.png)

Adding the Postgres Official Repository and Updating the System Packages

3\. After the update, run each command below to install the Postgres package and verify the installation.

```bash
# Install Postgres
sudo apt install postgresql-13
# Verify Postgres installation
sudo systemctl status postgresql
```

![Installing Postgres Software](https://adamtheautomator.com/wp-content/uploads/2022/05/image-659.png)

Installing Postgres Software

![Verifying Postgres Database Installation](https://adamtheautomator.com/wp-content/uploads/2022/05/image-660.png)

Verifying Postgres Database Installation

Related:[Correct Way of Using Ubuntu systemctl to Control Systemd](https://adamtheautomator.com/ubuntu-systemctl/)

4\. Now, run each command and query to create a database (sonardb) and a user (sonaruser) in the database.

```bash
# Set the Postgres user password (passwd postgres). 
# By default, Postgres runs using the postgres user.
sudo passwd postgres
# Switch to postgres user
su - postgres
# Create a user in the database, which will be used in SonarQube.
createuser sonaruser
# Switch to postgres command-line
psql
# Sets password for the sonaruser
ALTER USER sonaruser WITH ENCRYPTED password 'password';
# Create the database (sonardb)
CREATE DATABASE sonardb OWNER sonaruser;
```

![Creating a Database (sonardb) on Postgres DB ](https://adamtheautomator.com/wp-content/uploads/2022/05/image-661.png)

Creating a Database (sonardb) on Postgres DB

5\. Run the below command or press Ctrl+D to exit from the Postgres command line.

```bash
\q
```

![Exiting Postgres Command-Line](https://adamtheautomator.com/wp-content/uploads/2022/05/image-662.png)

Exiting Postgres Command-Line

6\. Finally, run the command below or press Ctrl+D to logout from the postgres user.

```bash
logout
```

![Logging Out from the postgres User](https://adamtheautomator.com/wp-content/uploads/2022/05/image-663.png)

Logging Out from the postgres User

## Configuring SonarQube

At this point, you’ve created a database to store data. Now, it’s time to create a configuration file with correct database details and run SonarQube as a service.

> _Note that editing configuration files throughout this section requires sudo access._

1\. Create a SonarQube’s configuration file (_/home/ubuntu/install\_sonarqube\_demo/sonarqube-9.4.0.54424/conf/sonar.properties_) in your preferred text editor.

Be sure to change _/home/ubuntu_ with your home directory.

2\. Update the SonarQube with the following, save the changes and close the file:

*   **sonar.jdbc.username** – Set the sonar username (**sonaruser**).
    
*   **sonar.jdbc.password** – Sets the sonaruser’s password (**password**).
    
*   **sonar.jdbc.url** – Sets the database (sonardb) where SonarQube will store data.
    

![Updating Sonar Cube Configuration](https://adamtheautomator.com/wp-content/uploads/2022/05/image-664.png)

Updating Sonar Cube Configuration

3\. Next, open the _/etc/systemd/system/sonar.service_ file in your text editor and add the code below. But be sure to change /home/ubuntu with your own home directory, save the changes and close the editor.

This code creates the SonarQube service, which always runs in the background even when your system restart.

```bash
[Unit]
Description=SonarQube
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/home/ubuntu/install_sonarqube_demo/sonarqube-9.4.0.54424/bin/linux-x86-64/sonar.sh start
ExecStop=/home/ubuntu/install_sonarqube_demo/sonarqube-9.4.0.54424/bin/linux-x86-64/sonar.sh stop
LimitNOFILE=131072
LimitNPROC=8192
User=sonaruser
Group=sonaruser
Restart=on-failure
[Install]
WantedBy=multi-user.target
```

4\. Now, run the below systemctl command to reload the daemon (daemon-reload). This command doesn’t provide output but sets the service you created load in the system and runs in the next system reboot.

```bash
sudo systemctl daemon-reload
```

5\. Run each command below to enable and start the sonar service, and verify the service is running.

```bash
# Enabling the sonar service
sudo systemctl enable sonar.service
# Starting the sonar service
sudo systemctl start sonar
# Verifying the sonar service
sudo systemctl status sonar
```

> _The default operating system limits on mmap counts are too low. This behavior may result in out-of-memory exceptions. If your service stops, run the below command to solve it: sudo sysctl -w vm.max\_map\_count=262144_

![Viewing the SonarQube Service’s Status ](https://adamtheautomator.com/wp-content/uploads/2022/05/image-665.png)

Viewing the SonarQube Service’s Status

## Accessing SonarQube and Scanning Code

SonarQube is now installed, and the service is running. But how do you know that SonarQube is actually running? With the service started, verify the server is correctly running by accessing SonarQube’s default web page on your browser.

1\. Open your favorite web browser and navigate to your IP address followed by port 9000 (i.e., _52.15.72.168:9000_). If all goes well, you’ll see SonarQube’s login page below.

Log in to SonarQube using admin for both the username and password.

![Accessing SonarQube](https://adamtheautomator.com/wp-content/uploads/2022/05/image-666.png)

Accessing SonarQube

2\. Once you log in successfully, enter the old (admin) and new password (anything you like) when prompted, and click Update to change the current password.

After updating the password, you’ll see the SonarQube home page (step three).

![Updating Password](https://adamtheautomator.com/wp-content/uploads/2022/05/image-667.png)

Updating Password

3\. Next, click Manually as you’ll create the project on your Ubuntu machine rather than taking it from any repository.

![Initiating Creating a Project Manually](https://adamtheautomator.com/wp-content/uploads/2022/05/image-668.png)

Initiating Creating a Project Manually

4\. Now, define the project details, as shown below, and click on Set Up. This tutorial uses ATA-Learning for the Project display name and the Project key.

Take note of the project key, as you’ll need it when you run the scan later.

![Specifying the Project Name](https://adamtheautomator.com/wp-content/uploads/2022/05/image-669.png)

Specifying the Project Name

5\. Select the Locally option since you’re creating your project on your local machine.

![Locally option](https://adamtheautomator.com/wp-content/uploads/2022/05/image-670.png)

Locally option

6\. Next, generate a token with the following:

*   Select the **Generate a token option**.
    
*   Enter a name for the token. In this case, name the token the same as your project’s name (**ATA-Learning**) you set in step four.
    
*   Click on Generate to generate a token associated with your project.
    

![Generating Token for ATA-Learning Project](https://adamtheautomator.com/wp-content/uploads/2022/05/image-671.png)

Generating Token for ATA-Learning Project

7\. Note down the generated token, and click on Continue. You’ll need this token for scanning your code or local files later.

![Noting Down the Generated Token](https://adamtheautomator.com/wp-content/uploads/2022/05/image-672.png)

Noting Down the Generated Token

8\. Now, run the following commands to download the [SonarQube Scanner client](https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/) zip file on your machine and unzip the file.

```bash
# Download the SonarQube Scanner client zip
wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.7.0.2747-linux.zip
# Unzip the SonarQube Scanner client zip file
unzip sonar-scanner-cli-4.7.0.2747-linux.zip
```

![Downloading the SonarQube Scanner ](https://adamtheautomator.com/wp-content/uploads/2022/05/image-673.png)

Downloading the SonarQube Scanner

![Unzipping the SonarQube Scanner ](https://adamtheautomator.com/wp-content/uploads/2022/05/image-674.png)

Unzipping the SonarQube Scanner

9\. Run the following command while in the /_home/ubuntu/install\_sonarqube\_demo/sonar-scanner-4.7.0.2747-linux/bin_ directory to run the scanner.

Replace the following accordingly:

*   `ATA-Learning` – Your actual project key.
    
*   `http://54.145.237.10:9000` – The URL you use to access SonarQube.
    
*   `token - The token you noted down in step seven.`
    

```bash
./sonar-scanner  \
>   -Dsonar.projectKey=ATA-Learning \
>   -Dsonar.sources=. \
>   -Dsonar.host.url=http://54.145.237.10:9000 \
>   -Dsonar.login=token
```

![Running the Scan ](https://adamtheautomator.com/wp-content/uploads/2022/05/image-675.png)

Running the Scan

![Viewing the Scan Result](https://adamtheautomator.com/wp-content/uploads/2022/05/image-677-1024x300.png)

Viewing the Scan Result

10\. Finally, hop over to your SonarQube dashboard, click on the Projects tab, and see the first execution (scan) for your project (ATA-Learning) Passed.

![Checking the Scan Result via the SonarQube Dashboard](https://adamtheautomator.com/wp-content/uploads/2022/05/image-676.png)

Checking the Scan Result via the SonarQube Dashboard

## Conclusion

In this tutorial, you’ve learned how to set up a SonarQube and scan the data stored in your project. Scanning lets you catch tricky bugs to prevent a heavy impact on end-users.

SonarQube realizes vulnerabilities that compromise your app and offers cleaner and safer code writing. And since you now have a fully functional SonarQube installed in your machine, what do you plan to scan in your next project?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fsonarqube%2F&text=Definitive%20Getting%20Started%20Guide%20for%20SonarQube%20on%20Linux)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fsonarqube%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fsonarqube%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/publisher/3f95d9c85b2b81d9a96fd378dc47c60a/121705eb357e56699bca0c73e19055d636ddcfdd7418053591d9dbf69af050e2.jpg)

### [Audit Every SMB Share Permission with Get-SmbShareAccess](/audit-smb-share-permissions-powershell/)

Audit SMB share permissions with Get-SmbShareAccess, flag Everyone and Full Control grants, and check the NTFS ACL before you revoke anything.

![](https://adamtheautomator.com/wp-content/uploads/publisher/3f95d9c85b2b813190efe4fbf5ba49f1/01cf2782293526c501009180bd8555b6e73ab2cef8c62a48d76f7970916c3080.jpg)

### [Migrate DHCP Without Losing Leases with Export-DhcpServer](/migrate-dhcp-server-export-dhcpserver/)

Move a DHCP server with Export-DhcpServer and Import-DhcpServer, keeping leases intact, then authorize the new server in Active Directory.

![](https://adamtheautomator.com/wp-content/uploads/publisher/3f95d9c85b2b817aab5dca59f1ca0e14/38378bffb6e067df12a1bd90a3921833213a6e328f7d3baabd1fae698d38ddfd.jpg)

### [Fix Kerberos Clock Skew Logon Failures Using w32tm](/fix-kerberos-clock-skew-w32tm/)

Fix Kerberos clock skew logon failures by checking w32tm sources, pointing the PDC emulator at NTP, and resyncing drifted clients.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
