How to Set Up Conda Environment from environment.yml for AI for Beginners

Run conda env create --name ai4beg --file environment.yml to build the complete Python environment for Microsoft's AI curriculum, then activate with conda activate ai4beg.

The microsoft/AI-For-Beginners repository provides a pre-configured Conda environment that bundles all dependencies needed for the Jupyter notebooks, GPU-accelerated labs, and the Vue.js quiz application. Using the environment.yml file ensures you get version-locked packages that match the curriculum's tested configuration, avoiding common dependency conflicts between PyTorch, TensorFlow, and OpenCV.

Where to Find the environment.yml File

The master environment definition lives at [environment.yml](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) in the repository root.

According to the source, this YAML declares:

  • Channels – defaults, conda-forge, and pytorch (for GPU-accelerated deep learning)
  • Core dependencies – python, ipykernel, jupyter, numpy=1.26, matplotlib=3.9, scikit-learn, scipy=1.13, requests=2.32, setuptools
  • OpenCV – pulled from conda-forge for computer vision lessons
  • Pip integration – a pip: block that installs everything from [requirements.txt](https://github.com/microsoft/AI-For-Beginners/blob/main/requirements.txt), including TensorFlow 2.17 and Keras 3.5

The file is referenced in [lessons/0-course-setup/setup.md](https://github.com/microsoft/AI-For-Beginners/blob/main/lessons/0-course-setup/setup.md), the official setup guide for the course.

Step-by-Step: Creating the Conda Environment

1. Clone the Repository

git clone https://github.com/microsoft/AI-For-Beginners.git
cd AI-For-Beginners

If you already have the code, simply ensure environment.yml is in your working directory.

2. Create the Environment

conda env create --name ai4beg --file environment.yml

The --name ai4beg flag creates a predictably named environment that the curriculum's instructions reference throughout. The conda env create command:

  1. Resolves channels in priority order (defaults → conda-forge → pytorch)
  2. Installs all Conda-packaged dependencies
  3. Executes the embedded pip block to install TensorFlow, Keras, and other PyPI-only packages

This single command typically takes 5–15 minutes depending on download speed and whether CUDA toolkits are installed.

3. Activate the Environment

conda activate ai4beg

Activation puts the environment's Python interpreter and libraries on your $PATH and registers the ipykernel for Jupyter notebook integration.

4. Verify Installation

python -c "import torch, tensorflow; print('Torch:', torch.__version__, 'TF:', tensorflow.__version__)"

Expected output shows PyTorch and TensorFlow versions matching those pinned in requirements.txt. If you see import errors, check that activation succeeded (which python should point to your Conda environment path).

5. Launch Jupyter

jupyter notebook lessons/3-NeuralNetworks/05-Frameworks/IntroPyTorch.ipynb

All notebooks in lessons/ are configured to use the ai4beg kernel automatically once the environment is active.

Why the environment.yml Structure Matters

Channel Priority Prevents Conflicts

The YAML's channel order (defaults, conda-forge, pytorch) ensures packages with multiple builds resolve correctly. OpenCV, for example, is explicitly pulled from conda-forge rather than potentially outdated defaults builds.

Version Pinning Guarantees Reproducibility

Pinned versions like numpy=1.26, matplotlib=3.9, and scipy=1.13 prevent breaking changes from newer releases. This is critical for educational content where API shifts in TensorFlow or PyTorch would invalidate exercise code.

The Pip Block Bridges Ecosystems

The pip: section in environment.yml delegates to requirements.txt for packages not available in Conda channels:

  • TensorFlow 2.17 – Google's deep learning framework
  • Keras 3.5 – high-level neural network API
  • Additional utilities like tqdm, Pillow, and pandas

Installing these after Conda packages preserves correct dependency hierarchies—Conda manages compiled binaries (CUDA, MKL), while pip handles pure-Python machine learning libraries.

Complete Setup Script

#!/bin/bash

# Full environment setup for AI for Beginners

# Clone repository

git clone https://github.com/microsoft/AI-For-Beginners.git
cd AI-For-Beginners

# Create and activate environment

conda env create --name ai4beg --file environment.yml
conda activate ai4beg

# Verify key imports

python - <<'PY'
import torch
import tensorflow as tf
import numpy as np
import cv2

print(f"PyTorch: {torch.__version__}")
print(f"TensorFlow: {tf.__version__}")
print(f"NumPy: {np.__version__}")
print(f"OpenCV: {cv2.__version__}")
print("Environment ready for AI for Beginners")
PY

Troubleshooting Common Issues

Slow downloads or timeouts

Add the conda-forge channel explicitly and prefer it for heavy packages:

conda config --add channels conda-forge
conda config --set channel_priority flexible

CUDA/GPU not detected

The environment.yml installs CPU-only PyTorch by default. For GPU support, modify the file to include pytorch-cuda or install separately:

conda install pytorch torchvision torchtext torchdata pytorch-cuda=12.1 -c pytorch -c nvidia

Conflicting Python versions

Force a specific Python version before creating the environment:


# Add to environment.yml dependencies section

- python=3.11

Or create from a modified file:

conda env create --name ai4beg --file environment.yml python=3.11

Summary

  • Clone the microsoft/AI-For-Beginners repository to obtain environment.yml
  • Create the environment with conda env create --name ai4beg --file environment.yml
  • Activate with conda activate ai4beg before any work session
  • Verify with Python imports to confirm PyTorch and TensorFlow versions
  • The YAML channels (defaults, conda-forge, pytorch) and version pins ensure reproducible, conflict-free dependency resolution

Frequently Asked Questions

Can I use pip instead of Conda for AI for Beginners?

Yes, but Conda is strongly recommended. The environment.yml handles compiled dependencies like OpenCV and MKL-optimized NumPy that often fail with pure-pip installs. If you must use pip, install the requirements in [requirements.txt](https://github.com/microsoft/AI-For-Beginners/blob/main/requirements.txt) and manually resolve OpenCV and Jupyter kernel registration.

How do I update the environment when the curriculum changes?

Run conda env update --name ai4beg --file environment.yml --prune to sync with upstream changes. The --prune flag removes packages no longer listed. For major version bumps, it's safer to conda env remove --name ai4beg and recreate from scratch.

Does the environment support GPU training?

The base environment.yml installs CPU-only PyTorch to maximize compatibility. For NVIDIA GPU support, modify the file to include pytorch-cuda or install GPU-enabled PyTorch after environment creation using the pytorch and nvidia channels. The curriculum's notebooks will automatically use CUDA if available.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →