Using pip install on MAC often reports ERROR: Clang error. The reason is that the default gcc of MAC is 4.2, and many existing software installations require gcc4.9 or above. Knowing the reason, you need to upgrade the gcc that comes with the MAC.
1. First check the gcc version of the machine (my MAC has been upgraded, so it shows version 7.5)
gcc -v
2. Turn off the SIP protection of MAC
- Restart the MAC, press and hold command+R, enter recovery mode
- Click Utilities-Terminal, enter the following command to close SIP
csrutil disable
- restart mac
3. Install gcc through brew
//先安装brew
/bin/bash -c "$(curl -fsSL https://gitee.com/igeting/HomebrewCN/raw/master/Homebrew.sh)"
//替换brew源为国内源(示例中更新为USTC)
//替换Homebrew源
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
//替换homebrew-core源
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
//替换homebrew-cask源
cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
//更新brew
brew update
//查找brew下相关的gcc版本
brew search gcc
//安装所需版本的gcc(我这里安装的是7,实际中根据自己需要安装)
brew install gcc@7
//验证gcc安装
gcc-7 -v
4. Open the ROOT permission of MAC
- Click Settings-Users and Groups, click the small lock icon in the lower left corner, and enter the user password to unlock
- Select the login option - join - open the utility, click the small lock icon in the lower left corner, enter the user password to unlock
- Click Edit at the top - enable the root user, set the root user password OK
- Login with root account after logout
5. Modify the default gcc soft link
//挂载根目录可读写
sudo mount -uw /
//备份现有的gcc
mv cc cc.bak
mv c++ c++.bak
mv gcc gcc.bak
mv g++ g++.bak
mv cpp cpp.bak
//将安装好的gcc-7软链到现有目录
ln -s /usr/local/Cellar/gcc@7/7.5.0_4/bin/c++-7 /usr/bin/c++
ln -s /usr/local/Cellar/gcc@7/7.5.0_4/bin/g++-7 /usr/bin/g++
ln -s /usr/local/Cellar/gcc@7/7.5.0_4/bin/gcc-7 /usr/bin/gcc
ln -s /usr/local/Cellar/gcc@7/7.5.0_4/bin/gcc-7 /usr/bin/cc
6. Modify environment variables
bash environment
vi ~/.bash_profile
PATH="/usr/local/Cellar/gcc@7/7.5.0_4/bin:/usr/local/Cellar/gcc@7/7.5.0_4/lib:${PATH}"
export PATH
zsh environment
vi ~/.zshrc
PATH="/usr/local/Cellar/gcc@7/7.5.0_4/bin:/usr/local/Cellar/gcc@7/7.5.0_4/lib:${PATH}"
export PATH
7. Verify installation
gcc -v
8. Close ROOT authority and reopen SIP
Close ROOT permission
- Click Settings-Users and Groups, click the small lock icon in the lower left corner, and enter the user password to unlock
- Select the login option - join - open the utility, click the small lock icon in the lower left corner, enter the user password to unlock
- Click Edit at the top - disable root user
Re-enable SIP
- Restart the MAC, press and hold command+R, enter recovery mode
- Click Utilities-Terminal, enter the following command to enable SIP
csrutil enable
9. At this point, the MAC upgrade of gcc is successfully completed
In the subsequent pip install, if there is an error, it is recommended to run pip install xxx separately. After the installation is successful, modify the corresponding library version in requirements.txt to already installed.