GitLab on Linux: Simplified Installation and Configuration

GitLab on Linux: Simplified Installation and Configuration

What is GitLab?

GitLab, an open-source application, serves as a primary platform for hosting Git repositories and offers additional development-oriented functionalities such as issue tracking. This versatile tool is intended for self-hosting on your infrastructure and offers adaptability, whether you need it as an internal repository hub for your development team, a public interface for user interactions, or a platform for contributors to host their own projects.

Advantage of GitLab

GitLab offers an all-in-one solution for software development with advantages like built-in Git repository hosting, issue tracking, CI/CD pipelines, container registry, and code review. It's highly customizable, secure, and scalable, catering to small teams and large enterprises. GitLab comes in both free Community and paid Enterprise editions, and it integrates seamlessly with other tools, streamlining the development process.

Prerequisites

For this tutorial, make sure you have the following:

  • This guide is designed for Centos, RHEL, Fedora, Amazon Linux 2, and Ubuntu 20.04. Here I am using Amazon Linux 2 and Ubuntu 20.04

GitLab hardware requirements :

  • 4 cores CPU and 4GB memory But here I am using 2 cores CPU and 4GB memory.

Let's Start.

Step 1 — Installing the Dependencies

To install GitLab, it's crucial to install the necessary software it relies on during both the installation and ongoing operation.

Start by updating the local package index.

#For Amazon Linux 2/Centos/RHEL/Fedora
Sudo yum update
#For Ubuntu
sudo apt update

Next, you can install the dependencies by executing the following command:

#For Amazon Linux 2/Centos/RHEL/Fedora
sudo yum install ca-certificates curl openssh-server postfix tzdata perl
#For Ubuntu
sudo apt install ca-certificates curl openssh-server postfix tzdata perl

For the postfix installation, select Internet Site when prompted. On the next screen, enter your server’s domain name or provide any name to configure how the system will send mail.

Now, you’re ready to install GitLab.

Step 2 — Installing GitLab

Now that you have the necessary dependencies installed, you can proceed to install GitLab. This installation process utilizes a script that configures your system with the GitLab repositories.

First, move into the /tmp directory:

cd /tmp

Then download the installation script:

#For Amazon Linux 2/Centos/RHEL/Fedora
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
#For Ubuntu
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash

Now, run the installer:

#For Amazon Linux 2/Centos/RHEL/Fedora. you don't need to use this Command.
#For Ubuntu
sudo bash /tmp/script.deb.sh

This operation installs the essential components on your system and may require some time to finish.

#For Amazon Linux 2/Centos/RHEL/Fedora
sudo yum install gitlab-ce
#For Ubuntu
sudo apt install gitlab-ce

Step 3 — Added Firewall Rules

To configure GitLab, it's essential to confirm that your firewall rules are sufficiently permissive to enable web traffic.

Check the present status of your active firewall by executing:

#For Amazon Linux 2/Centos/RHEL/Fedora
sudo systemctl status firewalld.service
#For Ubuntu
sudo ufw status

Ensure that you permit the following ports; otherwise, the functionality will be impaired.

#For Amazon Linux 2/Centos/RHEL/Fedora
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
#For Ubuntu
sudo ufw allow http
sudo ufw allow https
sudo ufw allow OpenSSH
sudo ufw status

Step 4 — Edit the GitLab Configuration File

Before you can use the application, update the configuration file and run a reconfiguration command. First, open GitLab’s configuration file.

sudo vim /etc/gitlab/gitlab.rb

Search for the external_url configuration line. Update it to match your domain or IP Address.

/etc/gitlab/gitlab.rb

...
##! Note: During installation/upgrades, the value of the environment variable
##! EXTERNAL_URL will be used to populate/replace this value.
##! On AWS EC2 instances, we also attempt to fetch the public hostname/IP
##! address from AWS. For more details, see:
##! https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
external_url 'http://65.2.129.222' #Using your Domain/IP Address here.

## Roles for multi-instance GitLab
...

Next, find the letsencrypt['contact_emails'] setting. It’s recommended to uncomment and fill this out to inform yourself of any issues that may occur:

/etc/gitlab/gitlab.rb

# Let's Encrypt integration
################################################################################
# letsencrypt['enable'] = nil
letsencrypt['contact_emails'] = [parvaze@gmail.com] # This should be an array of email addresses to add as contacts
# letsencrypt['group'] = 'root'
# letsencrypt['key_size'] = 2048
# letsencrypt['owner'] = 'root'
# letsencrypt['wwwroot'] = '/var/opt/gitlab/nginx/www'
# See http://docs.gitlab.com/omnibus/settings/ssl.html#automatic-renewal for more on these sesttings
# letsencrypt['auto_renew'] = true
# letsencrypt['auto_renew_hour'] = 0
# letsencrypt['auto_renew_minute'] = nil # Should be a number or cron expression, if specified.
# letsencrypt['auto_renew_day_of_month'] = "*/4"
# letsencrypt['auto_renew_log_directory'] = '/var/log/gitlab/lets-encrypt'

Once you’re done making changes, save and close the file.

Now run the following command to reconfigure GitLab:

sudo gitlab-ctl reconfigure

This will initiate GitLab using the server information it can automatically detect. It's a fully automated process, requiring no manual prompts or input.

Step 5 — Configuring Initial Settings via the Web Interface

Once GitLab is up and running, you can carry out the initial configuration of the application via the web interface.

Step 6 - Logging and Check

Visit the domain name or IP address of your GitLab server in your web browser:

http://your_domain_or_your_IP_Address

During your initial visit, you will encounter a login page.

GitLab generates an initial secure password for you. It is stored in a folder that you can access as an administrative sudo user. Remember this file will be automatically deleted in the first reconfigure run after 24 hours.

sudo cat /etc/gitlab/initial_root_password

/etc/gitlab/initial_root_password

# WARNING: This value is valid only in the following conditions
#          1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
#          2. Password hasn't been changed manually, either via UI or via command line.
#
#          If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.

Password: xn0LmFjaA3JKgZH3gRsyRNDPL0mlLIhU0Slb/lRPhzk= 

# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.

Back on the login page, enter the following:

  • Username: root

  • Password: [the password listed on /etc/gitlab/initial_root_password]

Input these values into the respective fields, then click the "Sign in" button. This action will log you into the application and redirect you to a landing page where you can start adding projects.

You can now fine-tune your GitLab instance.

Update Your Password

After logging in, one of your initial tasks should be to update your password. To do this, simply click on the icon located at the upper-right corner of the navigation bar, and then choose "Edit Profile."

You will be redirected to the login screen with a notification confirming the password change. Simply enter your new password to log back into your GitLab instance and explore all the necessary options.

Congratulations, you've successfully set up a functional GitLab instance on your server. You can now start importing or creating new projects and configuring the appropriate team access levels. Keep in mind that GitLab frequently introduces new features and updates, so make sure to visit the project's homepage to stay informed about any enhancements or important announcements.