Installing Gitlab on Ubuntu 22.04

Installing Gitlab on Ubuntu 22.04
Hostman Team
Technical writer
GitLab
07.11.2024
Reading time: 7 min

GitLab is a software tool designed to store and manage Git repositories. It facilitates and accelerates the process of collaborative coding, testing, and deployment.

Key Features of GitLab

  • Planning and managing developing projects
  • Creating, viewing, and managing project code and data
  • Code verification through automated testing and reporting
  • Resource monitoring and metric viewing
  • Using pre-made template models

This list of features is not exhaustive; it represents just the essential components necessary to understand the importance of using this software in project activities.

This article will provide a detailed guide on how to install GitLab on Ubuntu and configure it afterward.

Prerequisites

Below is a list of requirements sufficient for installing GitLab on a server.

First, you will need a server located either on a personal computer or in the cloud. According to GitLab's documentation requirements, the server should be equipped with:

  • A quad-core processor (load up to 500 users)
  • 4 GB of RAM (load up to 500 users)
  • Ubuntu 22.04 OS

Second, you will need a domain name that points to the server. In this guide, we will use example.com.

Installing GitLab on Ubuntu

Step 1. Adding Dependencies

The first step is to install the software required for GitLab. You can download it from the Ubuntu repositories.

Perform a package update:

sudo apt-get update

Next, install the necessary dependencies:

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

The next thing to install on the server is Postfix (or Sendmail), which serves as a mechanism for sending notifications to email:

sudo apt-get install -y postfix

During the installation, you need to select Internet Site in the pop-up window.

Next, specify the server's domain — in our case, it will be example.com.

Step 2. Installing GitLab

Now we can start installing GitLab. Let's download the script:

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

To check the functionality of this script, enter the command:

sudo less /tmp/script.deb.sh

If everything is satisfactory, you can proceed directly to running the script:

sudo bash /tmp/script.deb.sh

Image6

Once the script has finished running, you can start the GitLab installation:

sudo apt install gitlab-ce

Step 3. Configuring the Firewall

First, you need to check which network protocols are allowed. To see the status of the firewall, enter the command:

sudo ufw status

In the output, we see that traffic is currently allowed only for the SSH protocol. 

Status: active
To             Action       From
–              -------      ----
OpenSSH        ALLOW        Anywhere
OpenSSH (v6)   ALLOW        Anywhere (v6)

In addition to SSH, you need to allow the HTTP and HTTPS traffic.

sudo ufw allow http
sudo ufw allow https

After making these changes, check the firewall status again to make sure all changes have taken effect.

Status: active

To                Action      From
--                ------      ----
OpenSSH           ALLOW       Anywhere                               
80/tcp            ALLOW       Anywhere      
443/tcp           ALLOW       Anywhere     
OpenSSH (v6)      ALLOW       Anywhere (v6)       
80/tcp (v6)       ALLOW       Anywhere (v6)           
443/tcp (v6)      ALLOW       Anywhere (v6)  

Step 4. Configuring the Configuration File

Before starting GitLab, it is important to edit the configuration in the gitlab.rb file using the nano editor:

sudo nano /etc/gitlab/gitlab.rb

In the opened file, find the external_url line and change the domain name to example.com. You will have a different name. We will also switch to a more secure encryption protocol — HTTPS.

##! 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 'https://example.com'

## Roles for multi-instance GitLab
##! The default is to have no roles enabled, which results in GitLab running as an all-in-one instance.

Next, find the Let's Encrypt integration section and the # letsencrypt['contact_emails'] line. Uncomment the line and insert the email address into the brackets.

# Let's Encrypt integration
################################################################################
# letsencrypt['enable'] = nil
letsencrypt['contact_emails'] = [[email protected]] # This should be an array of email addresses to add as contacts
# letsencrypt['group'] = 'root'
# letsencrypt['key_size'] = 2048

This is necessary for sending notifications in case of issues with the domain. 

Save your changes (CTRL+O) and exit the file (CTRL+X). To apply all changes, run the reconfiguration:

sudo gitlab-ctl reconfigure

This will pass all the server information to the application and reconfigure Let's Encrypt for the domain.

GitLab Setup

Enter your domain name in your browser's address bar to access your GitLab instance.

Registration and Authentication

Upon your first visit, a welcome page will appear, prompting you to set a password for your account. After entering and confirming a strong password, you will be redirected to the authentication window.

To log into the administrator account, you need to fill in the following fields:

  • Username: root

  • Password: the password you set earlier.

Once logged in, the home page will open, where the user can create their first project and get started.

Profile Setup

The new GitLab user will have standard user settings applied. To change them, go to the Settings tab in the upper right corner of the window in the dropdown menu.

In the Profile section, you can update your information.

Important changes at this stage will be the name and email address. The name will be visible to other project members, while the email is necessary for receiving notifications from the service. To link the new email, you need to confirm it via the email sent by the service.

To apply the changes, click the Update profile settings button at the bottom of the screen.

Changing the Username

Navigate to the Account section in the settings menu on the left. Here, you should change the automatically assigned name to something more unique, as illustrated in the image below. This will provide additional security for the account in the event of attempted hacks.

Image5

Additionally, in this section, the user can enable two-factor authentication.

Adding an SSH Key

To interact with Git and GitLab, you need to generate and attach a public key in the settings.

Start by generating the keys:

ssh-keygen

During the key generation process, the system will prompt you to specify a custom storage location for the key pair and set a passphrase for added security. To skip these steps, simply press Enter.

Once the key generation is complete, copy the public key and paste it into the interface, adding a brief description.

To open the public key, use the following command:

cat ~/.ssh/id_rsa.pub

After copying the key to the required section, click the "Add key" button.

Your First Project

To create your first project in GitLab:

  1. Go to the main page and click on Create a project. This will open the project creation form.
  2. Fill in the required information, such as the project name and repository type. The repository can be public, private, or internal.
  3. If you plan to add files from an existing repository, do not check the box for Initialize repository with a README. Otherwise, you should check it.
  4. Next, click the Create repo button to create your first project.

Now you can upload project files, add users for collaboration, or delete the project if necessary.

Conclusion

This article has provided a step-by-step guide to installing GitLab on Ubuntu 22.04. After completing all the steps, you can deploy your first project in the system and start working with it.

GitLab
07.11.2024
Reading time: 7 min

Similar

Git

Installing and Using GitLab Runner

GitLab Runner is a web application (agent) designed to launch and automatically run CI/CD processes in GitLab. GitLab Runner runs tasks from the .gitlab-ci.yml file, which is located in the root directory of your project. Runner can be installed either on the same server with GitLab or separately. The main thing is that there is network communication between GitLab Runner and the GitLab server. You can install Gitlab Runner on operating systems such as Linux, Windows, macOS, and it also supports running in a Docker container. In this article, we will install GitLab Runner in Docker and run a test project. Prerequisites To install GitLab Runner, you will need: A cloud server or a virtual machine running a Linux OS. You can use any Linux distribution compatible with Docker. Docker installed.  An account on gitlab.com, as well as a pre-prepared project. You can install Docker manually (we have a step-by-step guide for Ubuntu) or automatically from Marketplace, when creating a new Hostman server. Installing GitLab Runner using Docker First, connect to the server where Docker is installed. Create a Docker volume in which we will store the configuration. A Docker volume is a file system for persistent storage of information. Data in a volume is stored separately from the container. The volume and the data will remain if you restart, stop, or delete the container.  The command to create a volume named runner1 is: docker volume create runner1 Next, launch the container with the gitlab-runner image: docker run -d --name gitlab-runner1 --restart always \     -v /var/run/docker.sock:/var/run/docker.sock \     -v runner1:/etc/gitlab-runner\     gitlab/gitlab-runner:latest Check the status of the container and make sure that it is running (Up): docker ps This completes the installation of GitLab Runner. The next step is Runner registration. Registering GitLab Runner Once Runner has been installed, it must be registered. Without registration, Runner will not be able to complete tasks. Launch the gitlab-runner container and execute the register command: docker run --rm -it -v runner1:/etc/gitlab-runner gitlab/gitlab-runner:latest register You will be prompted to enter the URL of the GitLab server: If you are using self hosted GitLab installed on a separate server, use the address of your GitLab instance. For example, if your project is located at https://gitlab.test1.com/projects/testproject, then the URL will be https://gitlab.test1.com. If your projects are stored on GitLab.com, then the URL is https://gitlab.com. Next, you will need to enter the registration token. To get the token, go to the GitLab web interface, select the project, select the Settings section on the left, then CI/CD: Find the Runners menu, expand the section. You will find the token in the Action menu (three dots): Next, you'll be prompted to enter a description for this Runner. You can skip it not writing anything: Now you need to set the tags. Tags are labels designed to determine which runner will be used when running tasks. You can enter one or several tags separating them by commas: When entering a maintenance note, you can add information for other developers, containing, for example, technical information about the server. You can also skip this step.  Select an executor, i.e. the environment for launching the pipeline. We will choose docker. In this case, the pipeline will be launched in Docker containers, and upon completion, the containers will be deleted. At the last step, select the Docker image to use in the container where the pipeline will be launched. As an example, let's choose the python 3.10-slim image: After you are done registering the Runner, it will be displayed in the project settings, in the Runners section: Using GitLab Runner when starting a pipeline In order to use Runner to run a pipeline, you need to create a file called .gitlab-ci.yml. You can create a file directly in the root directory of the project or in the GitLab web interface: On the project main page click Set up CI/CD (this button is only visible before you set up CI/CD for the first time): Click Configure pipeline: When you first set up a pipeline, GitLab provides the basic pipeline syntax. In our example, we use a Python project, namely a script to test the speed of an Internet connection. If the script executes successfully, the output should display information about incoming and outgoing connections: Your Download speed is 95.8 Mbit/sYour Upload speed is 110.1 Mbit/s The pipeline syntax will look like this: image: python:3.10-slim default: tags: - test1 before_script: - pip3 install -r requirements.txt run: script: - python3 check_internet_speed.py To assign a previously created Runner to this project, you need to add: default:   tags:     - test1 Where test1 is the tag of the Runner we created. With this tag, the pipeline will be executed on the Runner that is assigned the test1 tag. Save the changes to the file (make a commit) and launch the pipeline. If you look at the job execution process, you can see at the very beginning of the output that the gitlab runner is used: The full output of the entire pipeline is shown in the screenshot below: Conclusion In this tutorial, we have installed and configured GitLab Runner, assigned it to a project, and launched the pipeline.
27 May 2024 · 5 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support