GitLab is an open-source web-based Git repository management tool that offers a complete DevOps platform. It provides a wide range of features and tools for software development teams to manage their projects, including version control, project management, CI/CD, monitoring, and more. GitLab has become one of the most popular DevOps platforms and is used by thousands of organizations worldwide.
Here are some of the key features of GitLab:
- Version control: GitLab provides powerful version control capabilities that allow you to manage and track changes to your source code and other digital assets.
- Issue tracking: GitLab includes an issue tracking system that enables you to create, assign, and track issues, bugs, and feature requests.
- Continuous integration and deployment (CI/CD): GitLab’s built-in CI/CD pipeline allows you to automate your software development process, including building, testing, and deploying your applications.
- Code review: GitLab provides a code review feature that allows you to review and collaborate on changes made to your code by team members or contributors.
- Wiki and documentation: GitLab provides a built-in wiki and documentation system that allows you to create and manage project documentation.
- Project management: GitLab includes a range of project management features, such as milestones, burndown charts, and agile boards, that enable you to plan and manage your project’s progress.
- Integration with external tools: GitLab integrates with various external tools and services, such as Kubernetes, Jenkins, and Slack, to streamline your workflow and enhance your development process.
- Security and access control: GitLab provides advanced security features, such as two-factor authentication, SAML integration, and LDAP integration, to enhance the security of your projects and prevent unauthorized access.
- Customization: GitLab allows you to customize and configure your instance to suit your needs, including custom branding, theme, and workflows.
- Scalability: GitLab is designed to be scalable and can support many users and projects, making it suitable for enterprise-level organizations.
To summarize, GitLab is a versatile and potent DevOps platform that provides software development teams with a broad spectrum of features and tools. When you install GitLab on either Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 LTS Focal Fossa, you benefit from a dependable, secure, and cost-effective platform for managing your projects. To help you get started with GitLab, the following guide will walk you through all the necessary steps for installing and setting up GitLab.
Table of Contents
Step 1: Update Ubuntu
To ensure that all the existing packages on your Ubuntu operating system are up to date, performing an update is recommended. This will ensure your system has the latest security updates and bug fixes, improving its stability and reliability. To update your Ubuntu operating system, you can use the following command.
sudo apt update && sudo apt upgrade
Step 2: Install the Required Packages
Before installing and operating GitLab on your system, installing its dependencies is essential. 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 Repository
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.
Example output from the script:
Step 4: Install Gitlab
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.
Example:
Step 5: Configure GitLab
Configuring GitLab on Ubuntu involves steps that must be followed carefully. This section will explain the process step-by-step, including configuring the hostname, setting up the firewall, and configuring email settings.
Configure the Hostname
The first step is to configure the hostname for your GitLab server. This hostname will be used to access your GitLab instance, so it’s essential to choose a name that’s easy to remember and access. To configure the hostname, follow these steps:
- 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 Firewall
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 Email Settings
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
Example output:
This command will print the root password to the terminal. It’s important 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 access tokens and personal access tokens, so users must create new ones after the password is reset.
It’s recommended that you change the root password to a strong and unique one as soon as possible after the initial setup and that you 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.
Step 6: Access GitLab User Interface
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 password printout, the guide’s password was “vsakkLBNnapKEI6Jtj7Zw3HZzlxVXF2WPwC32CBz3ys=”.
Example:
You are successfully logged in as the root account and will hit the default landing page.
Example:
Here are some tips on securing, customizing, or any other general first-time tips when first logging into GitLab’s user interface:
- Changing the default password: It’s essential to change the root user’s default password to enhance your GitLab instance’s security. To do this, log in to GitLab using the default username and password, then navigate to “User Settings” > “Password” and enter your new password.
- Setting up two-factor authentication (2FA): 2FA is an extra layer of security that adds an authentication factor, such as a code sent to your phone or generated by an app, to the login process. You can enable 2FA in GitLab by navigating to “User Settings” > “Account” > “Two-Factor Authentication” and following the instructions.
- Creating a project: To create a new project in GitLab, navigate to the “Projects” page and click the “New Project” button. You can create an empty project or import an existing project from a Git repository.
- Customizing the user interface: GitLab provides several customization options, such as changing the theme, adding a custom logo, and configuring the navigation bar. You can customize the user interface by navigating to “Admin Area” > “Settings” and selecting the appropriate options.
- Configuring access control: GitLab provides powerful access control features that allow you to control who can access your projects and what they can do. You can configure access control by navigating to “Project Settings” > “Members” and adding or removing members with different access levels.
- Using GitLab CI/CD: GitLab provides powerful CI/CD capabilities that allow you to automate the testing and deployment of your code. You can configure CI/CD pipelines by creating a “.gitlab-ci.yml” file in your project’s root directory and defining the stages and jobs for your pipeline.
- Setting up GitLab Runner: GitLab Runner is a lightweight agent that runs CI/CD jobs and sends the results back to GitLab. You can install and configure GitLab Runner on your Ubuntu system using the instructions provided in the GitLab documentation.
- Backing up your GitLab instance: It’s essential to regularly back up your GitLab instance to prevent data loss in case of hardware failure or other disasters. You can back up your GitLab instance by running the “gitlab-rake gitlab:backup:create” command in your terminal.
Step 7: Create Cronjob for GitLab Auto Backup
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:
Creating a Backup Script
The first step is to create a backup script that will perform the backup of your GitLab instance. 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.
Setting Permissions and Ownership
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
Creating a Cronjob
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.
Testing the Cronjob
To test the cronjob, you can manually run the backup script and check that the backup file is created correctly. To do this, run the following command 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
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
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 start, stop, restart, or reload all GitLab services. The output will indicate which services are being started or stopped and whether the operation was successful.
Checking the status of GitLab services
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.
Backing up and restoring GitLab data
To backup and restore GitLab data, use the following commands:
- Create a backup of GitLab data:
sudo gitlab-rake gitlab:backup:create
This command will create a backup of all GitLab data, including repositories, configuration files, and user data. The backup file will be saved 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.
Checking the GitLab configuration
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.
Checking GitLab logs
To check GitLab logs, use the following commands:
- View the GitLab logs:
sudo gitlab-ctl tail gitlab-rails/production.log
This 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
This 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 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.