/docs/nvim_setup_linux.sh

https://github.com/jdhao/nvim-config · Shell · 217 lines · 148 code · 37 blank · 32 comment · 35 complexity · f9aea5e73512aaa0561244231b6b2024 MD5 · raw file

  1. #!/bin/bash
  2. set -exu
  3. set -o pipefail
  4. # Whether python3 has been installed on the system
  5. PYTHON_INSTALLED=true
  6. # If Python has been installed, then we need to know whether Python is provided
  7. # by the system, or you have already installed Python under your HOME.
  8. SYSTEM_PYTHON=false
  9. # If SYSTEM_PYTHON is false, we need to decide whether to install
  10. # Anaconda (INSTALL_ANACONDA=true) or Miniconda (INSTALL_ANACONDA=false)
  11. INSTALL_ANACONDA=false
  12. # Whether to add the path of the installed executables to system PATH
  13. ADD_TO_SYSTEM_PATH=true
  14. # select which shell we are using
  15. USE_ZSH_SHELL=true
  16. USE_BASH_SHELL=false
  17. if [[ ! -d "$HOME/packages/" ]]; then
  18. mkdir -p "$HOME/packages/"
  19. fi
  20. if [[ ! -d "$HOME/tools/" ]]; then
  21. mkdir -p "$HOME/tools/"
  22. fi
  23. #######################################################################
  24. # Anaconda or miniconda install #
  25. #######################################################################
  26. if [[ "$INSTALL_ANACONDA" = true ]]; then
  27. CONDA_DIR=$HOME/tools/anaconda
  28. CONDA_NAME=Anaconda.sh
  29. CONDA_LINK="https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2021.05-Linux-x86_64.sh"
  30. else
  31. CONDA_DIR=$HOME/tools/miniconda
  32. CONDA_NAME=Miniconda.sh
  33. CONDA_LINK="https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh"
  34. fi
  35. if [[ ! "$PYTHON_INSTALLED" = true ]]; then
  36. echo "Installing Python in user HOME"
  37. SYSTEM_PYTHON=false
  38. echo "Downloading and installing conda"
  39. if [[ ! -f "$HOME/packages/$CONDA_NAME" ]]; then
  40. curl -Lo "$HOME/packages/$CONDA_NAME" $CONDA_LINK
  41. fi
  42. # Install conda silently
  43. if [[ -d $CONDA_DIR ]]; then
  44. rm -rf "$CONDA_DIR"
  45. fi
  46. bash "$HOME/packages/$CONDA_NAME" -b -p "$CONDA_DIR"
  47. # Setting up environment variables
  48. if [[ "$ADD_TO_SYSTEM_PATH" = true ]] && [[ "$USE_BASH_SHELL" = true ]]; then
  49. echo "export PATH=\"$CONDA_DIR/bin:\$PATH\"" >> "$HOME/.bash_profile"
  50. fi
  51. else
  52. echo "Python is already installed. Skip installing it."
  53. fi
  54. # Install some Python packages used by Nvim plugins.
  55. echo "Installing Python packages"
  56. declare -a py_packages=("pynvim" 'python-lsp-server[all]' "black" "vim-vint" "pyls-isort" "pylsp-mypy")
  57. if [[ "$SYSTEM_PYTHON" = true ]]; then
  58. echo "Using system Python to install $(PY_PACKAGES)"
  59. # If we use system Python, we need to install these Python packages under
  60. # user HOME, since we do not have permissions to install them under system
  61. # directories.
  62. for p in "${py_packages[@]}"; do
  63. pip install --user "$p"
  64. done
  65. else
  66. echo "Using custom Python to install $(PY_PACKAGES)"
  67. for p in "${py_packages[@]}"; do
  68. "$CONDA_DIR/bin/pip" install "$p"
  69. done
  70. fi
  71. #######################################################################
  72. # Install node and vim-language-server #
  73. #######################################################################
  74. NODE_DIR=$HOME/tools/nodejs
  75. NODE_SRC_NAME=$HOME/packages/nodejs.tar.gz
  76. # when download speed is slow, we can also use its mirror site: https://mirrors.ustc.edu.cn/node/v15.0.0/
  77. NODE_LINK="https://mirrors.ustc.edu.cn/node/v15.0.0/node-v15.0.0-linux-x64.tar.xz"
  78. if [[ -z "$(command -v node)" ]]; then
  79. echo "Install Nodejs"
  80. if [[ ! -f $NODE_SRC_NAME ]]; then
  81. echo "Downloading nodejs and renaming"
  82. wget $NODE_LINK -O "$NODE_SRC_NAME"
  83. fi
  84. if [[ ! -d "$NODE_DIR" ]]; then
  85. echo "Creating nodejs directory under tools directory"
  86. mkdir -p "$NODE_DIR"
  87. echo "Extracting to $HOME/tools/nodejs directory"
  88. tar xvf "$NODE_SRC_NAME" -C "$NODE_DIR" --strip-components 1
  89. fi
  90. if [[ "$ADD_TO_SYSTEM_PATH" = true ]] && [[ "$USE_BASH_SHELL" = true ]]; then
  91. echo "export PATH=\"$NODE_DIR/bin:\$PATH\"" >> "$HOME/.bash_profile"
  92. fi
  93. else
  94. echo "Nodejs is already installed. Skip installing it."
  95. fi
  96. # Install vim-language-server
  97. "$NODE_DIR/bin/npm" install -g vim-language-server
  98. #######################################################################
  99. # Ripgrep part #
  100. #######################################################################
  101. RIPGREP_DIR=$HOME/tools/ripgrep
  102. RIPGREP_SRC_NAME=$HOME/packages/ripgrep.tar.gz
  103. RIPGREP_LINK="https://hub.fastgit.org/BurntSushi/ripgrep/releases/download/12.0.0/ripgrep-12.0.0-x86_64-unknown-linux-musl.tar.gz"
  104. if [[ -z "$(command -v rg)" ]] && [[ ! -f "$RIPGREP_DIR/rg" ]]; then
  105. echo "Install ripgrep"
  106. if [[ ! -f $RIPGREP_SRC_NAME ]]; then
  107. echo "Downloading ripgrep and renaming"
  108. wget $RIPGREP_LINK -O "$RIPGREP_SRC_NAME"
  109. fi
  110. if [[ ! -d "$RIPGREP_DIR" ]]; then
  111. echo "Creating ripgrep directory under tools directory"
  112. mkdir -p "$RIPGREP_DIR"
  113. echo "Extracting to $HOME/tools/ripgrep directory"
  114. tar zxvf "$RIPGREP_SRC_NAME" -C "$RIPGREP_DIR" --strip-components 1
  115. fi
  116. if [[ "$ADD_TO_SYSTEM_PATH" = true ]] && [[ "$USE_BASH_SHELL" = true ]]; then
  117. echo "export PATH=\"$RIPGREP_DIR:\$PATH\"" >> "$HOME/.bash_profile"
  118. fi
  119. else
  120. echo "ripgrep is already installed. Skip installing it."
  121. fi
  122. #######################################################################
  123. # Ctags install #
  124. #######################################################################
  125. CTAGS_SRC_DIR=$HOME/packages/ctags
  126. CTAGS_DIR=$HOME/tools/ctags
  127. CTAGS_LINK="https://hub.fastgit.org/universal-ctags/ctags.git"
  128. if [[ ! -f "$CTAGS_DIR/bin/ctags" ]]; then
  129. echo "Install ctags"
  130. if [[ ! -d $CTAGS_SRC_DIR ]]; then
  131. mkdir -p "$CTAGS_SRC_DIR"
  132. else
  133. # Prevent an incomplete download.
  134. rm -rf "$CTAGS_SRC_DIR"
  135. fi
  136. git clone --depth=1 "$CTAGS_LINK" "$CTAGS_SRC_DIR" && cd "$CTAGS_SRC_DIR"
  137. ./autogen.sh && ./configure --prefix="$CTAGS_DIR"
  138. make -j && make install
  139. if [[ "$ADD_TO_SYSTEM_PATH" = true ]] && [[ "$USE_BASH_SHELL" = true ]]; then
  140. echo "export PATH=\"$CTAGS_DIR/bin:\$PATH\"" >> "$HOME/.bash_profile"
  141. fi
  142. else
  143. echo "ctags is already installed. Skip installing it."
  144. fi
  145. #######################################################################
  146. # Nvim install #
  147. #######################################################################
  148. NVIM_DIR=$HOME/tools/nvim
  149. NVIM_SRC_NAME=$HOME/packages/nvim-linux64.tar.gz
  150. NVIM_CONFIG_DIR=$HOME/.config/nvim
  151. NVIM_LINK="https://hub.fastgit.org/neovim/neovim/releases/download/nightly/nvim-linux64.tar.gz"
  152. if [[ ! -f "$NVIM_DIR/bin/nvim" ]]; then
  153. echo "Installing Nvim"
  154. echo "Creating nvim directory under tools directory"
  155. if [[ ! -d "$NVIM_DIR" ]]; then
  156. mkdir -p "$NVIM_DIR"
  157. fi
  158. if [[ ! -f $NVIM_SRC_NAME ]]; then
  159. echo "Downloading Nvim"
  160. wget "$NVIM_LINK" -O "$NVIM_SRC_NAME"
  161. fi
  162. echo "Extracting neovim"
  163. tar zxvf "$NVIM_SRC_NAME" --strip-components 1 -C "$NVIM_DIR"
  164. if [[ "$ADD_TO_SYSTEM_PATH" = true ]] && [[ "$USE_BASH_SHELL" = true ]]; then
  165. echo "export PATH=\"$NVIM_DIR/bin:\$PATH\"" >> "$HOME/.bash_profile"
  166. fi
  167. else
  168. echo "Nvim is already installed. Skip installing it."
  169. fi
  170. echo "Setting up config and installing plugins"
  171. if [[ -d "$NVIM_CONFIG_DIR" ]]; then
  172. mv "$NVIM_CONFIG_DIR" "$NVIM_CONFIG_DIR.backup"
  173. fi
  174. git clone --depth=1 https://hub.fastgit.org/jdhao/nvim-config.git "$NVIM_CONFIG_DIR"
  175. echo "Installing packer.nvim"
  176. git clone --depth=1 https://hub.fastgit.org/wbthomason/packer.nvim \
  177. ~/.local/share/nvim/site/pack/packer/start/packer.nvim
  178. echo "Installing plugins"
  179. "$NVIM_DIR/bin/nvim" +PackerInstall +qall
  180. echo "Finished installing Nvim and its dependencies!"