Skip to content

Commit e649806

Browse files
chore: Add script to create a Conda environment with latest Python version
1 parent 89fcf2c commit e649806

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
| [setup_ssh.sh](/setup/setup_ssh.sh) | Setup SSH on your machine and get the ip address of the machine. |
77
| [install_miniconda.sh](/setup/install_miniconda.sh) | Install Miniconda on your machine. |
88
| [install_docker.sh](/setup/install_docker.sh) | Install Docker on your machine. | [install_docker.sh](/setup/install_docker.sh) |
9+
| [create_conda_env.sh](/setup/create_conda_env.sh) | Create a conda environment with latest python version. |
10+

setup/create_conda_env.sh

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)