Ubuntu源碼安裝最新版本git

Git是個低頻更新但很重要的開發軟件,系統官方倉庫的版本總是落後最新版不少,不想安裝額外的日更源的話,手動源碼安裝最新版本是不錯的解決方法。

1. 安裝依賴包(install libraries that Git depends)

sudo apt-get install dh-autoreconf libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev install-info

2. 下載git源碼包 (grab the latest source package)

源碼包在kernel.org,懂的都懂,Linus創造,必須放在高大上的地方, 地址爲: https://www.kernel.org/pub/software/scm/git 按時間找到最新版本,寫這個文章的時候最新版本包爲:git-2.37.0.tar.xz

3. 解壓到指定位置,我一般喜歡把非系統的源碼包放在/opt (uncompress the files)

➜  sudo tar -xvf git-2.37.0.tar.xz -C /opt/
git-2.37.0/
git-2.37.0/.cirrus.yml
git-2.37.0/.clang-format
git-2.37.0/.editorconfig
git-2.37.0/.gitattributes
....

4. 編譯安裝 (configuire && make install)

➜  cd /opt/git-2.37.0

看看默認編譯參數(check default location)

➜  git-2.37.0 ./configure --help
`configure' configures git 2.37.0 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
      --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources in DIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc.  You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.

For better control, use the options below.

符合規範,沒有有必要特殊設置,開始編譯

  ➜  git-2.37.0 sudo ./configure
configure: Setting lib to 'lib' (the default)
configure: Will try -pthread then -lpthread to enable POSIX Threads.
configure: CHECKS for site configuration
checking for gcc... gcc
checking whether the C compiler works... yes

安裝

➜  git-2.37.0 sudo make install
GIT_VERSION = 2.37.0
    * new build flags
    CC fuzz-commit-graph.o
    CC fuzz-pack-headers.o
    ......

檢查結果,新安裝的版本:

➜  git-2.37.0 /usr/local/bin/git --version
git version 2.37.0

原來系統的版本爲:

➜  git --version
git version 2.34.1

把老的版本備份爲git_old,留條退路是個好習慣。並確認環境變量PATH的搜索路徑中有/usr/local/bin

➜  sudo mv /usr/bin/git /usr/bin/git_old
➜  echo $PATH
/home/mephisto/.cargo/bin:/home/mephisto/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

大功告成

➜  git -v
git version 2.37.0

相關文章:

翻譯: