How to Install and Configure GitLab on VPS for Version Control

GitLab is a popular, open-source Git repository manager that provides a web interface for version control, issue tracking, CI/CD pipelines, and more. Installing GitLab on a Virtual Private Server (VPS) allows you to have a self-hosted version control system for managing your code repositories.

In this guide, we will walk you through the steps to install and configure GitLab on your VPS for managing your version control and project management needs.


Prerequisites

Before you begin the installation, ensure the following:

  1. A VPS running Ubuntu 20.04 or later (This guide assumes Ubuntu; you can adapt the instructions for other distributions such as CentOS or Debian).
  2. Root or sudo access to your VPS.
  3. A minimum of 4 GB of RAM and 2 CPU cores (recommended for GitLab).
  4. A domain name (optional, for a more professional setup) and access to your DNS provider.

Step 1: Update Your VPS

To ensure that your system packages are up-to-date, run the following commands:

 
sudo apt update && sudo apt upgrade -y

Step 2: Install Required Dependencies

GitLab requires several dependencies to run properly. Install them by running the following command:

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

For email notifications, you will need an SMTP server. You can configure SMTP later, but for now, we will proceed without it.


Step 3: Install GitLab

GitLab provides a convenient Omnibus package that bundles all necessary dependencies, making it easy to install and configure. You can install GitLab by following these steps:

  1. Download and Install the GitLab Package:

    First, you need to add the GitLab package repository to your system. Run the following commands to install GitLab:

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

    After the repository is added, you can install GitLab CE (Community Edition) by running:

     
    sudo apt install gitlab-ce -y
  2. Configure GitLab:

    Once GitLab is installed, you need to configure it. You can configure it using the gitlab-ctl command.

    Run the following command to configure GitLab:

     
    sudo gitlab-ctl reconfigure

    This command will configure GitLab with the default settings. It may take a few minutes to complete.


Step 4: Access GitLab Web Interface

Once the installation is complete, GitLab should be running on your VPS. You can access the GitLab web interface by navigating to the following URL in your browser:

  • If you're using the server's IP: http://your_server_ip
  • If you're using a domain: http://your_domain.com

The first time you access GitLab, you'll be prompted to set an admin password. This password will be used for the root admin account.

  1. Set the Admin Password: Enter a strong password and click "Save" to continue.
  2. Log In: After setting the password, you can log in to GitLab using the username root and the password you just set.

Step 5: Configure GitLab for Production Use

Configure GitLab URL

If you're using a domain name for your GitLab instance, you should configure the external URL so that GitLab knows how to handle requests properly.

  1. Open the GitLab configuration file:

     
    sudo nano /etc/gitlab/gitlab.rb
  2. Find the line that begins with external_url and change it to your domain name (or IP address if you're not using a domain):

    ruby
     
    external_url 'http://your_domain_or_ip'
  3. After saving the changes, reconfigure GitLab:

     
    sudo gitlab-ctl reconfigure

Set Up SSL (Optional but Recommended)

If you want to secure your GitLab instance with HTTPS, it is highly recommended to set up an SSL certificate. You can use a free SSL certificate from Let's Encrypt.

  1. Open the GitLab configuration file again:

     
    sudo nano /etc/gitlab/gitlab.rb
  2. Change the external_url to https:

    ruby
     
    external_url 'https://your_domain_or_ip'
  3. Enable Let's Encrypt:

    ruby
     
    letsencrypt['enable'] = true
  4. Save the file and reconfigure GitLab:

     
    sudo gitlab-ctl reconfigure

GitLab will automatically generate and install the SSL certificate for you.


Step 6: Create a GitLab Project and Add Repositories

After you have successfully set up GitLab, you can start creating projects and adding repositories.

  1. Create a Project:

    • Log in to the GitLab web interface with your root account.
    • Click on the “New Project” button.
    • Enter the project name, description, and visibility level (Private, Internal, or Public).
    • Click "Create Project".
  2. Create a New Repository:

    You can create a new repository inside your project by following the instructions on the project page. You can either create a repository through the GitLab interface or clone it to your local machine and push your code.

    Example commands to clone and push to GitLab:

     
    git clone http://your_domain_or_ip/your_project.git cd your_project echo "# My New Project" > README.md git add README.md git commit -m "initial commit" git push origin master
  3. Add Users and Permissions:

    You can invite other users to collaborate on your GitLab project. To add users:

    • Go to your project page.
    • Click on “Settings” > “Members”.
    • Enter the user's email address and select the appropriate permissions (Guest, Reporter, Developer, Maintainer, or Owner).

Step 7: Enable and Manage GitLab Services

GitLab is composed of several services (such as the web interface, database, etc.), all of which are managed using gitlab-ctl.

  1. Check GitLab Services Status:

    To check the status of GitLab services, run:

     
    sudo gitlab-ctl status
  2. Start, Stop, or Restart GitLab:

    You can start, stop, or restart GitLab services using the following commands:

     
    sudo gitlab-ctl start sudo gitlab-ctl stop sudo gitlab-ctl restart
  3. View Logs:

    To view the logs of GitLab, use:

     
    sudo gitlab-ctl tail

Step 8: Backup and Restore GitLab

It's important to regularly back up your GitLab data. GitLab provides built-in tools for creating and restoring backups.

  1. Create a Backup:

    To create a backup of your GitLab instance, run:

     
    sudo gitlab-rake gitlab:backup:create
  2. Restore from Backup:

    To restore a backup, run:

     
    sudo gitlab-rake gitlab:backup:restore BACKUP=your_backup_timestamp

Conclusion

You have now successfully installed and configured GitLab on your VPS for version control. With GitLab, you can manage your Git repositories, collaborate with your team, set up CI/CD pipelines, and much more. Remember to secure your GitLab installation, configure SSL, and regularly back up your data to ensure your project stays safe.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution