← DEVOPSVĂN LONG
ISSUE 01 — WINTER 2023
Biểu tượng GitLab màu cam được cài vào máy chủ Ubuntu trên nền tối
COVER STORY — DEVOPS

GitlabPart 1: Installing GitLab on Ubuntu 22.04

VĂN LONG·22.01.2023·2 min
SCROLL TO READ ↓
WORDS & PHOTOS — VĂN LONGDEVOPS22.01.2023 · 2 min

A step-by-step guide to installing GitLab CE on Ubuntu 22.04 using the Linux Package (Omnibus), from preparing the domain, SSL and SMTP to logging in as root and post-installation configuration.

CHAPTERI

Prerequisites

Before starting the installation, have the following ready so that installing and later running GitLab goes smoothly:

  • A domain pointing to the gitlab-server's public IP: for example gitlab.example.com, with an A record pointing to the server's public IP. This domain is used as the external_url during installation.
  • An external SMTP/API service for sending mail: GitLab needs to send email for tasks such as account confirmation, password resets, pipeline notifications, merge requests... You can use SMTP from Gmail, Amazon SES, SendGrid, Mailgun...
  • A domain for the registry: if you use the GitLab Container Registry, it's best to have a dedicated domain, for example registry.example.com, also pointing to the gitlab-server's IP.
  • SSL (if available): you can use your company's existing SSL certificate, or let GitLab obtain a free certificate from Let's Encrypt automatically (this requires the domain to be pointed correctly and ports 80 and 443 to be open to the Internet).

As for hardware, GitLab recommends that the server have at least 4 CPU cores and 4 GB RAM (enough for about 20 users). In practice, 8 GB RAM or more is more comfortable, especially if you also enable the Registry and CI/CD.

CHAPTERII

Installation methods

GitLab supports quite a few installation methods, depending on your infrastructure and use case:

  • Docker/Docker Compose: runs GitLab as a container, convenient for labs, testing, or when your infrastructure is already standardized on containers.
  • Linux Package (Omnibus): the method GitLab recommends for most cases. A single package bundles all the components (Nginx, PostgreSQL, Redis, Puma, Sidekiq, Gitaly...), making it easy to install and upgrade.
  • Helm chart (k8s): deploys GitLab on Kubernetes, suited to large systems that need to scale individual components.
  • GitLab Operator (k8s): manages the GitLab lifecycle on Kubernetes/OpenShift through an Operator.
  • Build from source code: building from source yourself is the most flexible option, but also the most complex and maintenance-heavy. Only use it if you genuinely have special requirements.

In this post I'll use the Linux Package (Omnibus) method because it's simple, stable, and fits most self-hosting needs.

CHAPTERIII

Installing GitLab CE Server via Package (Omnibus Method)

Step 1: Update the system

First, refresh the package list and upgrade the system:

Bash
1sudo apt update && sudo apt upgrade -y

Step 2: Install dependencies

Install the required packages:

Bash
1sudo apt install -y curl openssh-server ca-certificates tzdata perl

If the server has a firewall (UFW) enabled, open the ports needed for SSH, HTTP and HTTPS:

Bash
1sudo ufw allow OpenSSH
2sudo ufw allow http
3sudo ufw allow https

Step 3: Install Postfix (optional)

If you want to send mail directly from the server, you can install Postfix:

Bash
1sudo apt install -y postfix

During installation, choose Internet Site and enter the server's domain. However, as mentioned in the Prerequisites section, I recommend using External SMTP for reliability and to avoid being flagged as spam. If you use External SMTP, you can skip this step and configure SMTP after installation.

Step 4: Add the GitLab package repository

Run GitLab's script to add the official repository to the system:

Bash
1curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash

Step 5: Install GitLab CE

When installing, set the EXTERNAL_URL variable to the domain you prepared. If you use https://, GitLab will automatically request an SSL certificate from Let's Encrypt:

Bash
1sudo EXTERNAL_URL="https://gitlab.example.com" apt install gitlab-ce
!
The domain and ports must be ready beforehandFor Let's Encrypt to issue the certificate successfully, the domain must point to the server's IP and ports 80 and 443 must be reachable from the Internet. Otherwise, the reconfigure process will fail at the certificate request step.

You can specify a particular version if needed. List the versions available in the repository with apt-cache madison gitlab-ce, then install it like this:

Bash
1sudo EXTERNAL_URL="https://gitlab.example.com" apt install gitlab-ce=16.0.0-ce.0
Note — Thay 17.0.0-ce.0 bằng phiên bản bạn muốn cài

Installation takes a few minutes depending on the server's specs. When it finishes, GitLab automatically runs gitlab-ctl reconfigure to initialize the services.

Step 6: Get the root password and log in

After installation, GitLab generates a random password for the root account and stores it in the file:

Bash
1sudo cat /etc/gitlab/initial_root_password
‼
The password file is deleted after 24 hoursThis file is automatically deleted after 24 hours (on the first reconfigure run after 24h). You should log in and change the root password right away.

Go to https://gitlab.example.com, log in as root with the password you just retrieved, then go to Edit profile → Password to change it.

Step 7: Check the status of the services

Check that all GitLab components are up and running:

Bash
1sudo gitlab-ctl status

Some commonly used commands for day-to-day operations:

  • sudo gitlab-ctl reconfigure: reapplies the configuration after editing /etc/gitlab/gitlab.rb.
  • sudo gitlab-ctl restart: restarts all services.
  • sudo gitlab-ctl tail: view real-time logs of the services.
  • sudo gitlab-rake gitlab:check SANITIZE=true: run an overall check of the GitLab configuration.

Step 8: Post-installation configuration

All GitLab Omnibus configuration lives in the /etc/gitlab/gitlab.rb file. After every edit, remember to run sudo gitlab-ctl reconfigure.

Configuring External SMTP, for example with Gmail:

Ruby/etc/gitlab/gitlab.rb
1gitlab_rails['smtp_enable'] = true
2gitlab_rails['smtp_address'] = "smtp.gmail.com"
3gitlab_rails['smtp_port'] = 587
4gitlab_rails['smtp_domain'] = "smtp.gmail.com"
5gitlab_rails['smtp_user_name'] = "your.email@gmail.com"
6gitlab_rails['smtp_password'] = "your-app-password"
7gitlab_rails['smtp_authentication'] = "login"
8gitlab_rails['smtp_enable_starttls_auto'] = true
9gitlab_rails['smtp_tls'] = false
10gitlab_rails['smtp_openssl_verify_mode'] = 'peer'
11
12gitlab_rails['gitlab_email_from'] = "your.email@gmail.com"
13gitlab_rails['gitlab_email_reply_to'] = "your.email@gmail.com"
i
Use an App Password for GmailGmail doesn't allow SMTP login with your regular password. Enable 2-Step Verification on the account and create an App Password to use for smtp_password.

Apply the configuration:

Bash
1sudo gitlab-ctl reconfigure

Then send a test email from the Rails console to verify:

Bash
1sudo gitlab-rails console
2Notify.test_email('you@example.com', 'GitLab test', 'Hello from GitLab').deliver_now
ENDWords & photos: Văn Long · DEVOPS
Văn Long
WRITTEN BYVăn LongPlatform Engineer · Quảng Ninh · Viết từ 2017

Mình làm hệ thống phân tán ban ngày và đi bụi vào cuối tuần. Blog này là nơi ghi lại cả hai — đơn giản là chia sẻ những gì mình học được.

DISCUSSION

Comments (0)

R
Please be kind and respectful 🙏
New posts, once a month
One engineering post, one trip. No spam, unsubscribe anytime.