如何在 Ubuntu 上安装 GitLab
GitLab
![](/data/attachment/album/202308/08/113049el2dx242c4mwm40k.jpg)


GitLab 是一个开源平台，提供了强大且功能丰富的解决方案，用于管理仓库、问题、CI/CD 管道等。如果你是 Ubuntu 22.04 或 20.04 用户，并且想要设置自己的 [GitLab](https://about.gitlab.com/) 实例来简化你的 DevOps 工作流程，那么你来对地方了。


本分步指南将引导你完成 Ubuntu 22.04 或 20.04 上 GitLab 的安装过程。GItlab 提供企业版（Gitlab EE）和社区版（Gitlab CE）。在这篇文章中，我们将介绍社区版。


先决条件：


* 运行 Ubuntu 22.04 或 20.04 且具有 SSH 访问权限的虚拟或专用服务器。
* 静态主机名（`gitlab.linuxtechi.net`）
* 具有管理员权限的 Sudo 用户
* 2GB 内存或更多
* 2 个或更多 vCPU
* 互联网连接


### 1、更新系统包


让我们首先更新软件包列表并将任何现有软件包升级到最新版本。



```
$ sudo apt update
$ sudo apt upgrade -y

```

应用更新后重新启动系统。



```
$ sudo reboot

```

### 2、安装依赖项


GitLab 需要一些依赖项才能正常运行。使用以下命令安装它们：



```
$ sudo apt install -y curl openssh-server ca-certificates postfix

```

在 postfix 安装过程中，会出现一个配置窗口。选择 “Internet Site”并输入服务器的主机名作为邮件服务器名称。这将允许 GitLab 发送电子邮件通知。


![](/data/attachment/album/202308/08/113315wpmqepeqlt9wg00e.jpg)


选择 “Internet Site”，然后选择 “OK”。


![](/data/attachment/album/202308/08/113329pl11qt1ed17e8d9n.jpg)


检查系统的主机名并选择 “OK”。


### 3、添加 GitLab Apt 存储库


现在，我们将添加 GitLab 仓库，运行以下 `curl` 命令。它将自动检测你的 Ubuntu 版本并相应地设置仓库。



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

```

![](/data/attachment/album/202308/08/113342reg5uqxdoxd0f9d9.jpg)


### 4、安装 Gitlab


运行以下命令在你的 ubuntu 系统上自动安装和配置 gitlab-ce，将服务器的主机名替换为你的设置，



```
$ sudo EXTERNAL_URL="http://gitlab.linuxtechi.net" apt install gitlab-ce

```

上述命令成功执行后，我们将得到如下输出。


![](/data/attachment/album/202308/08/113400llqmmw55tllwy55w.jpg)


![](/data/attachment/album/202308/08/113414n5egejzo0wesjbbs.jpg)


上面的输出确认 GitLab 已成功安装。gitlab web 界面的用户名是 root，密码存储在 `/etc/gitlab/initial_root_password`。


注意：如果你的 ubuntu 系统上启用了操作系统防火墙，那请允许 80 和 443 端口。



```
$ sudo ufw allow http
$ sudo ufw allow https

```

### 5、访问 GitLab Web 界面


安装并配置 GitLab 后，打开 Web 浏览器并输入服务器的 IP 地址或主机名。



```
http://<Server-IP-Address-or-Hostname>

```

* 用户名：`root`
* 密码：从 `/etc/gitlab/initial_root_password` 获取密码


![](/data/attachment/album/202308/08/113430dwg8gh388fp0w81p.jpg)


点击“<ruby> 登录 <rt>  Sign in </rt></ruby>”。


![](/data/attachment/album/202308/08/113452ach16rhr866i0q1m.jpg)


很好，上面确认我们已经成功登录 Gitlab Web 界面。


目前我们的 GitLab 服务器运行在 http（80）协议上，如果你想为你的 GitLab 启用 https，请参考以下步骤。


### 6、为 GitLab Web 界面设置 HTTPS


为提高安全性，可使用自签名证书或 Let's Encrypt 为 GitLab 实例配置 HTTPS。Let's Encrypt 只适用于互联网上有 A 记录的公有域。但在本例中，我们使用的是私有域，因此将使用自签名证书来确保 GitLab 的安全。


现在，让我们创建以下文件夹并使用 `openssl` 命令生成自签名证书：



```
$ sudo mkdir -p /etc/gitlab/ssl
$ sudo chmod 755 /etc/gitlab/ssl

```

使用以下 `openssl` 命令生成私钥：



```
$ sudo openssl genrsa -des3 -out /etc/gitlab/ssl/gitlab.linuxtechi.net.key 2048

```

输入密码并记住它。


使用以下命令创建 CSR：



```
$ sudo openssl req -new -key /etc/gitlab/ssl/gitlab.linuxtechi.net.key -out /etc/gitlab/ssl/gitlab.linuxtechi.net.csr

```

![](/data/attachment/album/202308/08/113509c4ba94a4yj29fa9l.jpg)


从密钥中删除密码串，依次执行以下命令：



```
$ sudo cp -v /etc/gitlab/ssl/gitlab.linuxtechi.net.{key,original}
$ sudo openssl rsa -in /etc/gitlab/ssl/gitlab.linuxtechi.net.original -out /etc/gitlab/ssl/gitlab.linuxtechi.net.key
$ sudo rm -v /etc/gitlab/ssl/gitlab.linuxtechi.net.original

```

创建证书文件：



```
$ sudo openssl x509 -req -days 1460 -in /etc/gitlab/ssl/gitlab.linuxtechi.net.csr -signkey /etc/gitlab/ssl/gitlab.linuxtechi.net.key -out /etc/gitlab/ssl/gitlab.linuxtechi.net.crt

```

使用下面的 `rm` 命令删除 CSR 文件：



```
$ sudo rm -v /etc/gitlab/ssl/gitlab.linuxtechi.net.csr

```

设置密钥和证书文件的权限：



```
$ sudo chmod 600 /etc/gitlab/ssl/gitlab.linuxtechi.net.key
$ sudo chmod 600 /etc/gitlab/ssl/gitlab.linuxtechi.net.crt

```

Gitlab 服务器的所有重要配置均由文件 `/etc/gitlab/gitlab.rb` 控制，因此编辑此文件，搜索 `external_url` 并添加 `https://gitlab.linuxtechi.net`。



```
$ sudo vi /etc/gitlab/gitlab.rb
----------------------------------------------------------
external_url 'https://gitlab.linuxtechi.net'
----------------------------------------------------------

```

保存并退出文件，使用下面的命令重新配置 gitlab，以便其 Web 界面可以使用 HTTPS。



```
$ sudo gitlab-ctl reconfigure

```

![](/data/attachment/album/202308/08/113523fsw47nugk7pl37nm.jpg)


成功执行上述命令后，你的 GitLab 界面应该可以通过 HTTPS 协议访问，在我的例子中，URL 为：`https://gitlab.linuxtechi.net/`


当你第一次访问它时，它会说你的连接不安全，点击“接受风险并继续”。


![](/data/attachment/album/202308/08/113538ikwkirbmxfzubmou.jpg)


### 结论


恭喜！ 你已在 Ubuntu 22.04 或 20.04 系统上成功安装 GitLab。随着 GitLab 的启动和运行，你现在可以创建仓库，与你的团队协作，并通过 GitLab 令人印象深刻的功能增强你的开发工作流程。享受无缝版本控制、持续集成等，一切尽在你的掌控之中！


*（题图：MJ/c6a3e27e-fe58-4184-b133-9e9c67224316）*




---


via: <https://www.linuxtechi.com/how-to-install-gitlab-on-ubuntu/>


作者：[Pradeep Kumar](https://www.linuxtechi.com/author/pradeep/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In this blog post, we will explain how to install GitLab on Ubuntu 22.04 step-by-step. Gitlab comes with Enterprise edition (Gitlab EE) and Community edition (Gitlab CE). In this post , we will cover the community edition installation.

GitLab, an open-source platform, provides a robust and feature-rich solution for managing repositories, issues, CI/CD pipelines, and much more. If you’re an Ubuntu 22.04 user and want to set up your own [GitLab](https://about.gitlab.com/) instance to streamline your DevOps workflow, you’re in the right place.

#### Prerequisites

- A virtual or dedicated server running Ubuntu 22.04 with SSH access.
- Static Hostname (gitlab.linuxtechi.net)
- Sudo User with admin rights
- 2 GB RAM or more
- 2 vCPUs or more
- Internet Connectivity

## 1) Update System Packages


Let’s begin by updating the package lists and upgrading any existing packages to their latest versions.

$ sudo apt update $ sudo apt upgrade -y

Reboot the system after applying updates,

$ sudo reboot

## 2) Install Dependencies


GitLab requires some dependencies to function correctly. Install them using the following commands:

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

During the postfix installation, a configuration window will appear. Choose “Internet Site” and enter your server’s hostname as the mail server name. This will allow GitLab to send email notifications.

Choose “Internet Site” and then slect OK.

Check system’s hostname and choose OK.

## 3) Add GitLab Apt Repository


Now, we’ll add the GitLab repository, run the following curl command. It will automatically detect your Ubuntu version and will set the repository accordingly.

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

## 4) Install Gitlab on Ubuntu 22.04


Run the beneath command to install and configure gitlab-ce on your ubuntu system automatically, replace the server’s hostname as your setup,

$ sudo EXTERNAL_URL="http://gitlab.linuxtechi.net" apt install gitlab-ce

Once the above the command is executed successfully, we will get output something like below,

Output above confirms that GitLab has been installed successfully. User name for gitlab web interface is root and password is stored at “/etc/gitlab/initial_root_password”

Note : If OS firewall is enabled on your ubuntu system, then allow 80 and 443 ports.

$ sudo ufw allow http $ sudo ufw allow https

## 5) Access GitLab Web Interface


With GitLab installed and configured, open your web browser and enter your server’s IP address or hostname.

http://<Server-IP-Address-or-Hostname>

- User Name: root
- Password : <<Get Password from /etc/gitlab/initial_root_password>>

click on “Sign in”

Great, above confirms that we have successfully login to Gitlab Web interface.

As of now our GitLab Server is working on http (80) protocol, if you want to enable https for your GitLab, then refer the below steps,

## 6) Secure GitLab Web Interface


For added security, you can configure HTTPS for your GitLab instance using self-signed certificate or Let’s Encrypt. We can use Let’s encrypt only for public domain whose A recorded available on internet. But in our case, we are using a private domain, so we will be using self-signed certificate to secure GitLab.

Now, let’s create following folder and generate self-sign certificates using openssl command

$ sudo mkdir -p /etc/gitlab/ssl $ sudo chmod 755 /etc/gitlab/ssl

Generate the private key using following openssl command,

$ sudo openssl genrsa -des3 -out /etc/gitlab/ssl/gitlab.linuxtechi.net.key 2048

Enter the passphrase and and remember it

Create the CSR using below command,

$ sudo openssl req -new -key /etc/gitlab/ssl/gitlab.linuxtechi.net.key -out /etc/gitlab/ssl/gitlab.linuxtechi.net.csr

Remove Passphrase from the key, execute the following commands one after the another

$ sudo cp -v /etc/gitlab/ssl/gitlab.linuxtechi.net.{key,original} $ sudo openssl rsa -in /etc/gitlab/ssl/gitlab.linuxtechi.net.original -out /etc/gitlab/ssl/gitlab.linuxtechi.net.key $ sudo rm -v /etc/gitlab/ssl/gitlab.linuxtechi.net.original

Create the Certificate file

$ sudo openssl x509 -req -days 1460 -in /etc/gitlab/ssl/gitlab.linuxtechi.net.csr -signkey /etc/gitlab/ssl/gitlab.linuxtechi.net.key -out /etc/gitlab/ssl/gitlab.linuxtechi.net.crt

Remove the CSR file using below [rm command](https://www.linuxtechi.com/rm-command-examples-for-beginners/),

$ sudo rm -v /etc/gitlab/ssl/gitlab.linuxtechi.net.csr

Set the permissions on key and certificate file

$ sudo chmod 600 /etc/gitlab/ssl/gitlab.linuxtechi.net.key $ sudo chmod 600 /etc/gitlab/ssl/gitlab.linuxtechi.net.crt

All the important configuration for Gitlab server is controlled by the file “/etc/gitlab/gitlab.rb” So edit this file, search “external_url” and add the “https://gitlab.linuxtechi.net”

$ sudo vi /etc/gitlab/gitlab.rb ---------------------------------------------------------- external_url 'https://gitlab.linuxtechi.net' ----------------------------------------------------------

Save and exit the file, reconfigure gitlab using beneath command so that it’s web interface start working https.

$ sudo gitlab-ctl reconfigure

Once above command is executed successfully, then your GitLab interface should be accessible over https protocol, In my case url will be: https://gitlab.linuxtechi.net/

When you access it first time, it will say something like your connection is not secure, click on “Accept the Risk and Continue”

#### Conclusion

Congratulations! You’ve successfully installed GitLab on your Ubuntu 22.04 system. With GitLab up and running, you can now create repositories, collaborate with your team, and enhance your development workflow through GitLab’s impressive features. Enjoy seamless version control, continuous integration, and more, all under your control!

Gerald BrownAfter running steps 1 & 2 above then running step 3 I get the following error

sudo apt install gitlab-ce returns Unable to locate package gitlab-ce

Pradeep KumarHi Gerald,

It seems like gitlab ce repository is not configured properly on your system that’s why you are getting the error.

To Fix this issue, please try to enable the repo using the step2 and then run “apt-get update” and after that try to install gitlab-ce packages.