如果網絡不行,安裝依賴包的速度小於 100k/s 或者丟包嚴重導致安裝很慢,我認為就應該使用國內源了。今天因為國內源的問題弄了很久,我覺得在國內服務器部署,全部應該從國內下載資源。分為兩種情況,
存在國內鏡像
沒什么好說的,搜索各種使用國內鏡像的參數或者弄成配置文件,隨服務器遷移
不存在國內鏡像,直接從國外的官網或者github上面下載
找找有沒有國內緩存的文件,比如 Phantomjs,使用 npm 下載會報錯,我就找到 淘寶npm鏡像地址,選擇一個版本,如2.1.1,然后使用 wget 下載到服務器,然后解壓,添加軟鏈接。
還有一類國內找不到,比如 tesseract,那就先使用 vpn 下載到本地,再上傳到國內雲(公司雲)上面,然后分享成公開鏈接,使用 curl 下載到服務器上面。
pip 使用國內源
在 pip 最后增加參數
- 使用清華源 --index https://pypi.tuna.tsinghua.edu.cn/simple
- 或者使用豆瓣源 --index https://pypi.douban.com/simple/
如果非要使用國外的網址,並且連接超時的話,可以增大超時時間嘗試一下
--default-timeout=100
關於配置文件路徑。我使用 macos,pip 中文檔說配置文件應該放在 $HOME/Library/Application Support/pip/pip.conf 里面,但是我不知道這個路徑指什么。於是放在 ~/.pip/pip.conf 里面,也能生效。
brew 使用國內源
使用 npm 安裝 phantomjs 遇到的問題:
沒有 npm remove -g phantomjs 直接安裝就會報錯
`Failed at the phantomjs@2.1.7 install script 'node install.js'.
3979 error Make sure you have the latest version of node.js and npm installed.`
但是這樣還是有問題,因為 npm -g 會錯誤,又會從官方源下載(牆外)。所以最終變成:
wget https://npm.taobao.org/mirrors/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2 && \ mkdir -p /opt/phantomjs && tar -xjvf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /opt/phantomjs/ && \ ln -s /opt/phantomjs/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/bin/phantomjs && \
rm -rf phantomjs-2.1.1-linux-x86_64
npm 使用國內源
增加參數
--registry=https://registry.npm.taobao.org
npm install -g cnpm --registry=https://registry.npm.taobao.org
文檔
參考
pip Config file
https://havee.me/mac/2014-05/individual-scheme-for-pip.html