Setting up your own git server on Ubuntu

This will create a new user ‘gitosis’ and prepare a structure for repositories in /srv/gitosis. Now let’s initialize a gitosis-admin repo – it is used for managing repositories and access

Of course there’s always an option to use github. And if you’re working on an open source project, or want to concentrate on coding and not system administration, github is a lot better option than setting up and managing your own git server (I’ve been so impressed by @defunkt‘s presentation on #frozenrails, that started recommending github to everyone 🙂 But if you already pay for a virtual machine somewhere (like Linode), then setting up your own git server might be a viable option, especially that it is sooo easy.

The following instructions have been verified on Ubuntu 10.04 Lucid Lynx, but should work at least on Ubuntu 9.04 and 9.10 just as well.

Let’s start with installing gitosis itself. Issue the following command on the server:

sudo apt-get install gitosis

This will create a new user ‘gitosis’ and prepare a structure for repositories in /srv/gitosis. Now let’s initialize a gitosis-admin repo – it is used for managing repositories and access rights.

sudo -H -u gitosis gitosis-init < ~/tmp/my_public.key

You need to have a public key for accessing it. If you don’t have one yet, you can use

ssh-keygen -t rsa

command on your local machine to generate public/private key pair. Copy public key to the server before initializing gitosis-admin repository.

Now with gitosis-admin repo initialized on the server – let’s clone it to the local computer.

git clone gitosis@myserver.com:gitosis-admin.git

If you see an error like:

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

That is most likely because you restricted access to you server via ssh to only certain users, and gitosis is not one of them. Edit /etc/ssh/sshd_config, find AllowUsers line and add gitosis to the list.

Now that you have successfully cloned gitosis-admin repo to your local computer, you can add  new users and new repositories.

To add new repository, edit gitosis.conf and add lines like:

[group myrepo]
writable = myrepo
members = user@computer

After that commit and push the changes to gitosis-admin project:

git commit -a -m "Added myrepo repository"

Now you can clone this new repository to your local machine (note .git added to the name of the repository):

git clone gitosis@mygitserver.com:myrepo.git

To allow new user to access your repository, get this user’s public key, copy it to gitosis-admin/keydir as newuser@computer.pub, then edit gitosis.conf and add newuser@computer (without .pub) to the list of members:

[group myrepo]
writable = myrepo
members = user@computer newuser@computer

Now add new files and commit and push changes to git server (make sure you really add all files and don’t forget to push – these are quite common mistakes when adding new users).

git add .
git commit -m "Added user newuser"
git push

With a freshly cloned empty repository you’ll need to add you first files, do a commit and push origin master:

vim README.txt
git commit -a -m "Added readme"
git push origin master

Now you can git pull and git push as much as you like.


Comments

16 responses to “Setting up your own git server on Ubuntu”

  1. sudo -H -u git gitosis-init < ~/tmp/my_public.key

    should read…

    sudo -H -u gitosis gitosis-init < ~/tmp/my_public.key

    No git user was found using the first example. After installing gitosis, the user created is "gitosis", not "git".

    Good tutorial by the way. Yours is the first I have come across which made it work.

    Many thanks.

  2. Thank you, Ethan. I corrected the guide.

  3. diamond diana

    i cannot get this to work.
    i am basically stuck at the first step. when i am trying to clone the git repo i get the following error

    fatal: ‘gitosis-admin.git’ does not appear to be a git repository
    fatal: The remote end hung up unexpectedly

    i am using ubuntu 10.04. any ideas?

  4. What is the exact command you use to do the clone? Do you have anything extra before “gitosis-admin.git”? Do you have correct key to access the remote machine – i.e. does does ssh gitosis@yourserver.com work?

  5. Srihari

    As you said “sudo apt-get install gitosis”
    will create a new user ‘gitosis’ and prepare a structure for repositories in /srv/gitosis.
    Is there a way to change the location from /srv/gitosis to say for example /home/mygit or something ?

  6. @Diana instead of using the above scp like syntax for addressing the repo you can use git clone ssh://gitosis@myserver.com/gitosis-admin.git

  7. TheWakeUpCall

    Hi, I have tried to follow your guide but I can’t seem to get it to work. I get to the stage where I put ‘git clone gitosis@myserver.com:gitosis-admin.git’ but I just get asked for a password for the gitosis account. I have searched the internet far and wide and cannot find a solution. My key is definitely right, as I checked the settings in gitosis and it matches that of my machine.

    Any clues?

  8. Thanks for the instructions. I was able to get it set up. I am doing a connection from an svn repository, so I created the MyRepo.git with “git svn clone” and then copied the results into /srv/gitosis/repositories”.

    Since I created these from svn repositories, do you have methods to continue to update the gitosis from

  9. Bjørn T.

    You probably want a git push after the git commit -a -m “Added myrepo repository” as well…

  10. A Group section defines which groups there are and what users are in them and in what repos they have write access.
    Thank you!

  11. after adding README.txt you first have to “git add .”
    Great simple walkthrough though! thanks!

  12. […] I didnt confuse you too much on the way. This guide uses parts from Ivan Kuznetsov’s guide, the gelp with the ssh came from the ssh manpage. This entry was posted in Guide and tagged […]

  13. […] here’s some I followed, but they all have slight variations in the approach: here, here, and here. It seems somewhat hit and miss whether it works or not (as you’ll find if you google for […]

  14. […] followed this guide http://www.ivankuznetsov.com/2010/05…on-ubuntu.html but when I doing the clone the password isn’t […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.