Install GitLab on Ubuntu 20.04 | Best DevOps Platform

Posted on

Install GitLab on Ubuntu 20.04 | Best DevOps Platform

GitLab stands out as a comprehensive DevOps platform, offering much more than just remote access to Git repositories. It’s a complete solution for managing the entire software development lifecycle, incorporating features like code sharing management, bug tracking, wiki spaces, and a suite of tools designed to foster collaborative coding practices.

This guide provides a detailed walkthrough of How To Install GitLab on Ubuntu 20.04. The instructions below, crafted by the Orcacore team, will guide you through the process of setting up GitLab on your Ubuntu 20.04 server.

Steps To Install and Configure Install GitLab on Ubuntu 20.04

Before proceeding, ensure you have the following prerequisites:

  • A server running Ubuntu 20.04.
  • A non-root user with sudo privileges. Refer to our guide on Initial Server Setup with Ubuntu 20.04 for assistance.
  • A domain name pointed to your server’s IP address.

1. Install GitLab on Ubuntu 20.04

First, refresh your local package index:

sudo apt update

Next, install the necessary dependencies for GitLab:

sudo apt install ca-certificates curl openssh-server postfix tzdata perl

During the postfix installation, select "Internet Site" when prompted. On the subsequent screen, enter your server’s domain name to configure email sending.

Download GitLab Installation Script on Ubuntu 20.04

Navigate to the /tmp directory:

cd /tmp

Download the GitLab installation script:

curl -LO https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh

Execute the downloaded script:

sudo bash /tmp/script.deb.sh

Upon successful execution, you should see the following output:

**Output**
The repository is setup! You can now install packages.

Now, install the GitLab application:

sudo apt install gitlab-ce
Install GitLab on Ubuntu 20.04

Configure Firewall Rules for Gitlab

Ensure your firewall allows web traffic. We assume you have UFW enabled.

Allow HTTP, HTTPS, and OpenSSH traffic:

# sudo ufw allow http
# sudo ufw allow https
# sudo ufw allow OpenSSH

Reload the firewall to apply the changes:

sudo ufw reload

2. Configure GitLab on Ubuntu 20.04

Edit the GitLab configuration file:

sudo vi /etc/gitlab/gitlab.rb

Locate the external_url configuration line and update it with your domain, changing http to https for automatic redirection to the Let’s Encrypt secured site.

...
external_url 'https://your_domain'
...

Uncomment the letsencrypt['contact_emails'] line and set your email address:

...
letsencrypt['contact_emails'] = ['daniel@example.com']
...

Save and close the file.

Reconfigure Gitlab

Reconfigure GitLab:

sudo gitlab-ctl reconfigure

This process initializes GitLab using your server’s information and configures a Let’s Encrypt certificate. It may take a while to complete.

Upon completion, you should see:

**Output**
gitlab Reconfigured!

3. Access GitLab Application Dashboard

Access the GitLab web interface by navigating to your domain in your web browser:

https://your_domain

You will be presented with the GitLab Community Edition Login page.

Note: GitLab generates an initial secure password, stored in /etc/gitlab/initial_root_password:

sudo vi /etc/gitlab/initial_root_password

You’ll see something like this:

Password: your-initial-root-password

Log in with the username root and the initial password.

Gitlab login screen
Access GitLab Application Dashboard

You will be redirected to the GitLab application dashboard, where you can begin adding projects.

Gitlab dashboard
Access GitLab Application Dashboard

You can now update your password, profile settings, account name, and add an SSH key.

4. Renew Let’s Encrypt Certificates for GitLab on Ubuntu 20.04

GitLab automatically renews Let’s Encrypt certificates after midnight every fourth day.

You can customize the renewal schedule in the GitLab configuration file:

sudo vi /etc/gitlab/gitlab.rb

Uncomment and update the following lines:

letsencrypt['auto_renew'] = true
letsencrypt['auto_renew_hour'] = "12"
letsencrypt['auto_renew_minute'] = "30"
letsencrypt['auto_renew_day_of_month'] = "*/7"
...

This configuration renews the certificates every 7th day at 12:30.

To disable auto-renewal, set letsencrypt['auto_renew'] to false:

...
letsencrypt['auto_renew'] = false
...

Save and close the file.

Conclusion

You have now successfully learned How To Install GitLab on Ubuntu 20.04 and access the GitLab Application Dashboard. GitLab on Ubuntu 20.04 provides robust capabilities for source code management, CI/CD automation, and DevOps collaboration.

You may also find these articles helpful:

Alternative Solutions for Installing GitLab on Ubuntu 20.04

While the above method is a standard approach, alternative methods exist to Install GitLab on Ubuntu 20.04. Here are two such alternatives:

1. Using Docker Compose

Docker Compose allows you to define and manage multi-container Docker applications. This method is particularly useful for isolating GitLab and its dependencies from the host system, ensuring a clean and reproducible installation. It also simplifies upgrades and rollbacks.

Explanation:

  • We will create a docker-compose.yml file that defines the GitLab container, its dependencies (like PostgreSQL and Redis, if not using the built-in ones), volume mappings for persistent data, and network configurations.
  • Docker Compose will then orchestrate the creation and linking of these containers.

Steps:

  1. Install Docker and Docker Compose:

    sudo apt update
    sudo apt install docker.io docker-compose
  2. Create a docker-compose.yml file:

    Create a directory for your GitLab configuration and navigate into it:

    mkdir gitlab-docker
    cd gitlab-docker

    Create a docker-compose.yml file with the following content (adjust versions and configurations as needed):

    version: '3.6'
    services:
      gitlab:
        image: 'gitlab/gitlab-ce:latest'
        restart: always
        hostname: 'gitlab.example.com' # Replace with your domain
        environment:
          GITLAB_OMNIBUS_CONFIG: |
            external_url 'https://gitlab.example.com' # Replace with your domain
            gitlab_rails['gitlab_shell_ssh_port'] = 2224
            letsencrypt['enable'] = true
            letsencrypt['contact_emails'] = ['youremail@example.com'] # Replace with your email
        ports:
          - '80:80'
          - '443:443'
          - '2224:22'
        volumes:
          - './config:/etc/gitlab'
          - './logs:/var/log/gitlab'
          - './data:/var/opt/gitlab'

    Explanation of the docker-compose.yml:

    • version: Specifies the Docker Compose file format version.
    • services: Defines the services that make up the application. In this case, we have one service named gitlab.
    • image: Specifies the Docker image to use for the service. We’re using the official gitlab/gitlab-ce image.
    • restart: Specifies the restart policy for the container. always ensures that the container restarts automatically if it crashes.
    • hostname: Sets the hostname of the container.
    • environment: Defines environment variables to configure the GitLab Omnibus package. GITLAB_OMNIBUS_CONFIG allows you to specify configuration options directly within the docker-compose.yml file. This is where you configure the external_url, SSH port, Let’s Encrypt settings, and other GitLab options.
    • ports: Maps ports from the host machine to the container. Port 80 (HTTP) and 443 (HTTPS) are mapped to the corresponding ports on the container, allowing access to the GitLab web interface. Port 2224 on the host is mapped to port 22 inside the container, which allows SSH access to the GitLab server (you’ll need to configure your SSH client to connect to port 2224 on the host).
    • volumes: Mounts directories from the host machine into the container. This is crucial for persisting GitLab data across container restarts and upgrades. The volumes ensure that your configuration, logs, and data are stored on the host machine and won’t be lost if the container is removed.
  3. Start GitLab with Docker Compose:

    docker-compose up -d

    This command pulls the GitLab image, creates the necessary containers, and starts GitLab in detached mode (-d).

  4. Access GitLab:

    After a few minutes (it takes time for GitLab to initialize), you can access GitLab through your browser using the domain name you configured in the external_url (e.g., https://gitlab.example.com).

  5. Initial Login:

    Follow the instructions in the original article to retrieve the initial root password and log in.

2. Using GitLab’s Official Helm Chart (Kubernetes)

For more complex deployments, especially in cloud environments, deploying GitLab using its official Helm chart on Kubernetes offers scalability, resilience, and centralized management. This requires a running Kubernetes cluster.

Explanation:

  • Helm is a package manager for Kubernetes, allowing you to define, install, and upgrade even the most complex Kubernetes applications.
  • GitLab provides an official Helm chart that simplifies the deployment of GitLab onto a Kubernetes cluster.
  • This approach offers benefits such as automated deployments, scaling, and self-healing capabilities inherent in Kubernetes.

Steps:

  1. Have a Kubernetes Cluster Ready: This assumes you have a Kubernetes cluster running and kubectl configured to interact with it. This can be a local cluster (minikube, kind) or a cloud-based cluster (GKE, EKS, AKS).

  2. Install Helm:

    curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
    chmod 700 get_helm.sh
    ./get_helm.sh
  3. Add the GitLab Helm Repository:

    helm repo add gitlab https://charts.gitlab.io/
    helm repo update
  4. Create a values.yaml file:

    Create a values.yaml file to customize your GitLab deployment. This file will override the default values in the Helm chart. Here’s a minimal example:

    global:
      hosts:
        domain: example.com  # Replace with your domain
        https: true
      ingress:
        configureCertmanager: false # Set to true if using cert-manager
    certmanager:
      install: false # Set to true if you want the chart to install cert-manager
    gitlab:
      gitlab-shell:
        resources:
          requests:
            cpu: 200m
            memory: 256Mi
      gitlab-workhorse:
        resources:
          requests:
            cpu: 200m
            memory: 256Mi
      sidekiq:
        resources:
          requests:
            cpu: 500m
            memory: 512Mi
      webservice:
        resources:
          requests:
            cpu: 500m
            memory: 512Mi
    
    postgresql:
      install: false  # Assumes you have an external PostgreSQL database
    redis:
      install: false    # Assumes you have an external Redis instance
    

    Explanation of values.yaml:

    • global.hosts.domain: Sets the domain name for your GitLab instance.
    • global.hosts.https: Enables HTTPS.
    • global.ingress.configureCertmanager: If set to true, the chart will attempt to configure cert-manager for automatic TLS certificate management.
    • certmanager.install: If set to true, the chart will install cert-manager if it’s not already present in the cluster. cert-manager automates the process of obtaining and renewing TLS certificates from Let’s Encrypt.
    • gitlab.*.resources.requests: Sets resource requests (CPU and memory) for the different GitLab components. Adjust these values based on the expected workload.
    • postgresql.install: Set to false to use an external PostgreSQL database. If set to true, the chart will deploy a PostgreSQL database within the cluster. For production environments, it’s highly recommended to use an external, managed database.
    • redis.install: Set to false to use an external Redis instance. If set to true, the chart will deploy a Redis instance within the cluster. Similar to PostgreSQL, using an external Redis instance is recommended for production.

    Important: This example assumes you have an external PostgreSQL and Redis instance. You’ll need to configure the chart to connect to those instances (see the GitLab Helm chart documentation for details). For simpler deployments, you can set postgresql.install and redis.install to true, but this is not recommended for production. You’ll also need to configure the appropriate DNS records to point your domain to the Kubernetes cluster’s ingress controller.

  5. Install GitLab using Helm:

    helm install gitlab gitlab/gitlab -f values.yaml

    This command installs GitLab into your Kubernetes cluster using the configurations specified in the values.yaml file.

  6. Access GitLab:

    After the installation completes, you can access GitLab through your browser using the domain name you configured (e.g., https://example.com).

  7. Initial Login:

    Follow the instructions provided in the GitLab Helm chart documentation to retrieve the initial root password and log in. The exact method for retrieving the password depends on the specific configuration of the chart.

Both alternative methods offer benefits over the standard package installation, providing increased flexibility, isolation, and scalability. The choice depends on your specific needs and infrastructure. These are different ways to Install GitLab on Ubuntu 20.04.

Leave a Reply

Your email address will not be published. Required fields are marked *