参考文档:https://www.cnblogs.com/huanhao/p/installbrew.html
前言
最近公司刚给换了台macbook,所以相应的环境都要重新搭建。在macbook上的包管理工具最常用的是brew了。但是macbook原生系统中是不带brew命令的。那就自己安装呗。
在百度上搜索brew,出现了brew的官网(https://brew.sh)。官网上有一键安装brew的命令:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
可是通过该命令却没办法安装,报错如下:
~ $ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
~ $
ping一下raw.githubusercontent.com也不通,结果如下:
~ $ ping raw.githubusercontent.com
PING raw.githubusercontent.com (0.0.0.0): 56 data bytes
ping: sendto: Socket is not connected
ping: sendto: Socket is not connected
Request timeout for icmp_seq 0
ping: sendto: Socket is not connected
Request timeout for icmp_seq 1
ping: sendto: Socket is not connected
Request timeout for icmp_seq 2
ping: sendto: Socket is not connected
Request timeout for icmp_seq 3
ping: sendto: Socket is not connected
Request timeout for icmp_seq 4
^C
--- raw.githubusercontent.com ping statistics ---
6 packets transmitted, 0 packets received, 100.0% packet loss
~ $
dig一下raw.githubusercontent.com发现压根儿没法找到该网址的解析,结果如下:
~ $ dig raw.githubusercontent.com
; <<>> DiG 9.10.6 <<>> raw.githubusercontent.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 5353
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;raw.githubusercontent.com. IN A
;; ANSWER SECTION:
raw.githubusercontent.com. 3600 IN A 0.0.0.0
;; Query time: 59 msec
;; SERVER: 192.168.2.1#53(192.168.2.1)
;; WHEN: Sun Nov 22 11:56:43 CST 2020
;; MSG SIZE rcvd: 70
~ $
所以按照官方提供的方法来安装brew这条路是走不通啦,因此必须要另辟蹊径。通过度娘后找到了参考的文章,参照文章方法果然在我的新macbook上装好了brew。现将我安装brew的过程记录下以备后用。
安装过程
创建Homebrew目录
~ $ sudo mkdir /usr/local/Homebrew
同步brew.git库
~ $ sudo git clone https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git /usr/local/Homebrew
创建brew命令的软连接
~ $ sudo ln -s /usr/local/Homebrew/bin/brew /usr/local/bin/brew
同步core库
~ $ sudo mkdir -p /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
~ $ sudo git clone https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
同步cask
~ $ sudo mkdir -p /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask
~ $ sudo git clone https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask
更改镜像源为国内镜像源
~ $ cd "$(brew --repo)"
~ $ git remote set-url origin git://mirrors.ustc.edu.cn/brew.git
查看brew版本
~ $ brew -v
Homebrew 2.5.11-125-g31adc60
Homebrew/homebrew-core (no git repository)
~ $
使用brew安装软件及过程中问题解决
上面已经将brew安装好了,下面我将通过用brew来安装cscope命令来验证brew的呈现安装。
查看是否可以通过brew来安装cscope
~ $ brew search cscope
==> Formulae
cscope
~ $
从搜索结果来看cscope命令可以通过brew来安装。
通过brew安装cscope命令
brew安装软件命令为“brew install xxxx”。
~ $ brew install cscope
mkdir: /usr/local/var/homebrew/locks: Permission denied
Error: Can't create update lock in /usr/local/var/homebrew/locks!
Fix permissions by running:
sudo chown -R $(whoami) /usr/local/var/homebrew
Error: No such file or directory @ dir_s_rmdir - /usr/local/var/homebrew/locks/33f5b4cc8d183a0da943414e4f562ddcb878da0a534bdc20915e5a3a0ead63db--cscope-15.9.catalina.bottle.tar.gz.incomplete.lock
Warning: Bottle installation failed: building from source.
Error: No such file or directory @ dir_s_rmdir - /usr/local/var/homebrew/locks/96b28826500337506c46fbdf2140505bdfe852a5d80a93ef9e777aa65de5916b--cscope-15.9.tar.gz.incomplete.lock
zdns@zdnsdeMacBook-Air Homebrew % sudo brew install cscope
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
~ $
安装出错,通过提示创建目录/usr/local/var/homebrew/locks的权限不够造成的。原因是/usr/local目录是root用户所拥有的,且该目录只有root用户才具有写权限。而我是在用普通用户在操作brew,因此没法在/usr/local目录下创建对应的子目录。
~ $ ls -l /usr
total 0
lrwxr-xr-x 1 root wheel 25 9 22 11:05 X11 -> ../private/var/select/X11
lrwxr-xr-x 1 root wheel 25 9 22 11:05 X11R6 -> ../private/var/select/X11
drwxr-xr-x 1014 root wheel 32448 9 22 11:13 bin
drwxr-xr-x 311 root wheel 9952 9 22 11:13 lib
drwxr-xr-x 264 root wheel 8448 9 22 11:13 libexec
drwxr-xr-x 11 root wheel 352 11 22 10:41 local
drwxr-xr-x 234 root wheel 7488 9 22 11:13 sbin
drwxr-xr-x 46 root wheel 1472 9 22 11:13 share
drwxr-xr-x 5 root wheel 160 9 22 05:29 standalone
~ $
解决方案分为两种:
- 对目录/usr/local的赋予普通用户都有写操作的权限。这个操作一次解决,但是改变了原有目录的权限结构,可能会有风险。命令为:chmod -Rf a+w /usr/local。
- 通过root账户在目录/usr/local下创建子目录,然后再改变该子目录的拥有者为普通用户。本操作比较麻烦,但是不会改变系统原有的目录权限结构。命令为:sudo mkdir -p /usr/local/var; sudo chmod -Rf a+w /usr/loca/var; sudo chown -Rf username /usr/local/var。
我出于简单方便,且我自身电脑也没什么太多的机密可供窃取,所以我直接简单粗暴的使用了第一种方案。
再次通过brew命令安装cscope,这次成功。
~ $ brew install cscope
Warning: cscope 15.9 is already installed and up-to-date
To reinstall 15.9, run `brew reinstall cscope`
zdns@zdnsdeMacBook-Air local % brew reinstall cscope
==> Downloading https://homebrew.bintray.com/bottles/cscope-15.9.catalina.bottle.tar.gz
Already downloaded: /Users/zdns/Library/Caches/Homebrew/downloads/33f5b4cc8d183a0da943414e4f562ddcb878da0a534bdc20915e5a3a0ead63db--cscope-15.9.catalina.bottle.tar.gz
==> Reinstalling cscope
==> Pouring cscope-15.9.catalina.bottle.tar.gz
🍺 /usr/local/Cellar/cscope/15.9: 11 files, 716.0KB
~ $ cscope -h
Usage: cscope [-bcCdehklLqRTuUvV] [-f file] [-F file] [-i file] [-I dir] [-s dir]
[-p number] [-P path] [-[0-8] pattern] [source files]
-b Build the cross-reference only.
-C Ignore letter case when searching.
-c Use only ASCII characters in the cross-ref file (don't compress).
-d Do not update the cross-reference.
-e Suppress the <Ctrl>-e command prompt between files.
-F symfile Read symbol reference lines from symfile.
-f reffile Use reffile as cross-ref file name instead of cscope.out.
-h This help screen.
-I incdir Look in incdir for any #include files.
-i namefile Browse through files listed in namefile, instead of cscope.files
-k Kernel Mode - don't use /usr/include for #include files.
-L Do a single search with line-oriented output.
-l Line-oriented interface.
-num pattern Go to input field num (counting from 0) and find pattern.
-P path Prepend path to relative file names in pre-built cross-ref file.
-p n Display the last n file path components.
-q Build an inverted index for quick symbol searching.
-R Recurse directories for files.
-s dir Look in dir for additional source files.
-T Use only the first eight characters to match against C symbols.
-U Check file time stamps.
-u Unconditionally build the cross-reference file.
-v Be more verbose in line mode.
-V Print the version number.
Please see the manpage for more information.
~ $ which cscope
/usr/local/bin/cscope
~ $
至此,我们已经成功安装好了brew命令,并通过brew命令安装好了命令cscope,以完成对brew命令的测试。