How to Install GitLab on Ubuntu 22.04 or 20.04

GitLab is a comprehensive DevOps platform offering various tools and features tailored for software development teams. For those aiming to install GitLab on Ubuntu 22.04 Jammy Jellyfish or its older stable release of Ubuntu 20.04 Focal Fossa, this introduction delves into the core attributes that make GitLab a preferred choice for developers and organizations globally.

Key Features of GitLab:

  • Version Control: Efficiently manage and monitor changes to source code and digital assets.
  • Issue Tracking: Seamlessly create, assign, and monitor issues, bugs, and feature requests.
  • CI/CD Integration: Automate the development process, from building and testing to deployment, with GitLab’s integrated CI/CD pipeline.
  • Code Review: Collaborate on code changes, ensuring quality and consistency.
  • Documentation: Utilize the built-in wiki system for creating and managing project documentation.
  • Project Management Tools: Plan and track project progress with milestones, agile boards, and burndown charts.
  • External Integrations: Streamline workflows with tools like Kubernetes, Jenkins, and Slack.
  • Enhanced Security: Implement advanced security measures, including two-factor authentication and LDAP integration.
  • Customization: Tailor your GitLab instance with custom branding, themes, and workflows.
  • Scalability: Designed for growth, GitLab can accommodate many users and projects, making it ideal for large-scale organizations.

With its robust feature set and adaptability, GitLab provides a unified platform that caters to the diverse needs of software development teams. By choosing to deploy GitLab on Ubuntu, you’re equipping your team with a reliable, secure, and efficient platform for project management. The subsequent guide will detail the steps to install and configure GitLab on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa.

Pre-Installation Steps For GitLab on Ubuntu

Step 1: Update Ubuntu Before GitLab Installation

We recommend performing an update to update all the existing packages on your Ubuntu operating system. This gives your system the latest security updates and bug fixes, enhancing its stability and reliability. Use the following command to update your Ubuntu operating system:

sudo apt update && sudo apt upgrade 

Step 2: Install Initial Packages For GitLab on Ubuntu

Installing its dependencies is essential before installing and operating GitLab on your system. These are necessary components that GitLab relies on to function correctly. You can easily install the dependencies by opening the terminal and running the following command:

sudo apt install curl ca-certificates apt-transport-https tzdata perl

Step 3: Import GitLab CE APT Repository on Ubuntu

By default, GitLab is not available in Ubuntu’s default repositories. This means you must create a repository manually to install GitLab. Fortunately, GitLab provides an APT script to assist you with this process.

To download the GitLab APT script, you can use the following command in your terminal:

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

This command will download the APT script and pipe it to the bash command to execute it. Once executed, the script will add the GitLab repository to your system and update the package list. This will enable you to install GitLab using the standard APT package manager.

Screenshot of terminal showing successful import of GitLab Community Edition repository via bash script on Ubuntu 22.04 or 20.04.Pin
Terminal screenshot showcasing successful GitLab installation.

Install Gitlab on Ubuntu 22.04 or 20.04

The next step is to install GitLab, and it’s important to note that the script already used the “apt update” command to synchronize the newly created and modified repository. With that done, you can now proceed to execute the install command by running the following:

sudo apt install gitlab-ce

This command will install the GitLab Community Edition (CE) package, the free and open-source version of GitLab. The installation process may take some time, depending on your internet speed and system specifications. Once the installation is complete, you can access GitLab by visiting your server’s IP address or domain name in a web browser.

Once the installation is complete, you should get the following output in your terminal.

Screenshot of terminal confirming successful GitLab installation on Ubuntu 22.04 or 20.04.Pin
Terminal screenshot showcasing successful GitLab installation.

Configure GitLab on Ubuntu 22.04 or 20.04

Configuring GitLab on Ubuntu requires careful attention to each step. This section explains the process, covering hostname configuration, firewall setup, and email settings adjustment.

Configure the GitLab Hostname on Ubuntu

Configure the hostname for your GitLab server as the first step. Choose a name that’s easy to remember and access because you’ll use this hostname to access your GitLab instance. Follow these steps to set the hostname:

  • Open the GitLab configuration file using a text editor:
sudo nano /etc/gitlab/gitlab.rb
  • Locate the external_url setting and replace the example URL with your desired hostname:
external_url 'https://gitlab.example.com'
  • Save the file and exit the text editor.
  • Reconfigure GitLab to apply the changes:
sudo gitlab-ctl reconfigure

Set Up the UFW Firewall For GitLab on Ubuntu

GitLab uses port 80 for HTTP and port 443 for HTTPS by default. It’s essential to configure your firewall to allow traffic on these ports to ensure that your GitLab instance is accessible. To set up the firewall, follow these steps:

  • Open the firewall configuration file using a text editor:
sudo nano /etc/ufw/applications.d/gitlab
  • Copy and paste the following lines into the file:
[GitLab]
title=GitLab
description=GitLab application
ports=80,443/tcp
  • Save the file and exit the text editor.
  • Enable the GitLab application in the firewall:
sudo ufw app update GitLab
sudo ufw allow GitLab

Configure GitLab Email Settings on Ubuntu

GitLab uses email notifications to inform users about project activities, such as new issues, merge requests, and comments. To configure email settings, follow these steps:

  • Open the GitLab configuration file using a text editor:
sudo nano /etc/gitlab/gitlab.rb
  • Locate the gitlab_rails[‘smtp_enable’] setting and set it to true:
gitlab_rails['smtp_enable'] = true

Configure the SMTP server settings for your email provider, such as Gmail, by adding the following lines to the file:

gitlab_rails['smtp_address'] = "smtp.gmail.com"
gitlab_rails['smtp_port'] = 587
gitlab_rails['smtp_user_name'] = "example@gmail.com"
gitlab_rails['smtp_password'] = "your_password"
gitlab_rails['smtp_domain'] = "smtp.gmail.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
  • Save the file and exit the text editor.
  • Reconfigure GitLab to apply the changes:
sudo gitlab-ctl reconfigure

It’s worth noting that after running the command “gitlab-ctl reconfigure,” you may have noticed that the terminal output ends with the following message.

Notes:
Default admin account has been configured with following details:
Username: root
Password: You didn't opt-in to print initial root password to STDOUT.
Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours.

If you need to view the root (GitLab) password, you can use the following command in your terminal:

sudo cat /etc/gitlab/initial_root_password
Terminal screenshot displaying GitLab login password on Ubuntu 22.04 or 20.04.Pin
Command terminal showing GitLab login credentials.

This command will print the root password to the terminal. It’s essential to keep this password secure and to avoid sharing it with unauthorized users.

In case you need to reset the root password, you can use the following command:

sudo gitlab-rake "gitlab:password:reset[root]"

This command will reset the root password to a random string displayed in the terminal output. It’s important to note that resetting the root password will invalidate all existing and personal access tokens, so users must create new ones after resetting the password.

You should change the root password to a strong and unique one as soon as possible after the initial setup and avoid using the default password for security reasons. You can change the root password by logging in to the GitLab web interface, navigating to “User Settings” > “Account,” and selecting the “Change Password” option.

Access GitLab User Interface on Ubuntu 22.04 or 20.04

With the backend configuration complete, it’s time to log in and access GitLab. To do this, open the domain path assigned to GitLab in the configuration file.

https://gitlab.example.com

Next, sign in to GitLab using the username “root” and the password you received during installation. In the image mentioned in the previous section, the guide’s password printout was “vsakkLBNnapKEI6Jtj7Zw3HZzlxVXF2WPwC32CBz3ys=”.

Screenshot of GitLab login page on Ubuntu 22.04 or 20.04.Pin
GitLab login page as seen on Ubuntu.

You successfully logged in with the root account and landed on the default page.

Screenshot of GitLab dashboard after successful login on Ubuntu 22.04 or 20.04.Pin
A first look at the GitLab dashboard post-login on Ubuntu.

Create Cronjob for GitLab Auto Backup on Ubuntu 22.04 or 20.04

Creating a cronjob for GitLab auto-backup on Ubuntu is crucial to ensure your data is safe and secure. A cronjob is a scheduled task that runs at specific intervals to perform a backup of your GitLab instance. Here are the steps to create a cronjob for GitLab auto-backup on Ubuntu:

Create a Backup Script For GitLab on Ubuntu

The first step is to create a backup script to perform your GitLab instance’s backup. To do this, follow these steps:

  • Create a new file for your backup script using a text editor, for example:
sudo nano /usr/local/bin/gitlab-backup.sh
  • Copy and paste the following code into the file:
#!/bin/bash

BACKUP_DIR=/var/opt/gitlab/backups
TIMESTAMP=$(date +%s)

sudo gitlab-rake gitlab:backup:create

sudo cp ${BACKUP_DIR}/$(ls -t ${BACKUP_DIR} | head -1) ${BACKUP_DIR}/gitlab_backup_${TIMESTAMP}.tar
  • Save the file and exit the text editor.

This script will create a backup of your GitLab instance using the “gitlab-rake gitlab:backup:create” command, then copy the latest backup to a new file with a timestamp in the file name.

Set GitLab Permissions and Ownership on Ubuntu

The next step is to set the correct permissions and ownership for the backup script to ensure the cronjob can execute it. To do this, follow these steps:

  • Set the owner of the backup script to the GitLab user:
sudo chown git:git /usr/local/bin/gitlab-backup.sh
  • Set the permissions of the backup script to allow execution:
sudo chmod +x /usr/local/bin/gitlab-backup.sh

Create a Cronjob on Ubuntu For GitLab Cron Script

The final step is to create a cronjob that will run the backup script at a specified interval. To do this, follow these steps:

  • Open the crontab configuration file for the GitLab user:
sudo crontab -u git -e
  • Add the following line to the file to run the backup script every day at 1:00 AM:
0 1 * * * /path/to/backup/script.sh
  • Save the file and exit the text editor.

This cronjob will run the backup script every day at 1:00 AM, creating a new backup file with a timestamp in the file name.

Test GitLab Cronjob on Ubuntu

To test the cronjob, run the backup script manually and ensure the system creates the backup file correctly. Use the command below in your terminal:

sudo /usr/local/bin/gitlab-backup.sh

This command will create a new backup file in the “/var/opt/gitlab/backups” directory with a timestamp in the file name.

GitLab Terminal Commands on Ubuntu 22.04 or 20.04

GitLab-ctl is a command-line tool that allows you to manage GitLab on Ubuntu. It provides a set of terminal commands that you can use to start, stop, restart, and manage GitLab services, among other things. Here is a detailed section on GitLab-ctl terminal commands, along with example commands and outputs with explanations.

Starting and Stopping GitLab services on Ubuntu

To start and stop GitLab services, use the following commands:

  • Start all GitLab services:
sudo gitlab-ctl start
  • Stop all GitLab services:
sudo gitlab-ctl stop
  • Restart all GitLab services:
sudo gitlab-ctl restart
  • Reload GitLab services:
sudo gitlab-ctl reload

These commands will initiate, halt, reboot, or refresh all GitLab services. The output will show which services start or stop and whether the operation succeeds.

Check the Status of GitLab services on Ubuntu

To check the status of GitLab services, use the following command:

sudo gitlab-ctl status

This command will display the status of all GitLab services, including whether they are running. The output will also show any errors or warnings affecting the services.

Back up and Restore GitLab data on Ubuntu

To backup and restore GitLab data, use the following commands:

  • Create a backup of GitLab data:
sudo gitlab-rake gitlab:backup:create

This command creates a backup of all GitLab data, including repositories, configuration files, and user data. The system saves the backup file in the “/var/opt/gitlab/backups” directory.

  • Restore a backup of GitLab data:
sudo gitlab-rake gitlab:backup:restore BACKUP=<backup_file>

This command will restore a backup of GitLab data from the specified backup file. The backup file should be in the “/var/opt/gitlab/backups” directory, or you can specify a full path to the backup file.

Check GitLab configuration on Ubuntu

To check the GitLab configuration, use the following command:

sudo gitlab-rake gitlab:check

This command will check the GitLab configuration for errors or warnings, such as missing dependencies, misconfigured settings, or outdated versions. The output will summarize the results and provide detailed information about any issues.

Check GitLab logs on Ubuntu

To check GitLab logs, use the following commands:

  • View the GitLab logs:
sudo gitlab-ctl tail gitlab-rails/production.log

The first example command will display the last 10 lines of the GitLab production log, which contains information about user activities, errors, and warnings.

  • View the GitLab NGINX logs:
sudo gitlab-ctl tail nginx/gitlab_access.log

The second example command will display the last 10 lines of the GitLab NGINX access log, which contains information about HTTP requests and responses.

  • View the GitLab PostgreSQL logs:
sudo gitlab-ctl tail postgresql/current

This third example command will display the last 10 lines of the GitLab PostgreSQL log, which contains information about database queries and transactions.

Conclusion

In conclusion, GitLab is a powerful DevOps platform that offers software development teams a wide range of features and tools. Installing GitLab on Ubuntu provides a secure, reliable, and cost-effective platform for managing your projects. The installation and configuration process for GitLab on Ubuntu may seem daunting initially. Still, following the steps outlined in this guide, you can set up a functional and secure GitLab instance that meets your organization’s needs.

Leave a Comment


Your Mastodon Instance
Share to...