Advertisement
J2897

install_pyenv

Sep 6th, 2024 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.59 KB | None | 0 0
  1. #! /usr/bin/bash
  2.  
  3. # ==============================================================================
  4. # This script installs pyenv for setting up multiple Python versions on Debian.
  5. #
  6. # Usage:
  7. # 1. Save this script to a file, e.g., install_pyenv.sh
  8. # 2. Make the script executable:
  9. #      chmod +x install_pyenv.sh
  10. # 3. Run the script:
  11. #      ./install_pyenv.sh
  12. #
  13. # After installation, restart your terminal or run:
  14. #      exec $SHELL
  15. # ==============================================================================
  16.  
  17. # Function to define color codes
  18. set_colors() {
  19.     GREEN='\033[0;32m'
  20.     RED='\033[0;31m'
  21.     NC='\033[0m' # No color
  22. }
  23.  
  24. # Call the function to set colors
  25. set_colors
  26.  
  27. # Update and install dependencies
  28. echo -e "${GREEN}Updating package lists and installing dependencies...${NC}"
  29. sudo apt update && sudo apt install -y make build-essential libssl-dev zlib1g-dev \
  30. libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
  31. libncurses5-dev libncursesw5-dev xz-utils tk-dev \
  32. libffi-dev liblzma-dev python3-openssl git || { echo -e "${RED}Failed to install dependencies.${NC}" >&2; exit 1; }
  33.  
  34. # Install pyenv
  35. echo -e "${GREEN}Installing pyenv...${NC}"
  36. if ! command -v pyenv >/dev/null 2>&1; then
  37.     curl https://pyenv.run | bash || { echo -e "${RED}Failed to install pyenv.${NC}" >&2; exit 1; }
  38. else
  39.     echo -e "${GREEN}pyenv is already installed.${NC}"
  40. fi
  41.  
  42. # Check and add pyenv to shell configuration
  43. echo -e "${GREEN}Configuring shell for pyenv...${NC}"
  44. if ! grep -q 'pyenv init' ~/.bashrc; then
  45.     {
  46.         echo -e "\n# pyenv configuration"
  47.         echo 'export PATH="$HOME/.pyenv/bin:$PATH"'
  48.         echo 'eval "$(pyenv init --path)"'
  49.         echo 'eval "$(pyenv init -)"'
  50.         echo 'eval "$(pyenv virtualenv-init -)"'
  51.     } >> ~/.bashrc
  52.     echo -e "${GREEN}Shell configuration updated.${NC}"
  53. else
  54.     echo -e "${GREEN}Shell already configured for pyenv.${NC}"
  55. fi
  56.  
  57. # Reload shell configuration
  58. echo -e "${GREEN}Reloading shell configuration...${NC}"
  59. source ~/.bashrc
  60.  
  61. # Re-apply colors after sourcing ~/.bashrc
  62. set_colors
  63.  
  64. # Instructions for manual Python installations
  65. echo -e "${GREEN}Installation complete.${NC}"
  66. echo -e "${GREEN}To install multiple versions of Python, run:${NC}"
  67. echo -e "${GREEN}  pyenv install <version>${NC}  (e.g., pyenv install 3.9.19)"
  68. echo -e "${GREEN}Then, to set a default global Python version, run:${NC}"
  69. echo -e "${GREEN}  pyenv global <version>${NC}  (e.g., pyenv global 3.12.5)"
  70. echo -e "${GREEN}Your shell has also been reloaded. So if pyenv isn't recognized, try opening a new terminal or running 'exec \$SHELL'.${NC}"
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement