Introduction:
SonarQube is a powerful tool for continuous inspection of code quality. In this step-by-step guide, we will walk you through the process of setting up SonarQube 9.9.2 LTS on an Amazon EC2 instance running Ubuntu. By the end of this tutorial, you will have a fully functional SonarQube server that you can use to analyze the quality of your code.
Prerequisites:
Before you begin, make sure you have the following prerequisites in place:
An AWS account with an active EC2 instance.
Basic knowledge of the Linux command line.
Access to the internet from your EC2 instance.
Step 1: Create and Connect to an EC2 Instance First, create and connect to an EC2 instance. If you're not familiar with how to create and connect to an EC2 instance, you can refer to "How to Create and Connect to an EC2 Instance," for detailed step-by-step instructions. Once you have your EC2 instance ready, you can continue with the installation of SonarQube.
Step 2: Update Ubuntu
Once you are connected to your EC2 instance, it's essential to keep your system up-to-date. Run the following commands to update the package list and upgrade installed packages:
sudo apt update
sudo apt upgrade -y
Step 3: Install Java 17
SonarQube requires Java 17. Install it using the following command:
sudo apt install openjdk-17-jdk -y
Step 4: Download SonarQube 9.9.2 LTS
Download SonarQube 9.9.2 LTS and place it in the /opt
directory. You can use the wget
command to download the package:
sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.9.2.77730.zip -P /opt
Step 5: Add a User for SonarQube
Create a user named "sonar" that will be used to run the SonarQube service:
sudo useradd sonar
Step 6: Install Unzip
If unzip is not already installed, you can install it with the following command:
sudo apt install unzip -y
Step 7: Rename the Extracted Directory
Unzip the SonarQube package and rename the extracted directory to "sonar":
sudo unzip /opt/sonarqube-9.9.2.77730.zip -d /opt
sudo mv /opt/sonarqube-9.9.2.77730 /opt/sonar
Step 8: Set Permissions for the Sonar Directory
Give the "sonar" user ownership of the SonarQube directory:
sudo chown -R sonar:sonar /opt/sonar
Step 9: Add Sonar User to the sudoers File
To allow the "sonar" user to execute commands without a password prompt, add the following line to the sudoers file:
sudo vi /etc/sudoers
Add the following line under the "User privilege specification" section:
sonar ALL=(ALL:ALL) NOPASSWD:ALL
Save and exit the visudo editor.
Step 10: Write a Service for SonarQube
Create a systemd service unit file for SonarQube. Create a file named /etc/systemd/system/sonar.service
and add the following content:
[Unit]
Description=SonarQube service
After=network.target
[Service]
Type=forking
User=sonar
ExecStart=/opt/sonar/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonar/bin/linux-x86-64/sonar.sh stop
Restart=always
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
Step 11: Start the SonarQube Service
Start the SonarQube service using the following commands:
sudo systemctl start sonar
sudo systemctl enable sonar
Step 12: Access SonarQube with the Public IP
You can access your SonarQube server by opening a web browser and navigating to http://your-public-ip:9000
. Replace your-public-ip
with the actual public IP address of your EC2 instance.
Step 13: Log in to SonarQube
Log in to SonarQube using the default username and Password "admin".
Congratulations! You have successfully set up SonarQube 9.9.2 LTS on your EC2 instance. You can now use it to analyze the quality of your code and improve your software development process.