Easy Self-Hosted Git Installation on Ubuntu Server
Tags: git, ubuntu, postgres, visualstudio
Published: 2024-05-01 15:27:07

Welcome to a quick guide detailing the installation and setup of a self-hosted git server designed for beginners. We'll start by installing Ubuntu server, a popular choice known for its ease of use. Next, we'll set up PostgreSQL, a powerful and reliable open-source database management system, to ensure seamless data management for our git server. Finally, we'll integrate Gitea, a lightweight self-hosted git service, enabling you to effortlessly host your repositories and collaborate with your team members. For advanced configurations, detailed manuals for each service will be referenced later in the text. Let's proceed, step by step.

## Ubuntu Server
To begin, we'll need an operating system. For this guide, Ubuntu Server is our choice. If you already have a Linux system installed, feel free to skip this step. Installing Ubuntu Server is straightforward—simply follow these steps:

1. Download the Ubuntu Server ISO image from the official website: [https://ubuntu.com/download/server](https://ubuntu.com/download/server)
    
2. Create a bootable drive and proceed with the installation, or create a new virtual machine from the ISO file.
    
3. Install the server according to your preferences. Detailed installation instructions can be found on the official website: [https://ubuntu.com/tutorials/install-ubuntu-server](https://ubuntu.com/tutorials/install-ubuntu-server)

>**For this tutorial I have chosen the following settings:**
>- **Language**: English
>- **Keyboard**: English (US)
>- **The base for the installation**: Ubuntu Server
>- **Network**: Choose according to your own preferences
>- **Storage and file system**: Choose according to your own preferences
>- **Profile setup**: Choose your name and password
>    - **name**: administrator
>    - **server name**: git\_server
>    - **password**: choose your strong password.
>- **OpenSSH**: Install OpenSSH server

After installing the server, log in with your login to the system.

## PostgreSQL
Before we proceed with installing Gitea, it's essential to set up the database. Gitea supports various databases, including PostgreSQL, MySQL, MariaDB, and MSSQL. In this guide, we'll focus on installing and configuring PostgreSQL on the local server.

1. First, ensure your system is up to date:
    
    `$ sudo apt update`
    
2. Install PostgreSQL and its additional components:
    
    `$ sudo apt install postgresql postgresql-contrib`
    
    or
    
    `$ sudo apt install postgresql`
    
3. Enable PostgreSQL to start automatically on system boot:
    
    `$ sudo systemctl enable postgresql.service`
    
4. Check the status of the PostgreSQL service:
    
    `$ sudo systemctl status postgresql.service`


![PostgreSQL service status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bosxncl8ikb01kmnljyy.png)

5. Switch to the postgres user:
    
    `$ sudo su - postgres`
    
6. View the ***postgresql.conf*** file to check the '***password\_encryption***' attribute. You can use a text editor like **nano**. Find the path to the configuration file using the '***SHOW config\_file***' command.
    
    `$ psql -c 'SHOW config_file'`
    
    `$ nano [FILE-NAME]`
    
    Ensure '***password\_encryption***' is set to '***scram-sha-256***':
    
    `password_encryption = scram-sha-256`
    
7. Restart PostgreSQL to apply the changed settings:
    
    `$ systemctl restart postgresql`
    
8. Access the PostgreSQL terminal:
    
    `$ psql`
    
9. Create a database user. Replace '***mypassword***' with a strong password. *Don't forget the semicolon at the end*:
    
    `CREATE ROLE gitea WITH LOGIN PASSWORD 'mypassword';`


![Create role](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ri3f5c9lj6v6h4yapuiz.png)

10. Create the '***giteadb***' database. Later, you connect to the database using the name of that database and the name of the user created above:
    
    `CREATE DATABASE giteadb WITH OWNER gitea TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';`
    
11. Exit the PostgreSQL terminal:
    
    `\q`
    
12. Stay logged in with '***postgres***' user and add an authentication rule to the '***pg\_hba.conf***' file. You can use a text editor like **nano**. Find the path to the configuration file using the '***SHOW hba\_file***' command:
    
    `$ psql -c 'SHOW hba_file'`
    
    `$ nano [FILE-NAME]`
    
    Add the following line, where '***giteadb***' is the database name and '***gitea***' is the user name:
    
    `local giteadb gitea scram-sha-256`
    
13. Restart PostgreSQL to apply the changed settings:
    
    `$ systemctl restart postgresql`
    
14. Test the connection to the database, where '***gitea***' is the user name and '***giteadb***' is the database name:
    
    `$ psql -U gitea -d giteadb`


![Test database connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6dcmirmxf4ig11k5iek.png)

15. Exit the PostgreSQL terminal and switch user with a user with sudo privileges:
    
    `\q`
    
    `$ su - administrator`
    
## Gitea
For self-hosted git I chose Gitea. It's a powerful tool for smaller teams that provides many advanced features such as Git hosting, code review, team collaboration, CI/CD, and more. For a detailed feature comparison, visit [https://docs.gitea.com/installation/comparsion](https://docs.gitea.com/installation/comparsion). Additionally, you'll find a comprehensive installation guide there.

1. Install Gitea using wget. Replace the URL with the desired version. We recommend opting for the latest version. Here's an example for 64-bit Linux, version 1.22. For the full list, visit [https://dl.gitea.com/gitea/](https://dl.gitea.com/gitea/):
    
    `$ wget -O gitea https://dl.gitea.com/gitea/1.22/gitea-1.22-linux-amd64`
    
    `$ chmod +x gitea`
    
2. Verify if Git is installed:
    
    `$ git --version`
    

![Gitea version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qi9s17z7n7ixj1sn152j.png)

3. Create a user for Gitea:
    
    `$ sudo adduser --system --shell /bin/bash --gecos 'Git Version Control' --group --disabled-password --home /home/git git`
    

![Create user](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bf5vd9rd25043u5lspuy.png)

4. Set up the directory structure:
    
    `$ mkdir -p /var/lib/gitea/{custom,data,log}`
    
    `$ chown -R git:git /var/lib/gitea/`
    
    `$ chmod -R 750 /var/lib/gitea/`
    
    `$ mkdir /etc/gitea`
    
    `$ chown root:git /etc/gitea`
    
    `$ chmod 770 /etc/gitea`
    
5. Copy Gitea to a global location:
    
    `$ sudo cp gitea /usr/local/bin/gitea`
    
6. Create a system service. Download the file and save it to `/etc/systemd/system/` or view the `raw` file in a browser and replace the URL with the version of Gitea you installed. You can find the list on [https://github.com/go-gitea/gitea/blob/release/v1.22/contrib/systemd/gitea.service](https://github.com/go-gitea/gitea/blob/release/v1.22/contrib/systemd/gitea.service):
    
    `$ sudo wget https://raw.githubusercontent.com/go-gitea/gitea/release/v1.22/contrib/systemd/gitea.service -P /etc/systemd/system/`
    
7. Enable automatic start of the service:
    
    `$ sudo systemctl enable gitea --now`
    

![Enable gitea service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvpi2b3atarys5mwrdwk.png)

    If you have a problem running the service, check the contents of the file: `/etc/systemd/system/gitea.service`
    
8. Verify the status of the Gitea service:
    
    `$ systemctl status gitea`

    
## Logging in to the Gitea Web Interface
If the installation was successful and the gitea service is running, it's time to log in to Gitea via the web interface. Launch the web browser and enter the ***IP*** of your new git server and port ***3000*** in the address field. You should see the ***Initial Configuration*** page.

Example: `192.168.52.129:3000`

### Initial Configuration - Database Settings

In this section we will configure the connection between git and your PostgreSQL database.

![Database settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ffi01yfhxvb6or9qy1c.png)

* **Database Type**: Select `PostgreSQL`
    
* **Host**: Since we have installed the database as local, we choose local IP and default port for PostgreSQL. `127.0.0.1:5432`
    
* **Username**: The PostgreSQL database user (role) we created. In this case `gitea`.
    
* **Password**: Password for the PostgreSQL database user we created. In this case `mypassword`.
    
* **Database Name**: The name of the database we created. In this case `giteadb`.
    
* **SSL**: Securing the communication between the gitea and the PostgreSQL database. In this case `Disable` since the database server is installed locally.
    
* **Schema**: Leave blank, the database is in the default `Public` scheme.
    
### Initial Configuration - General Settings
Gitea configuration. We can use the default settings, or change them according to our needs. It is worth mentioning the path to the repositories, LFS and logs. Change to a more reasonable repository if necessary.

![General settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ziemdnxluehyhud1exmh.png)

### Initial Configuration - Optional Settings
In this section it is possible to set up a mailer for gitea, set up a server and third party service and set up an administrator account.

![Optional settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3g50xxncsne8zkry17rg.png)

For this quick tutorial we can leave it in the default settings and click the `Install Gitea` button. All settings can be changed later in the Gitea configuration file `/etc/gitea/app.ini`

![Install Gitea button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuaqjluumknpujn8uyfp.png)

### Sign In to Gitea
If you have created an administrator account in ***Initial Configuration - Optional Settings***, you can now log in with it. If not, register a new account. The first user registered to Gitea becomes the admin.

![Sign in to gitea](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8besyg732v3yynv8ygb.png)

And that's it, your self-hosted git server is ready to use.

## Let's Test Your Self-Hosted Git Using MS Visual Studio.
We have no choice but to try git server. First we create a new test repository in Gitea and then link it to the project in Visual Studio.

### Create New Repository
After logging into Gitea, we create a new repository.

![Create repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pf8juv9xci1vr2t44a1i.png)

Fill in all the settings according to your preferences and click the `Create Repository` button.

### Visual Studio and Git
First, we'll copy the URL link from our new test repository in Gitea.

![Repository url link](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyvi047fiwuo2cq6ile1.png)

Now in the Visual Studio project, go to the `Git` tab and select `Create Git repository...`. On the left hand side under the `Other` category, select `Existing remote`. In the `Push your code to an existing remote` section, paste your copied URL link and click on `Create and Push`. Visual Studio prompts you for a username and password.

![Visual Studio repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjs18miefaf5py8oq5qq.png)

In the `View` tab, select `Git Changes`, using this window you can work with the linked git. If `Commit` doesn't work, you probably don't have a user name set. Check this in `Git -> Settings`. In Visual Studio 2022 I noticed a small bug when VS had to be restarted, otherwise it didn't allow me to commit changes even if I had the user set up.

Now you should be able to see all your commits in your git repository in Gitea using the web browser.

## Conclusion
In this simple tutorial, I tried to show you how to install git server using Gitea in a simple step-by-step way. It was installed on an Ubuntu server using PostgreSQL as the datastore. I didn't go into any detailed setup of Linux, the database server or Gitea itself, the goal was to show how easy it is to run a self-hosted git server. I have tested all the steps in the tutorial in a virtual environment, there may be slight changes depending on the versions of each component installed. I hope you have fun discovering the new features.

👀 In another article I wrote a little bit about workflow actions. If you would like to run Continuous Integration on Gitea, see here: [Gitea Self-Hosted Workflow Action For CI](https://dev.to/mortylen/gitea-self-hosted-workflow-action-for-ci-1bhf)
    

> *Cover Photo by <a href="https://unsplash.com/@scottrodgerson?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Scott Rodgerson</a> on <a href="https://unsplash.com/photos/a-bunch-of-blue-wires-connected-to-each-other-PSpf_XgOM5w?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Unsplash</a>*
    
***

> *My github profile [GitHub](https://github.com/mortylen)*
> *My blog page [Hashnode](https://mortylen.hashnode.dev/)*