linux非root用戶下安裝軟體,搭建生產環境

  • 2019 年 10 月 3 日
  • 筆記

之前的用實驗室的伺服器,因為某些原因,使用的用戶沒有root許可權。linux的非root用戶很多軟體無法安裝,非常的不方便。我的方法是使用brew來代替系統的包管理工具。brew是最先用在mac上的包管理工具,可以將所有的包託管在user本地的環境內。下面的文檔中運行的時候記得將用戶名改成自己的。

1. 安裝anaconda

下載官方anaconda python安裝包(minicondo也行),將conda加入~/.bashrc的系統變數中(在安裝中會提示你運行 conda init,點yes就會直接複製到~/.bashrc中了)。這一步的目的是去安裝curl, curl是下載安裝brew必備的工具,此時curl會安裝在/home/username/anaconda3/bin中

conda install curl --use-local

建議這裡裝完再裝一個vim,方便文本編輯

2. 安裝brew

使用curl命令下載安裝brew,中間有一些錯誤不用管他,結束後輸入brew有返回就說明安裝成功

sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

之後將一下brew加入~/.bashrc中

export HOMEBREW_PREFIX="/home/username/.linuxbrew";  export HOMEBREW_CELLAR="/home/username/.linuxbrew/Cellar";  export HOMEBREW_REPOSITORY="/home/username/.linuxbrew/Homebrew";  export PATH="/home/username/.linuxbrew/bin:/home/username/.linuxbrew/sbin:$PATH";  export MANPATH="/home/username/.linuxbrew/share/man:$MANPATH";  export INFOPATH="/home/username/.linuxbrew/share/info:$INFOPATH";

此時保證brew已經是一個可以在終端被調用, 接下來是關鍵的一步

在/home/username/.linuxbrew/Homebrew/Library/Homebrew/brew.sh#L200上進行加上一行

if [[ -n "$HOMEBREW_FORCE_BREWED_CURL" &&        -x "$HOMEBREW_PREFIX/opt/curl/bin/curl" ]] &&           "$HOMEBREW_PREFIX/opt/curl/bin/curl" --version >/dev/null  then    HOMEBREW_CURL="$HOMEBREW_PREFIX/opt/curl/bin/curl"  elif [[ -n "$HOMEBREW_DEVELOPER" && -x "$HOMEBREW_CURL_PATH" ]]  then    HOMEBREW_CURL="$HOMEBREW_CURL_PATH"  else    HOMEBREW_CURL="curl"  fi  HOMEBREW_CURL="/home/username/anaconda/bin/curl" # 加上這一行!

然後輸入 brew install curl

裝完之後brew.sh會自動抹去自己之前的修改 : ),然後brew就正式能用了

比如 brew install tmux, brew install htop,只有brew有的包都可以裝(參考包列表https://formulae.brew.sh/formula/)

3. 手動編譯安裝軟體包

如果brew裡面沒有某個包,需要自己編譯的話,一般的linux軟體包都是make編譯的,一般修改下configure然後再make && make install 就好了,如下三行命令:

./configure --prefix=/home/username/.local  make  make install  # 如果要卸載 運行 make uninstall

最後分享下我的~/.bashrc ,其中 ~/.local/bin~/.local/lib加到PATH裡面是為了自己編譯安裝用的。

export PATH=~/.local/bin:$PATH  export C_INCLUDE_PATH=$C_INCLUDE_PATH:~/.local/include  export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:~/.local/include  export LD_LIBRARY_PATH=~/.local/lib:$LD_LIBRARY_PATH      # >>> conda initialize >>>  # !! Contents within this block are managed by 'conda init' !!  __conda_setup="$('/home/username/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"  if [ $? -eq 0 ]; then      eval "$__conda_setup"  else      if [ -f "/home/username/anaconda3/etc/profile.d/conda.sh" ]; then          . "/home/username/anaconda3/etc/profile.d/conda.sh"      else          export PATH="/home/username/anaconda3/bin:$PATH"      fi  fi  unset __conda_setup  # <<< conda initialize <<<    export HOMEBREW_PREFIX="/home/username/.linuxbrew";  export HOMEBREW_CELLAR="/home/username/.linuxbrew/Cellar";  export HOMEBREW_REPOSITORY="/home/username/.linuxbrew/Homebrew";  export PATH="/home/username/.linuxbrew/bin:/home/username/.linuxbrew/sbin:$PATH";  export MANPATH="/home/username/.linuxbrew/share/man:$MANPATH";  export INFOPATH="/home/username/.linuxbrew/share/info:$INFOPATH";