|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to create a new Conda environment with the latest Python version |
| 4 | + |
| 5 | +# Check if the script is run with an argument for the environment name |
| 6 | +if [ "$#" -ne 1 ]; then |
| 7 | + echo "Usage: $0 <env_name>" |
| 8 | + exit 1 |
| 9 | +fi |
| 10 | + |
| 11 | +# Get the environment name from the command-line argument |
| 12 | +ENV_NAME="$1" |
| 13 | + |
| 14 | +USER_NAME=$(whoami) |
| 15 | + |
| 16 | +# Define potential Conda installation paths |
| 17 | +MINICONDA_PATH="/home/$USER_NAME/miniconda3" |
| 18 | +ANACONDA_PATH="/home/$USER_NAME/anaconda3" |
| 19 | + |
| 20 | +# Function to initialize Conda |
| 21 | +initialize_conda() { |
| 22 | + local conda_base="$1" |
| 23 | + if [ -d "$conda_base" ]; then |
| 24 | + echo "Initializing Conda from $conda_base..." |
| 25 | + . "$conda_base/etc/profile.d/conda.sh" |
| 26 | + return 0 |
| 27 | + else |
| 28 | + return 1 |
| 29 | + fi |
| 30 | +} |
| 31 | + |
| 32 | +# Check and initialize Conda from Miniconda or Anaconda |
| 33 | +if initialize_conda "$MINICONDA_PATH"; then |
| 34 | + CONDA_PATH="$MINICONDA_PATH" |
| 35 | +elif initialize_conda "$ANACONDA_PATH"; then |
| 36 | + CONDA_PATH="$ANACONDA_PATH" |
| 37 | +else |
| 38 | + echo "Neither Miniconda nor Anaconda found in the defined paths." |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +# Get the latest Python version available in Conda |
| 43 | +echo "Fetching the latest Python version..." |
| 44 | +LATEST_PYTHON_VERSION=$(conda search python | grep "python " | awk '{print $2}' | sort -V | tail -n 1) |
| 45 | + |
| 46 | +if [ -z "$LATEST_PYTHON_VERSION" ]; then |
| 47 | + echo "Failed to retrieve the latest Python version. Please check your Conda configuration." |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +# Create the Conda environment with the latest Python version |
| 52 | +echo "Creating Conda environment '$ENV_NAME' with Python $LATEST_PYTHON_VERSION..." |
| 53 | +conda create -y -n "$ENV_NAME" python="$LATEST_PYTHON_VERSION" |
| 54 | + |
| 55 | +# Activate the new environment |
| 56 | +echo "Activating the new environment..." |
| 57 | +conda activate "$ENV_NAME" |
| 58 | + |
| 59 | +# Display the Python version in the new environment |
| 60 | +echo "Python version in the new environment:" |
| 61 | +python --version |
| 62 | + |
| 63 | +# Display final message |
| 64 | +echo "Conda environment '$ENV_NAME' created and activated with Python $LATEST_PYTHON_VERSION." |
| 65 | + |
| 66 | +# Provide instructions for activating the environment |
| 67 | +echo "To activate this environment in the future, use:" |
| 68 | +echo "conda activate $ENV_NAME" |
0 commit comments