142

I read about a security flaw in Git, which was fixed in version 2.2.1. I currently have Git 2.1.0 on my system (Ubuntu 14.10), and tried to reinstall it with apt. However, apt told me that I currently have the latest version.

The Git website does not have prebuilt versions for Linux. They say that you can install it with package managers. Without building from source, how would I install the latest version of Git?

Knowledge Cube
  • 14,681
  • 15
  • 80
  • 151
190n
  • 1,559
  • 2
  • 11
  • 6
  • 2
    I wouldn't worry too much. "Git 2.2.1 was released this afternoon ( 18 December 2014 ) to fix a critical security vulnerability in Git clients. Fortunately, the vulnerability doesn't plague Unix/Linux users but rather OS X and Windows." Source: http://www.phoronix.com/scan.php?page=news_item&px=MTg2ODA – Elder Geek Feb 20 '15 at 03:06
  • This question isn't Ubuntu-specific and was already asked on Unix&Linux three years earlier - [How can I update to a newer version of Git using apt-get?](http://unix.stackexchange.com/questions/33617/how-can-i-update-to-a-newer-version-of-git-using-apt-get/170831) – Dan Dascalescu Aug 30 '16 at 04:59
  • https://itsfoss.com/install-git-ubuntu/ – Deepak Dec 20 '22 at 07:20

4 Answers4

231

Use the PPA from the maintainers of git on Ubuntu:

sudo apt-add-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git

If you don't know what PPAs are, first read What are PPAs and how do I use them?

If you receive an error about add-apt-repository command not found you need to install software-properties-common, and then redo the above steps.

sudo apt-get install software-properties-common
muru
  • 193,181
  • 53
  • 473
  • 722
  • 2.9.0 is the version I have just installed with this PPA, in case anyone is wondering... – Aaron Hall Jul 12 '16 at 00:01
  • 1
    cool, now I can do `git pull --rebase --autostash` on 2.9 :) – Oscar Mederos Jul 22 '16 at 18:39
  • I used "apt-add-repository", it does not work. I used "add-apt-repository". I suspect a typo? – user1914692 Aug 07 '16 at 15:21
  • @user1914692 you do see the post has sudo applied to all three commands, right? – muru Aug 07 '16 at 15:23
  • I mean, "apt-add-repository" vs " add-apt-repository " – user1914692 Aug 07 '16 at 15:25
  • @user1914692 both commands exist in all current versions of Ubuntu and are the *same*. Something wrong with your system. – muru Aug 07 '16 at 15:28
  • @muru, OK, thanks for your information. But anyway thanks for your post that now I am now using the latest version of git! – user1914692 Aug 07 '16 at 15:34
  • 1
    For more clarification for later viewers, muru is correct that both commands should work. The first time when I used "apt-add-repository", it complains "Cannot add PPA: 'ppa:git-core/ppa'." Actually since the terminal didn't complain about the command not found, I should have not suspected the validity of the command. muru's response reminded me to test that command again. And the 2nd time I tried "apt-add-repository", it works! Both commands work! So I guess the failure of the first time I used that command might be due to something related to Internet. – user1914692 Aug 07 '16 at 15:41
  • 1
    @JeffPuckettII It appears to be now at least. Git 2.10.1 (Oct 3) is available now. – MEMark Oct 23 '16 at 18:59
  • For anyone finding this who is running Trusty LTS (14.04) you may need to run `sudo apt-get install software-properties-common` to be able to access `apt-add-repository`. – cori Mar 05 '17 at 13:25
  • @cori it's installed in the desktop editions by default. Might not be in the server or minimal installs. – muru Mar 05 '17 at 13:30
  • On Ubuntu 16.04, `apt-add-repository` is symlinked to `add-apt-repository`. – Asclepius Jun 21 '17 at 03:13
  • @dragon788 `add-apt-repository` and `apt-add-repository` are the **same** command. – muru Jul 17 '17 at 14:00
  • @muru You are correct, they are the same command and I think at one point `apt-add-repository` was the "real" binary, but now it is simply a symlink to `add-apt-repository` and I personally prefer to point to the actual binary rather than symlinks as depending how a system is set up symlinks may not behave as intended. – dragon788 Jul 17 '17 at 17:40
  • @dragon788 that's superstition and I'll thank you to keep such nonsense out of my posts, especially when you go about saying that it's an "incorrect" command. – muru Jul 17 '17 at 22:55
  • @muru, Is there a place to download git compiled binaries and just extract them? No installation or compilation. Just download it as you download ant other binary TAR. – Royi Jul 27 '17 at 21:38
4

This what i did to upgrade git 1.7.9.5 to 2.xxx on Ubuntu 12.04:

sudo apt-get install python-software-properties
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:git-core/ppa -y
sudo apt-get update
sudo apt-get install git -y
git --version
Duncan Lock
  • 131
  • 5
John Woods
  • 41
  • 3
  • 1
    The `sudo apt-get upgrade` could affect a LOT more than just `git` depending on the contents of the PPA, I'd recommend skipping that unless you know you want any packages that are newer in the PPA than just the dependencies that will be pulled in by the `install git`. – dragon788 Jul 17 '17 at 17:37
  • I edited the global apt upgrade out of the answer. – Duncan Lock Oct 26 '17 at 19:52
3

The most common situation is when you want to install the latest version of git, but your Operating System's repositories are not updated. For example, in my case I have a laptop running Ubuntu 20.04, and when I executed the command sudo apt install git the installed version was 2.25.1; instead of 2.32.0 which is the current version at git-scm.com.

How can I get the latest version?

Well, we can install it by following one of these methods: Using APT Reprositories, Building and Installing or Using binary files.

A. Building and Installing (recommended for developers)

A-1. Uninstall the default version provided by Ubuntu's package manager and configuration by using:

sudo apt remove --purge --auto-remove -y git

or:

sudo apt purge --auto-remove -y git

A-2. Go to the official CMake webpage, then download and extract the latest version. Update the version and build variables in the following command to get the desired version:

version=2.32
build=0
mkdir ~/temp
cd ~/temp
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-$version.$build.tar.gz
tar -xzvf git-$version.$build.tar.gz
cd git-$version.$build/

A-3. Install the extracted source by running:

make -j$(nproc) prefix=/usr/local all
sudo make prefix=/usr/local install

A-4. Test your new git version.

git --version

Results of git --version:

git version 2.25.X

B. Using PPA Repositories (recommended for normal users)

There is a PPA available from Ubuntu Git Maintainers team that we can use to easily install the latest stable Git version. So we can install it easily following these steps:

B-1. Uninstall the default version provided by Ubuntu's package manager as in A-1.

B-2. Add PPA repository to your sources list.

sudo add-apt-repository ppa:git-core/ppa

B-3. Finally we can update and install the git package.

sudo apt update
sudo apt install git

B-4. Test your new git version as in A-4.

Note

In 2.32.X the X represents the last part of the version that we defined as build. The build may change if git is updated. According to the official web page the Latest Release is 2.32.0. If you want the Previous Release 2.31.1 just replace the version and build parameters like this:

version=2.31
build=1
mkdir ~/temp
cd ~/temp
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-git-$version.$build.tar.gz
tar -xzvf git-$version.$build.tar.gz
cd git-$version.$build/
Teocci
  • 4,465
  • 2
  • 11
  • 12
1

Install specific version instead

If you want to install a specific version of the PPA, not necessarily the latest (I would recommend that for future reproducibility) you can use something like:

sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git=1:2.36.0-0ppa1~ubuntu20.04.1

This would install git 2.36 on Ubuntu 20.04.

The humongous version string 1:2.36.0-0ppa1~ubuntu20.04.1 was copy pasted directly from the PPA page: https://launchpad.net/~git-core/+archive/ubuntu/ppa

Some more info on version strings at: Why do some packages have extra numbers before a colon on the front of their version string?

Ciro Santilli OurBigBook.com
  • 26,663
  • 14
  • 108
  • 107