Migrating from GitHub to a Self-Hosted GitLab CE on Ubuntu 18.04

Following Microsoft’s acquisition of GitHub, a significant number of developers have opted to relocate their repositories to privately managed alternatives. Among these, GitLab Community Edition (CE) stands out as the preferred platform.

GitLab CE offers extensive features and deployment flexibility. While there are several setup options available, this guide will concentrate exclusively on the officially endorsed method: installing the Omnibus package.

Requirements Before You Begin

  • An untouched Ubuntu 18.04 LTS x64 instance with a minimum of 4GB RAM. For environments supporting up to 100 users, at least 8GB RAM is advised. Assume the server’s IPv4 address is 203.0.113.1.
  • Access to a user account with sudo privileges.
  • A domain name such as gitlab.example.com correctly directed to the aforementioned server.

Important: When using your own server, substitute every placeholder example in this guide with your actual values.

 

Step 1: Initial Setup for Hosting GitLab CE

Start by launching an SSH session and accessing your Ubuntu 18.04 LTS x64 server as a user with sudo rights.

Creating a Swap Partition and Adjusting Swappiness

If you’re installing GitLab CE version 11.x on a server equipped with 4GB of RAM, it’s essential to create a 4GB swap partition to maintain stable performance:

sudo dd if=/dev/zero of=/swapfile count=4096 bs=1M
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile   none    swap    sw    0   0' | sudo tee -a /etc/fstab
free -m

Note: The swap partition size may vary depending on your server’s specifications.

To enhance system performance, reduce the kernel’s swappiness value to 10 using the commands below:

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
cat /proc/sys/vm/swappiness

The final command should return a value of 10.

Configuring Hostname and Fully Qualified Domain Name (FQDN)

Assign the hostname gitlab and set gitlab.example.com as the FQDN using the following commands:

sudo hostnamectl set-hostname gitlab
sudo sed -i "1 i\203.0.113.1 gitlab.example.com gitlab" /etc/hosts

Validate the setup with these commands:

Setting Up Firewall Access

Allow incoming connections for SSH, HTTP, and HTTPS:

sudo ufw allow OpenSSH
sudo ufw allow http
sudo ufw allow https
sudo ufw enable

Updating the Operating System

Ensure your server packages are up to date by running:

sudo apt update
sudo apt upgrade -y && sudo shutdown -r now

During the upgrade, if you’re prompted about changes to the GRUB configuration file, select the option to use the package maintainer’s version by using the arrow keys and pressing Enter.

After the system reboots, log back in with the same sudo account to proceed with the installation.

 

Step 2: Installing Necessary Dependencies

Before you proceed with installing GitLab CE, certain packages must be present on your system. Use the command below to install them:

sudo apt install -y curl openssh-server ca-certificates

If you prefer to send notification emails via Postfix, install it using the following command:

sudo apt install -y postfix

During the Postfix setup process, you might encounter a configuration interface. Follow these steps:

  • Press Tab to select the <OK> option on the first screen and hit Enter.
  • Choose Internet Site and press Enter.
  • When prompted for the mail name, enter your server’s full domain name, gitlab.example.com, then press Enter.
  • If any additional screens appear, accept the defaults by pressing Enter.

Once installed, enable and start the Postfix service with the following:

sudo systemctl enable postfix.service
sudo systemctl start postfix.service

Then, adjust your firewall settings to allow Postfix traffic:

sudo ufw allow Postfix
sudo ufw allow 'Postfix SMTPS'
sudo ufw allow 'Postfix Submission'

After installing Postfix, customize its main configuration file located at /etc/postfix/main.cf to match your server’s specific setup.

Alternative Option: If you intend to use another email delivery method, you may skip installing Postfix. Instead, configure GitLab CE later to use an external SMTP service.

 

Step 3: Configure the GitLab APT Repository and Install GitLab CE

To begin, add the GitLab Community Edition (CE) APT repository to your system by running the following command:

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

Next, install GitLab CE version 11.x by executing:

sudo EXTERNAL_URL="http://gitlab.example.com" apt install -y gitlab-ce

Please note that this process may take some time to complete.

Once installation finishes, open your preferred web browser and navigate to http://gitlab.example.com. You’ll be asked to set a new password to finalize the setup.

Afterward, you can access the admin panel with the following login credentials:

  • Username: root
  • Password: <your-new-password>

 

Step 4: Secure GitLab with HTTPS via Let’s Encrypt SSL Certificate

At this point, GitLab CE 11.x is successfully set up on your server and can be accessed using the unsecured HTTP protocol. To safeguard traffic, it is advisable to enable HTTPS by configuring a Let’s Encrypt SSL certificate.

Begin by opening GitLab’s main configuration file using the vi editor:

sudo vi /etc/gitlab/gitlab.rb

Locate the following lines in the configuration file:

external_url 'http://gitlab.example.com'
# letsencrypt['contact_emails'] = [] # This should be an array of email addresses to add as contacts

Update them to enable HTTPS and specify a contact email:

external_url 'https://gitlab.example.com'
letsencrypt['contact_emails'] = ['admin@example.com']

Save the changes and exit the editor by typing:

Apply the new configuration by running:

sudo gitlab-ctl reconfigure

This step might take some time to finish.

Once reconfiguration completes, all connections to the GitLab site will be automatically redirected to use the HTTPS protocol.

Note: After switching to HTTPS, you may encounter a GitLab 422 error caused by outdated cookies. Simply clearing your browser cookies will resolve the issue.

 

Conclusion

By following this guide, you’ve successfully installed and configured GitLab Community Edition 11.x on an Ubuntu 18.04 LTS server. Starting from essential system preparation and dependency installation, to setting up GitLab via the official repository and securing access with HTTPS, your GitLab instance is now ready for use in a secure and reliable environment.

Remember to replace all placeholder values, such as domain names and email addresses, with those that apply to your specific setup. Whether you’re managing private projects or collaborating with a team, this self-hosted GitLab installation provides a robust and flexible foundation for your version control and DevOps workflows.