Setting Up SonarQube on an Alibaba Cloud VM Instance

SonarQube is a powerful tool for continuous inspection of code quality, providing detailed reports on bugs, vulnerabilities, and code smells. Setting it up on an Alibaba Cloud VM instance involves several steps, including preparing the environment, installing necessary software, and configuring SonarQube. 

Follow this guide to get started.

Prerequisites

  • Alibaba Cloud Account: Ensure you have an active Alibaba Cloud account.
  • VM Instance: Create a VM instance with at least 2 vCPUs and 4 GB of RAM.
  • Operating System: This guide uses Ubuntu 20.04 LTS.

Step 1: Create and Configure the VM Instance

  • Log in to Alibaba Cloud:
    • Go to the Alibaba Cloud Console.
    • Navigate to Elastic Compute Service (ECS).
  • Create a New Instance:
    • Select Instances & Images > Instances.
    • Click Create Instance.
    • Choose Pay-As-You-Go.
    • Select Ubuntu 20.04 LTS as the operating system.
    • Configure the instance with at least 2 vCPUs and 4 GB of RAM.
    • Define the disk size (at least 20 GB recommended).
  • Access the Instance:
    • Once the instance is running, connect to it via SSH:
ssh root@<your_instance_ip>

Step 2: Install Java

SonarQube requires Java 11. Install it using the following commands:

sudo apt update
sudo apt install openjdk-11-jdk -y
java -version

Step 3: Install and Configure PostgreSQL

SonarQube uses a database to store its data. PostgreSQL is a recommended choice.

  • Install PostgreSQL:
sudo apt install postgresql postgresql-contrib -y

  • Configure PostgreSQL:
    • Switch to the PostgreSQL user:
sudo -i -u postgres

    • Create a new database user for SonarQube:
createuser sonar

    • Create a new database
createdb sonarqube -O sonar
    • Set a password for the SonarQube user:
psql
ALTER USER sonar WITH ENCRYPTED PASSWORD 'your_password';
\q

    • Exit the PostgreSQL user:
exit

Step 4: Install SonarQube

  • Download SonarQube:
cd /opt
sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.8.zip
sudo unzip sonarqube-9.8.zip
sudo mv sonarqube-9.8 sonarqube

  • Configure SonarQube:
    • Edit the SonarQube configuration file:
sudo nano /opt/sonarqube/conf/sonar.properties

    • Set the database connection details:
sonar.jdbc.username=sonar
sonar.jdbc.password=your_password
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube

Step 5: Start SonarQube

  • Create a Systemd Service:
sudo nano /etc/systemd/system/sonarqube.service

    • Add the following content:
[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=root
Group=root
Restart=always
LimitNOFILE=65536
LimitNPROC=4096

[Install]
WantedBy=multi-user.target

  • Reload Systemd and Start SonarQube:
sudo systemctl daemon-reload
sudo systemctl start sonarqube
sudo systemctl enable sonarqube

Step 6: Access SonarQube

  • Open SonarQube in a Browser:
    • Navigate to http://<your_instance_ip>:9000.
    • Log in with the default credentials (admin/admin).
  • Change the Default Password:
    • After logging in, change the default password for security reasons.

Conclusion

You have successfully set up SonarQube on an Alibaba Cloud VM instance. You can now start analyzing your code and improving its quality. For further customization and advanced configurations, refer to the official SonarQube documentation.

Feel free to ask if you have any questions or need further assistance!

Checkout:

Comments

Popular posts from this blog

How to update build number in Azure DevOps pipeline?

How to get latest build ID from Azure DevOps pipeline?

How to install AWS System Manager (SSM) Agent on windows using PowerShell?