# Homebrew 安装旧版本软件包,macOS 安装特定版本软件包

mac OS中使用`Homebrew`安装软件包时，默认是安装最新稳定版本，有些情况下我们需要使用特定的历史版本软件包，此时我们通过以下方式来安装。

## 1. 搜索软件包可用版本

![brew search](https://cdn.hashnode.com/res/hashnode/image/upload/v1658739682155/Ox8kQxham.jpg align="left")

## 2. 安装历史版本
如上图所示，`postgresql`提供了历史版本，此时可以可以直接安装指定版本的包。如`brew install postgresql@9.5`。`jpeg`则没有直接提供历史版本，我们可以通过以下方式安装其历史版本。

![brew info](https://cdn.hashnode.com/res/hashnode/image/upload/v1658739745412/wgQkPENQi.jpg align="left")

通过上图可以看到`Homebrew`安装`jpeg`使用是`https://github.com/Homebrew/homebrew-core/blob/master/Formula/jpeg.rb`文件。那我们就可以在`Github`提交历史记录中找到该文件特定历史版本进行软件包安装即可，思路清楚之后下面来逐步执行。

![Github历史版本](https://cdn.hashnode.com/res/hashnode/image/upload/v1658740470167/LOX95pk87.jpg align="left")

如上图所示，直接访问`jpeg`的安装源文件时可以看到当前版本为`9c`。点击`History`查看该文件提交历史记录。

![Github版本](https://cdn.hashnode.com/res/hashnode/image/upload/v1658740530399/jLo1DVMll.jpg align="left")

由于`homebrew-core`库较大无法直接列出其提交历史记录，按照提示我们可以`clone`库到本地后再搜索其提交记录。

```bash
git clone https://github.com/Homebrew/homebrew-core.git
cd homebrew-core
git log master -- Formula/jpeg.rb
```

![Git log](https://cdn.hashnode.com/res/hashnode/image/upload/v1658740631591/H9y5ixCW2.jpg align="left")

根据实际情况选择特定版本的历史提交记录，如这里我们选择`jpeg 8d`的最后一次提交,访问该`commit`的安装文件`https://github.com/Homebrew/homebrew-core/blob/b23146936a3abb57aabb39017561e7b0c750abbf/Formula/jpeg.rb`，如下图所示可以看到`jpeg`版本为`8d`。点击`Raw`获取其原始文件URL并安装。

![旧版本提交记录](https://cdn.hashnode.com/res/hashnode/image/upload/v1658740733207/YW9v0MMWM.jpg align="left")

![安装旧版本](https://cdn.hashnode.com/res/hashnode/image/upload/v1658740745933/n-WIRV8-N.jpg align="left")
至此，`brew`安装特定版本的软件包就成功完成了。

## 3. 多版本切换
使用`brew info package`可以查看软件包的安装版本,如:
```
brew info postgresql
postgresql: stable 9.3.2 (bottled)
http://www.postgresql.org/
Conflicts with: postgres-xc
/usr/local/Cellar/postgresql/9.1.5 (2755 files, 37M)
  Built from source
/usr/local/Cellar/postgresql/9.3.2 (2924 files, 39M) *
  Poured from bottle
From: https://github.com/Homebrew/homebrew/commits/master/Library/Formula/postgresql.rb
```
使用brew switch package version可以切换软件包版本。如:
```
brew switch postgresql 9.1.5
Cleaning /usr/local/Cellar/postgresql/9.1.5
Cleaning /usr/local/Cellar/postgresql/9.3.2
384 links created for /usr/local/Cellar/postgresql/9.1.5
```
切换完成后可以再次查看确认。
```
brew info postgresql
postgresql: stable 9.3.2 (bottled)
http://www.postgresql.org/
Conflicts with: postgres-xc
/usr/local/Cellar/postgresql/9.1.5 (2755 files, 37M) *
  Built from source
/usr/local/Cellar/postgresql/9.3.2 (2924 files, 39M)
  Poured from bottle
From: https://github.com/Homebrew/homebrew/commits/master/Library/Formula/postgresql.rb
```
带 `*`项表示当前使用的版本。
