64

I've already set up a Git repository on GitHub and committed a few changes from my Windows machine.

But tomorrow I'll have to work in this repository from a machine running Ubuntu with limited privilege (i.e. no sudo).

Is there a portable version of Git for Linux? Or some source that allows me to compile and install Git only for the current user?

4
  • 1
    Why not just install it in your home directory instead of /usr/bin? Oct 28, 2010 at 3:00
  • 2
    If this is the only reason for using sudo during installations (as it now seems to be), I wasn't aware of that at all.
    – BoppreH
    Oct 28, 2010 at 3:07
  • 2
    Here is a nice step by step guide: http://joemaller.com/908/how-to-install-git-on-a-shared-host/ Nov 13, 2012 at 20:37
  • 1
    You certainly can, but first I'd ask the owner of the machine to install git (sudo apt-get install git). Nov 13, 2012 at 20:44

6 Answers 6

48

You can download the git source and do make configure && ./configure --prefix=/home/user/myroot && make && make install to install git to your home directory provided you have the build tools. If you don't have the build-essential package installed (dpkg --list|grep build-essential), you will need to install those to your home directory as well.

14
  • 4
    I wouldn't recommend using .git as the directory you install into, that just seems like it's asking for confusion (as git uses directories named .git to store the repo data for a non-bare repos). Oct 28, 2010 at 3:02
  • 3
    Ironically, by default Git configures itself to install into home directory.
    – P Shved
    Oct 29, 2010 at 13:57
  • 22
    If you can't find configure in the sources directory, you might have to run make configure beforehand (I did). Hope this helps!
    – Jonathan H
    Oct 16, 2013 at 10:27
  • Make sure to download a version that is compatible to your system. Latest isn't necessary the most proper...
    – Ruby Racer
    Sep 11, 2015 at 14:27
  • 2
    The INSTALL text file has many useful details. My remote non-sudo machine didn't have libcurl4-dev so I used "NO_CURL" option.
    – wbg
    Sep 26, 2015 at 20:44
18

I don't like link-only answers, but this link I followed step-by-step on a Fedora machine and it worked without modification. Very, very easy. The binaries end up in your ~/bin directory. You download a tarball, extract the sources, run make and run make install and that is it.

As the author states, the 2 prerequisites are gcc and ssh and if you meet these git should work for you as a non-root user.

1
  • 1
    Exactly! --prefix defaults to home install now, no need for it for this use case, tested v2.30.1. Feb 23, 2021 at 11:21
15

To install git and dependencies from source the following maybe useful.

Replace with the location you are installing your non-root apps and consider checking for latest versions of source code.

wget https://curl.haxx.se/download/curl-7.47.1.tar.gz
tar -xf curl-7.47.1.tar.gz
mkdir <local_curl_dir>
cd curl-7.47.1
./configure --prefix=<local_curl_dir>
make
make install

wget http://downloads.sourceforge.net/expat/expat-2.1.0.tar.gz
tar -xf expat-2.1.0.tar.gz
mkdir <local_expat_dir>
cd expat-2.1.0
./configure --prefix=<local_expat_dir>
make
make install

wget https://github.com/git/git/archive/v2.6.4.tar.gz
tar -xf v2.6.4
mkdir <local_git_dir>
cd git-2.6.4
make configure
./configure --prefix=<local_git_dir>/git --with-curl=<local_curl_dir>/curl --with-expat=<local_expat_dir>/expat
make
make install
4
  • 2
    Can you explain the downvote? If you are on a locked down machine without root and the dependencies are not installed, it gets you a working version of Git in 5 minutes.
    – 79E09796
    Aug 9, 2016 at 9:47
  • 3
    Did not work unfortunately. "make" on the git step says "zlib.h: No such file or directory"
    – phivo
    Feb 24, 2017 at 13:43
  • Worked for me. there was no autoconf so i had to generate config on EC2 and then copy it to my server
    – Alex B
    Jul 21, 2020 at 3:00
  • @phivo you can overcome that error with: cp /home/$(whoami)/install/zlib_install/include/zlib.h zlib.h and cp /home/$(whoami)/install/zlib-1.2.13/zconf.h zconf.h before you make git. (if you built zlib into ~/install/zlib_install. For me it this solution still throws error: cannot find -lz`.
    – a.t.
    Apr 27 at 16:41
13

This is what I ended up doing, the main trick being the make flags:

wget -O git.tar.gz https://github.com/git/git/archive/v2.17.0.tar.gz
tar zxf git.tar.gz
mv git-2.17.0 git
cd git
make configure
./configure --prefix=`pwd` --with-curl --with-expat
# ./configure --prefix=`pwd`
# Make flags from https://public-inbox.org/git/CAP8UFD2gKTourXUdB_9_FZ3AEECTDc1Fx1NFKzeaTZDWHC3jxA@mail.gmail.com/
make NO_GETTEXT=Nope NO_TCLTK=Nope
make install NO_GETTEXT=Nope NO_TCLTK=Nope

Credits:

  1. 79E09796's answer above was a good tip, but didn't work for my case on Cloudways and did not require compiling curl and expat.

  2. A random email record I found on the internet: https://public-inbox.org/git/CAP[email protected]/

4
  • 3
    Note that you should drop the -O git.tar.gz or update the following commands to use this name instead of the one with the version
    – Nathan
    Apr 22, 2020 at 19:56
  • Does not work. I get fatal error: zlib.h: No such file or directory. May 6, 2021 at 19:27
  • @Nathan I pinned the wget to v2.17.0 so it should work.
    – bdombro
    May 7, 2021 at 22:17
  • @IsaacGregson sounds like you need to install zlib somehow, which most linux systems have installed already.
    – bdombro
    May 7, 2021 at 22:18
12

A related answer is https://askubuntu.com/a/350.

I could get it work with the third method proposed:

apt-get source git
cd git_vXXX
./configure --prefix=$HOME
make
make install

I don't know why, but when I had tried to install from the source download from github instead, I had a lot of problems with missing dependencies

1
  • 1
    It works only if /etc/apt/source.list is configured with source repositories. May 20, 2015 at 8:41
8

Overkill workaround

Install Anaconda as a user and install git with conda.

Advantages

Anaconda can be installed as user, and a conda environment can be created which can help you to install other packages. This way you don't need to compile git from source, nor you need to install libcurl and perl, so you won't get the error

git: 'remote-https' is not a git command. See 'git --help'

after successfully compiling git.

Steps to install Anaconda, then git

  1. read the Anaconda installation manual which points to the download page's shell script file.
  2. Download the script file:
    • copy to your local machine and then copy with scp (e.g. winscp) to the Linux machine or
    • use a terminal on the Linux machine and issue wget https://repo.anaconda.com/archive/Anaconda3-2020.11-Linux-x86_64.sh.
  3. add executable rights to yourself on the file by issuing chmod +x Anaconda3-2020.11-Linux-x86_64.sh
  4. follow the installation instructions where you can also specify into which folder you want to install anaconda
  5. after installation, chose one of the possibilities below to successfully invoke git later:
    1. you either activate a conda environment, which is useful e.g. if you need to create different conda environments (it is common if you work with python). To activate an environment, you need to ask the installer (at the end of the installation) to add the conda initialization code into your ~/.bashrc file [see below]. Using this method, your path won't be polluted, and you will see if conda-related binaries are also in your current path. Or you can
    2. add the installed bin's folder to your path, e.g. if you installed anaconda into /home/myusername/anaconda3, it will be in /home/myusername/anaconda3/bin. Your new executable file conda will be also there which will help you to install packages like git, python or pandoc, or
    3. cd into the binary folder of anaconda, e.g. cd /home/myusername/anaconda3/bin, and execute the commands below.
  6. don't forget to take into effect the new settings by, e.g., closing and opening the terminal again if you selected method 1. or 2. in point 5. If you selected 1, you will see something like (base) myusername@servername indicating you are using the base conda environment.
  7. Now you can install git using conda by issuing conda install -c anaconda git.

Your .bashrc will contain likes like this if you told the Anaconda installer to initialize conda for you:

# content of your .bashrc in your home dir
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/myusername/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/myusername/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/myusername/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/myusername/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

2
  • 1
    This works for me. I don't have root on Cent OS so I have to install everything from source. But I still got errors like git: 'remote-https' is not a git command. I did some search and it looks like that I need to install libcurl-devel but I couldn't find its source, only rpm was found. So I tried your solution and finally can install linuxbrew correctly. After installation, brew install autojump gives me two errors: it's not using a newer version of curl I manually installed and neither the anaconda git. I tried export HOMEBREW_NO_ENV_FILTERING=1 && brew install autojump and it works
    – whyisyoung
    Feb 13, 2022 at 3:57
  • As all the other answers fail, this is the solution for me
    – 刘致远
    Nov 7, 2022 at 5:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.