J2897 icon

install_pyenv

J2897 | PRO | 09/08/24 01:06:14 PM UTC (Edited) | 0 ⭐ | 677 👁️ | Never ⏰ | []
Bash |

2.59 KB

|

None

|

0 👍

/

0 👎

#! /usr/bin/bash
 
# ==============================================================================
# This script installs pyenv for setting up multiple Python versions on Debian.
#
# Usage:
# 1. Save this script to a file, e.g., install_pyenv.sh
# 2. Make the script executable:
#      chmod +x install_pyenv.sh
# 3. Run the script:
#      ./install_pyenv.sh
#
# After installation, restart your terminal or run:
#      exec $SHELL
# ==============================================================================
 
# Function to define color codes
set_colors() {
    GREEN='\033[0;32m'
    RED='\033[0;31m'
    NC='\033[0m' # No color
}
 
# Call the function to set colors
set_colors
 
# Update and install dependencies
echo -e "${GREEN}Updating package lists and installing dependencies...${NC}"
sudo apt update && sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncurses5-dev libncursesw5-dev xz-utils tk-dev \
libffi-dev liblzma-dev python3-openssl git || { echo -e "${RED}Failed to install dependencies.${NC}" >&2; exit 1; }
 
# Install pyenv
echo -e "${GREEN}Installing pyenv...${NC}"
if ! command -v pyenv >/dev/null 2>&1; then
    curl https://pyenv.run | bash || { echo -e "${RED}Failed to install pyenv.${NC}" >&2; exit 1; }
else
    echo -e "${GREEN}pyenv is already installed.${NC}"
fi
 
# Check and add pyenv to shell configuration
echo -e "${GREEN}Configuring shell for pyenv...${NC}"
if ! grep -q 'pyenv init' ~/.bashrc; then
    {
        echo -e "\n# pyenv configuration"
        echo 'export PATH="$HOME/.pyenv/bin:$PATH"'
        echo 'eval "$(pyenv init --path)"'
        echo 'eval "$(pyenv init -)"'
        echo 'eval "$(pyenv virtualenv-init -)"'
    } >> ~/.bashrc
    echo -e "${GREEN}Shell configuration updated.${NC}"
else
    echo -e "${GREEN}Shell already configured for pyenv.${NC}"
fi
 
# Reload shell configuration
echo -e "${GREEN}Reloading shell configuration...${NC}"
source ~/.bashrc
 
# Re-apply colors after sourcing ~/.bashrc
set_colors
 
# Instructions for manual Python installations
echo -e "${GREEN}Installation complete.${NC}"
echo -e "${GREEN}To install multiple versions of Python, run:${NC}"
echo -e "${GREEN}  pyenv install <version>${NC}  (e.g., pyenv install 3.9.19)"
echo -e "${GREEN}Then, to set a default global Python version, run:${NC}"
echo -e "${GREEN}  pyenv global <version>${NC}  (e.g., pyenv global 3.12.5)"
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}"
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎