install.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #!/bin/sh
  2. #
  3. # This script should be run via curl:
  4. # sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  5. # or via wget:
  6. # sh -c "$(wget -qO- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  7. # or via fetch:
  8. # sh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  9. #
  10. # As an alternative, you can first download the install script and run it afterwards:
  11. # wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
  12. # sh install.sh
  13. #
  14. # You can tweak the install behavior by setting variables when running the script. For
  15. # example, to change the path to the Oh My Zsh repository:
  16. # ZSH=~/.zsh sh install.sh
  17. #
  18. # Respects the following environment variables:
  19. # ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh)
  20. # REPO - name of the GitHub repo to install from (default: ohmyzsh/ohmyzsh)
  21. # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS)
  22. # BRANCH - branch to check out immediately after install (default: master)
  23. #
  24. # Other options:
  25. # CHSH - 'no' means the installer will not change the default shell (default: yes)
  26. # RUNZSH - 'no' means the installer will not run zsh after the install (default: yes)
  27. # KEEP_ZSHRC - 'yes' means the installer will not replace an existing .zshrc (default: no)
  28. #
  29. # You can also pass some arguments to the install script to set some these options:
  30. # --skip-chsh: has the same behavior as setting CHSH to 'no'
  31. # --unattended: sets both CHSH and RUNZSH to 'no'
  32. # --keep-zshrc: sets KEEP_ZSHRC to 'yes'
  33. # For example:
  34. # sh install.sh --unattended
  35. # or:
  36. # sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
  37. #
  38. set -e
  39. # Make sure important variables exist if not already defined
  40. #
  41. # $USER is defined by login(1) which is not always executed (e.g. containers)
  42. # POSIX: https://pubs.opengroup.org/onlinepubs/009695299/utilities/id.html
  43. USER=${USER:-$(id -u -n)}
  44. # Track if $ZSH was provided
  45. custom_zsh=${ZSH:+yes}
  46. # Default settings
  47. ZSH=${ZSH:-~/.oh-my-zsh}
  48. REPO=${REPO:-ohmyzsh/ohmyzsh}
  49. REMOTE=${REMOTE:-https://github.com/${REPO}.git}
  50. BRANCH=${BRANCH:-master}
  51. # Other options
  52. CHSH=${CHSH:-yes}
  53. RUNZSH=${RUNZSH:-yes}
  54. KEEP_ZSHRC=${KEEP_ZSHRC:-no}
  55. command_exists() {
  56. command -v "$@" >/dev/null 2>&1
  57. }
  58. user_can_sudo() {
  59. # Check if sudo is installed
  60. command_exists sudo || return 1
  61. # The following command has 3 parts:
  62. #
  63. # 1. Run `sudo` with `-v`. Does the following:
  64. # • with privilege: asks for a password immediately.
  65. # • without privilege: exits with error code 1 and prints the message:
  66. # Sorry, user <username> may not run sudo on <hostname>
  67. #
  68. # 2. Pass `-n` to `sudo` to tell it to not ask for a password. If the
  69. # password is not required, the command will finish with exit code 0.
  70. # If one is required, sudo will exit with error code 1 and print the
  71. # message:
  72. # sudo: a password is required
  73. #
  74. # 3. Check for the words "may not run sudo" in the output to really tell
  75. # whether the user has privileges or not. For that we have to make sure
  76. # to run `sudo` in the default locale (with `LANG=`) so that the message
  77. # stays consistent regardless of the user's locale.
  78. #
  79. ! LANG= sudo -n -v 2>&1 | grep -q "may not run sudo"
  80. }
  81. # The [ -t 1 ] check only works when the function is not called from
  82. # a subshell (like in `$(...)` or `(...)`, so this hack redefines the
  83. # function at the top level to always return false when stdout is not
  84. # a tty.
  85. if [ -t 1 ]; then
  86. is_tty() {
  87. true
  88. }
  89. else
  90. is_tty() {
  91. false
  92. }
  93. fi
  94. # This function uses the logic from supports-hyperlinks[1][2], which is
  95. # made by Kat Marchán (@zkat) and licensed under the Apache License 2.0.
  96. # [1] https://github.com/zkat/supports-hyperlinks
  97. # [2] https://crates.io/crates/supports-hyperlinks
  98. #
  99. # Copyright (c) 2021 Kat Marchán
  100. #
  101. # Licensed under the Apache License, Version 2.0 (the "License");
  102. # you may not use this file except in compliance with the License.
  103. # You may obtain a copy of the License at
  104. #
  105. # http://www.apache.org/licenses/LICENSE-2.0
  106. #
  107. # Unless required by applicable law or agreed to in writing, software
  108. # distributed under the License is distributed on an "AS IS" BASIS,
  109. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  110. # See the License for the specific language governing permissions and
  111. # limitations under the License.
  112. supports_hyperlinks() {
  113. # $FORCE_HYPERLINK must be set and be non-zero (this acts as a logic bypass)
  114. if [ -n "$FORCE_HYPERLINK" ]; then
  115. [ "$FORCE_HYPERLINK" != 0 ]
  116. return $?
  117. fi
  118. # If stdout is not a tty, it doesn't support hyperlinks
  119. is_tty || return 1
  120. # DomTerm terminal emulator (domterm.org)
  121. if [ -n "$DOMTERM" ]; then
  122. return 0
  123. fi
  124. # VTE-based terminals above v0.50 (Gnome Terminal, Guake, ROXTerm, etc)
  125. if [ -n "$VTE_VERSION" ]; then
  126. [ $VTE_VERSION -ge 5000 ]
  127. return $?
  128. fi
  129. # If $TERM_PROGRAM is set, these terminals support hyperlinks
  130. case "$TERM_PROGRAM" in
  131. Hyper|iTerm.app|terminology|WezTerm) return 0 ;;
  132. esac
  133. # kitty supports hyperlinks
  134. if [ "$TERM" = xterm-kitty ]; then
  135. return 0
  136. fi
  137. # Windows Terminal or Konsole also support hyperlinks
  138. if [ -n "$WT_SESSION" ] || [ -n "$KONSOLE_VERSION" ]; then
  139. return 0
  140. fi
  141. return 1
  142. }
  143. # Adapted from code and information by Anton Kochkov (@XVilka)
  144. # Source: https://gist.github.com/XVilka/8346728
  145. supports_truecolor() {
  146. case "$COLORTERM" in
  147. truecolor|24bit) return 0 ;;
  148. esac
  149. case "$TERM" in
  150. iterm |\
  151. tmux-truecolor |\
  152. linux-truecolor |\
  153. xterm-truecolor |\
  154. screen-truecolor) return 0 ;;
  155. esac
  156. return 1
  157. }
  158. fmt_link() {
  159. # $1: text, $2: url, $3: fallback mode
  160. if supports_hyperlinks; then
  161. printf '\033]8;;%s\a%s\033]8;;\a\n' "$2" "$1"
  162. return
  163. fi
  164. case "$3" in
  165. --text) printf '%s\n' "$1" ;;
  166. --url|*) fmt_underline "$2" ;;
  167. esac
  168. }
  169. fmt_underline() {
  170. is_tty && printf '\033[4m%s\033[24m\n' "$*" || printf '%s\n' "$*"
  171. }
  172. # shellcheck disable=SC2016 # backtick in single-quote
  173. fmt_code() {
  174. is_tty && printf '`\033[2m%s\033[22m`\n' "$*" || printf '`%s`\n' "$*"
  175. }
  176. fmt_error() {
  177. printf '%sError: %s%s\n' "${FMT_BOLD}${FMT_RED}" "$*" "$FMT_RESET" >&2
  178. }
  179. setup_color() {
  180. # Only use colors if connected to a terminal
  181. if ! is_tty; then
  182. FMT_RAINBOW=""
  183. FMT_RED=""
  184. FMT_GREEN=""
  185. FMT_YELLOW=""
  186. FMT_BLUE=""
  187. FMT_BOLD=""
  188. FMT_RESET=""
  189. return
  190. fi
  191. if supports_truecolor; then
  192. FMT_RAINBOW="
  193. $(printf '\033[38;2;255;0;0m')
  194. $(printf '\033[38;2;255;97;0m')
  195. $(printf '\033[38;2;247;255;0m')
  196. $(printf '\033[38;2;0;255;30m')
  197. $(printf '\033[38;2;77;0;255m')
  198. $(printf '\033[38;2;168;0;255m')
  199. $(printf '\033[38;2;245;0;172m')
  200. "
  201. else
  202. FMT_RAINBOW="
  203. $(printf '\033[38;5;196m')
  204. $(printf '\033[38;5;202m')
  205. $(printf '\033[38;5;226m')
  206. $(printf '\033[38;5;082m')
  207. $(printf '\033[38;5;021m')
  208. $(printf '\033[38;5;093m')
  209. $(printf '\033[38;5;163m')
  210. "
  211. fi
  212. FMT_RED=$(printf '\033[31m')
  213. FMT_GREEN=$(printf '\033[32m')
  214. FMT_YELLOW=$(printf '\033[33m')
  215. FMT_BLUE=$(printf '\033[34m')
  216. FMT_BOLD=$(printf '\033[1m')
  217. FMT_RESET=$(printf '\033[0m')
  218. }
  219. setup_ohmyzsh() {
  220. # Prevent the cloned repository from having insecure permissions. Failing to do
  221. # so causes compinit() calls to fail with "command not found: compdef" errors
  222. # for users with insecure umasks (e.g., "002", allowing group writability). Note
  223. # that this will be ignored under Cygwin by default, as Windows ACLs take
  224. # precedence over umasks except for filesystems mounted with option "noacl".
  225. umask g-w,o-w
  226. echo "${FMT_BLUE}Cloning Oh My Zsh...${FMT_RESET}"
  227. command_exists git || {
  228. fmt_error "git is not installed"
  229. exit 1
  230. }
  231. ostype=$(uname)
  232. if [ -z "${ostype%CYGWIN*}" ] && git --version | grep -q msysgit; then
  233. fmt_error "Windows/MSYS Git is not supported on Cygwin"
  234. fmt_error "Make sure the Cygwin git package is installed and is first on the \$PATH"
  235. exit 1
  236. fi
  237. # Manual clone with git config options to support git < v1.7.2
  238. git init "$ZSH" && cd "$ZSH" \
  239. && git config core.eol lf \
  240. && git config core.autocrlf false \
  241. && git config fsck.zeroPaddedFilemode ignore \
  242. && git config fetch.fsck.zeroPaddedFilemode ignore \
  243. && git config receive.fsck.zeroPaddedFilemode ignore \
  244. && git config oh-my-zsh.remote origin \
  245. && git config oh-my-zsh.branch "$BRANCH" \
  246. && git remote add origin "$REMOTE" \
  247. && git fetch --depth=1 origin \
  248. && git checkout -b "$BRANCH" "origin/$BRANCH" || {
  249. rm -rf "$ZSH"
  250. fmt_error "git clone of oh-my-zsh repo failed"
  251. exit 1
  252. }
  253. echo
  254. }
  255. setup_zshrc() {
  256. # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
  257. # with datestamp of installation that moved them aside, so we never actually
  258. # destroy a user's original zshrc
  259. echo "${FMT_BLUE}Looking for an existing zsh config...${FMT_RESET}"
  260. # Must use this exact name so uninstall.sh can find it
  261. OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
  262. if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
  263. # Skip this if the user doesn't want to replace an existing .zshrc
  264. if [ "$KEEP_ZSHRC" = yes ]; then
  265. echo "${FMT_YELLOW}Found ~/.zshrc.${FMT_RESET} ${FMT_GREEN}Keeping...${FMT_RESET}"
  266. return
  267. fi
  268. if [ -e "$OLD_ZSHRC" ]; then
  269. OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
  270. if [ -e "$OLD_OLD_ZSHRC" ]; then
  271. fmt_error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
  272. fmt_error "re-run the installer again in a couple of seconds"
  273. exit 1
  274. fi
  275. mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
  276. echo "${FMT_YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
  277. "${FMT_GREEN}Backing up to ${OLD_OLD_ZSHRC}${FMT_RESET}"
  278. fi
  279. echo "${FMT_YELLOW}Found ~/.zshrc.${FMT_RESET} ${FMT_GREEN}Backing up to ${OLD_ZSHRC}${FMT_RESET}"
  280. mv ~/.zshrc "$OLD_ZSHRC"
  281. fi
  282. echo "${FMT_GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${FMT_RESET}"
  283. # Replace $HOME path with '$HOME' in $ZSH variable in .zshrc file
  284. omz=$(echo "$ZSH" | sed "s|^$HOME/|\$HOME/|")
  285. sed "s|^export ZSH=.*$|export ZSH=\"${omz}\"|" "$ZSH/templates/zshrc.zsh-template" > ~/.zshrc-omztemp
  286. mv -f ~/.zshrc-omztemp ~/.zshrc
  287. echo
  288. }
  289. setup_shell() {
  290. # Skip setup if the user wants or stdin is closed (not running interactively).
  291. if [ "$CHSH" = no ]; then
  292. return
  293. fi
  294. # If this user's login shell is already "zsh", do not attempt to switch.
  295. if [ "$(basename -- "$SHELL")" = "zsh" ]; then
  296. return
  297. fi
  298. # If this platform doesn't provide a "chsh" command, bail out.
  299. if ! command_exists chsh; then
  300. cat <<EOF
  301. I can't change your shell automatically because this system does not have chsh.
  302. ${FMT_BLUE}Please manually change your default shell to zsh${FMT_RESET}
  303. EOF
  304. return
  305. fi
  306. echo "${FMT_BLUE}Time to change your default shell to zsh:${FMT_RESET}"
  307. # Prompt for user choice on changing the default login shell
  308. printf '%sDo you want to change your default shell to zsh? [Y/n]%s ' \
  309. "$FMT_YELLOW" "$FMT_RESET"
  310. read -r opt
  311. case $opt in
  312. y*|Y*|"") ;;
  313. n*|N*) echo "Shell change skipped."; return ;;
  314. *) echo "Invalid choice. Shell change skipped."; return ;;
  315. esac
  316. # Check if we're running on Termux
  317. case "$PREFIX" in
  318. *com.termux*) termux=true; zsh=zsh ;;
  319. *) termux=false ;;
  320. esac
  321. if [ "$termux" != true ]; then
  322. # Test for the right location of the "shells" file
  323. if [ -f /etc/shells ]; then
  324. shells_file=/etc/shells
  325. elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
  326. shells_file=/usr/share/defaults/etc/shells
  327. else
  328. fmt_error "could not find /etc/shells file. Change your default shell manually."
  329. return
  330. fi
  331. # Get the path to the right zsh binary
  332. # 1. Use the most preceding one based on $PATH, then check that it's in the shells file
  333. # 2. If that fails, get a zsh path from the shells file, then check it actually exists
  334. if ! zsh=$(command -v zsh) || ! grep -qx "$zsh" "$shells_file"; then
  335. if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -n 1) || [ ! -f "$zsh" ]; then
  336. fmt_error "no zsh binary found or not present in '$shells_file'"
  337. fmt_error "change your default shell manually."
  338. return
  339. fi
  340. fi
  341. fi
  342. # We're going to change the default shell, so back up the current one
  343. if [ -n "$SHELL" ]; then
  344. echo "$SHELL" > ~/.shell.pre-oh-my-zsh
  345. else
  346. grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
  347. fi
  348. echo "Changing your shell to $zsh..."
  349. # Check if user has sudo privileges to run `chsh` with or without `sudo`
  350. #
  351. # This allows the call to succeed without password on systems where the
  352. # user does not have a password but does have sudo privileges, like in
  353. # Google Cloud Shell.
  354. #
  355. # On systems that don't have a user with passwordless sudo, the user will
  356. # be prompted for the password either way, so this shouldn't cause any issues.
  357. #
  358. if user_can_sudo; then
  359. sudo -k chsh -s "$zsh" "$USER" # -k forces the password prompt
  360. else
  361. chsh -s "$zsh" "$USER" # run chsh normally
  362. fi
  363. # Check if the shell change was successful
  364. if [ $? -ne 0 ]; then
  365. fmt_error "chsh command unsuccessful. Change your default shell manually."
  366. else
  367. export SHELL="$zsh"
  368. echo "${FMT_GREEN}Shell successfully changed to '$zsh'.${FMT_RESET}"
  369. fi
  370. echo
  371. }
  372. # shellcheck disable=SC2183 # printf string has more %s than arguments ($FMT_RAINBOW expands to multiple arguments)
  373. print_success() {
  374. printf '%s %s__ %s %s %s %s %s__ %s\n' $FMT_RAINBOW $FMT_RESET
  375. printf '%s ____ %s/ /_ %s ____ ___ %s__ __ %s ____ %s_____%s/ /_ %s\n' $FMT_RAINBOW $FMT_RESET
  376. printf '%s / __ \\%s/ __ \\ %s / __ `__ \\%s/ / / / %s /_ / %s/ ___/%s __ \\ %s\n' $FMT_RAINBOW $FMT_RESET
  377. printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s / /_%s(__ )%s / / / %s\n' $FMT_RAINBOW $FMT_RESET
  378. printf '%s\\____/%s_/ /_/ %s /_/ /_/ /_/%s\\__, / %s /___/%s____/%s_/ /_/ %s\n' $FMT_RAINBOW $FMT_RESET
  379. printf '%s %s %s %s /____/ %s %s %s %s....is now installed!%s\n' $FMT_RAINBOW $FMT_GREEN $FMT_RESET
  380. printf '\n'
  381. printf '\n'
  382. printf "%s %s %s\n" "Before you scream ${FMT_BOLD}${FMT_YELLOW}Oh My Zsh!${FMT_RESET} look over the" \
  383. "$(fmt_code "$(fmt_link ".zshrc" "file://$HOME/.zshrc" --text)")" \
  384. "file to select plugins, themes, and options."
  385. printf '\n'
  386. printf '%s\n' "• Follow us on Twitter: $(fmt_link @ohmyzsh https://twitter.com/ohmyzsh)"
  387. printf '%s\n' "• Join our Discord community: $(fmt_link "Discord server" https://discord.gg/ohmyzsh)"
  388. printf '%s\n' "• Get stickers, t-shirts, coffee mugs and more: $(fmt_link "Planet Argon Shop" https://shop.planetargon.com/collections/oh-my-zsh)"
  389. printf '%s\n' $FMT_RESET
  390. }
  391. main() {
  392. # Run as unattended if stdin is not a tty
  393. if [ ! -t 0 ]; then
  394. RUNZSH=no
  395. CHSH=no
  396. fi
  397. # Parse arguments
  398. while [ $# -gt 0 ]; do
  399. case $1 in
  400. --unattended) RUNZSH=no; CHSH=no ;;
  401. --skip-chsh) CHSH=no ;;
  402. --keep-zshrc) KEEP_ZSHRC=yes ;;
  403. esac
  404. shift
  405. done
  406. setup_color
  407. if ! command_exists zsh; then
  408. echo "${FMT_YELLOW}Zsh is not installed.${FMT_RESET} Please install zsh first."
  409. exit 1
  410. fi
  411. if [ -d "$ZSH" ]; then
  412. echo "${FMT_YELLOW}The \$ZSH folder already exists ($ZSH).${FMT_RESET}"
  413. if [ "$custom_zsh" = yes ]; then
  414. cat <<EOF
  415. You ran the installer with the \$ZSH setting or the \$ZSH variable is
  416. exported. You have 3 options:
  417. 1. Unset the ZSH variable when calling the installer:
  418. $(fmt_code "ZSH= sh install.sh")
  419. 2. Install Oh My Zsh to a directory that doesn't exist yet:
  420. $(fmt_code "ZSH=path/to/new/ohmyzsh/folder sh install.sh")
  421. 3. (Caution) If the folder doesn't contain important information,
  422. you can just remove it with $(fmt_code "rm -r $ZSH")
  423. EOF
  424. else
  425. echo "You'll need to remove it if you want to reinstall."
  426. fi
  427. exit 1
  428. fi
  429. setup_ohmyzsh
  430. setup_zshrc
  431. setup_shell
  432. print_success
  433. if [ $RUNZSH = no ]; then
  434. echo "${FMT_YELLOW}Run zsh to try it out.${FMT_RESET}"
  435. exit
  436. fi
  437. exec zsh -l
  438. }
  439. main "$@"