Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Last active December 4, 2024 23:11
Show Gist options
  • Save MohamedElashri/a3e24837a147e0f3589ff09f293f17a8 to your computer and use it in GitHub Desktop.
Save MohamedElashri/a3e24837a147e0f3589ff09f293f17a8 to your computer and use it in GitHub Desktop.
ROOT CERN Conda Environment creation script

Setup ROOT Environment Script

This script automates the creation of a Conda environment for working with ROOT and Python, including optional configuration for additional packages, Conda channels, and Python versions. It also sets up environment variables for seamless integration with tools like VSCode and Jupyter Notebook.

Features

  • Automates Environment Creation: Creates a Conda environment with ROOT and Python.
  • Customizable Options: Allows specifying additional packages, Conda channels, and Python versions.
  • Jupyter Kernel Integration: Automatically registers the environment as a Jupyter kernel.
  • Environment Variable Management: Dynamically sets up and cleans up ROOT-specific environment variables.

Usage

1. Basic Usage

Run the script to create a default ROOT environment (root-env) with Python 3.9:

./setup_root_env.sh

2. Customize the Environment

You can specify optional arguments to customize the environment:

  • -e or --env: Specify a custom environment name (default: root-env).
  • -p or --packages: Add additional packages to install (comma-separated).
  • -c or --channels: Specify Conda channels to use (comma-separated, default: conda-forge).
  • -v or --python-version: Specify the Python version (default: 3.9).

Example:

./setup_root_env.sh --env my-env --python-version 3.8 --packages numpy,matplotlib --channels defaults,conda-forge

Options Summary

Option Description Default
-h, --help Display help message and exit -
-e, --env Conda environment name root-env
-p, --packages Additional packages to install (comma-separated) -
-c, --channels Conda channels to use (comma-separated) conda-forge
-v, --python-version Python version 3.9

After Running

Once the script completes:

  1. Activate the environment:
conda activate root-env

(Replace root-env with your custom environment name if specified.)

  1. Use Jupyter Notebook or VSCode:
  • The environment is registered as a Jupyter kernel named Python (root-env).
  1. Test ROOT:
python -c "import ROOT; print(ROOT.__version__)"
#!/bin/bash
# Define color codes
GREEN='\033[1;32m' # Info
YELLOW='\033[1;33m' # Warning
RED='\033[1;31m' # Error
NC='\033[0m' # No Color
# Helper functions to print messages
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
# Function to display help
print_help() {
echo -e "Usage: ./setup_root_env.sh [OPTIONS]"
echo -e ""
echo -e "Options:"
echo -e " -h, --help Show this help message and exit"
echo -e " -e, --env ENV_NAME Specify the name of the Conda environment to create (default: root-env)"
echo -e " -p, --packages PACKAGES Additional packages to install (comma-separated)"
echo -e " -c, --channels CHANNELS Conda channels to use (comma-separated, default: conda-forge)"
echo -e " -v, --python-version VERSION Specify the Python version to use (default: 3.9)"
}
# Default values
ENV_NAME="root-env"
PACKAGES=""
CHANNELS="conda-forge"
PYTHON_VERSION="3.9"
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
print_help
exit 0
;;
-e|--env)
ENV_NAME="$2"
shift 2
;;
-p|--packages)
PACKAGES="$2"
shift 2
;;
-c|--channels)
CHANNELS="$2"
shift 2
;;
-v|--python-version)
PYTHON_VERSION="$2"
shift 2
;;
*)
error "Unknown option: $1. Use -h or --help for usage."
;;
esac
done
# Check if Conda is installed
if ! command -v conda &> /dev/null; then
error "Conda is not installed. Please install Conda or Miniconda and try again."
fi
# Convert comma-separated lists to space-separated lists
IFS=',' read -r -a CHANNEL_LIST <<< "$CHANNELS"
CHANNEL_ARGS=""
for CHANNEL in "${CHANNEL_LIST[@]}"; do
CHANNEL_ARGS+="-c $CHANNEL "
done
IFS=',' read -r -a PACKAGE_LIST <<< "$PACKAGES"
# Step 1: Remove existing environment if requested
if conda info --envs | grep -qE "^\s*$ENV_NAME\s"; then
warn "Environment '$ENV_NAME' already exists."
read -p "Do you want to remove it and create a fresh environment? (y/N): " choice
if [[ "$choice" =~ ^[Yy]$ ]]; then
info "Removing existing environment '$ENV_NAME'..."
conda remove -n "$ENV_NAME" --all -y || error "Failed to remove environment '$ENV_NAME'."
info "Environment '$ENV_NAME' removed successfully."
else
info "Exiting without making changes."
exit 0
fi
fi
# Step 2: Create a new environment
info "Creating new Conda environment '$ENV_NAME' with ROOT and Python $PYTHON_VERSION..."
CREATE_CMD="conda create -n \"$ENV_NAME\" python=\"$PYTHON_VERSION\" root ${PACKAGE_LIST[@]} ${CHANNEL_ARGS} -y"
eval $CREATE_CMD || error "Failed to create environment '$ENV_NAME'."
# Step 3: Activate the environment
info "Activating the environment..."
# Ensure Conda is initialized
CONDA_BASE=$(conda info --base) # Get the base directory of Conda
source "$CONDA_BASE/etc/profile.d/conda.sh" || error "Failed to initialize Conda."
# Activate the environment
conda activate "$ENV_NAME" || error "Failed to activate environment '$ENV_NAME'."
# Step 4: Install Jupyter kernel
info "Installing Jupyter kernel..."
conda install ipykernel -y || error "Failed to install Jupyter kernel."
python -m ipykernel install --user --name="$ENV_NAME" --display-name "Python ($ENV_NAME)" || error "Failed to register Jupyter kernel."
# Step 5: Create activation and deactivation scripts
info "Setting up environment variables for ROOT..."
# Create activation script
ACTIVATE_SCRIPT="$CONDA_PREFIX/etc/conda/activate.d/env_vars.sh"
mkdir -p "$(dirname "$ACTIVATE_SCRIPT")"
cat > "$ACTIVATE_SCRIPT" <<EOF
export ROOTSYS=\$CONDA_PREFIX
export LD_LIBRARY_PATH=\$CONDA_PREFIX/lib:\$LD_LIBRARY_PATH
export PYTHONPATH=\$CONDA_PREFIX/lib:\$PYTHONPATH
EOF
# Create deactivation script
DEACTIVATE_SCRIPT="$CONDA_PREFIX/etc/conda/deactivate.d/env_vars.sh"
mkdir -p "$(dirname "$DEACTIVATE_SCRIPT")"
cat > "$DEACTIVATE_SCRIPT" <<EOF
unset ROOTSYS
unset LD_LIBRARY_PATH
unset PYTHONPATH
EOF
info "Environment variable scripts created successfully."
# Step 6: Test PyROOT
info "Testing PyROOT setup..."
python -c "import ROOT; print(ROOT.__version__)" &> /dev/null
if [[ $? -eq 0 ]]; then
info "PyROOT is working correctly."
else
error "PyROOT is not working. Please check the installation or environment setup."
fi
info "Environment '$ENV_NAME' has been successfully set up!"
info "You can now use it with VSCode or Jupyter Notebook. Select the 'Python ($ENV_NAME)' kernel."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment