|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Using conda for Scientific Computing in Windows |
| 4 | +date: 2025-01-31 09:00:00 |
| 5 | +description: A quick start guide to setting up and using conda for reproducible scientific computing on Windows |
| 6 | +tags: tutorials computing-environment reproducibility conda |
| 7 | +categories: tutorials |
| 8 | +--- |
| 9 | + |
| 10 | + |
| 11 | +_This post was written for a graduate 8 week workshop "Gestión de Proyectos de Investigación y Ciencia Abierta" I'm teaching on Q1 of 2025._ |
| 12 | + |
| 13 | +In scientific computing and research, reproducibility is crucial. One of the main tools that helps us achieve this is `conda`, a powerful package and environment management system. This tutorial will guide you through setting up and using conda effectively on Windows, focusing on best practices for scientific computing. |
| 14 | + |
| 15 | +## Installing Miniconda |
| 16 | + |
| 17 | +Rather than installing the full Anaconda distribution, we'll use Miniconda - a lightweight alternative that gives us just what we need. The recommended installation method is through the command line interface (CLI) to practice those essential skills. The installation steps follow the official CLI tutorial from the official [documentation](https://docs.anaconda.com/miniconda/install/). |
| 18 | + |
| 19 | +### Steps |
| 20 | + |
| 21 | +1. Open Windows PowerShell and run these commands: |
| 22 | + |
| 23 | +```powershell |
| 24 | +wget "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" -outfile ".\miniconda.exe" |
| 25 | +Start-Process -FilePath ".\miniconda.exe" -ArgumentList "/S" -Wait |
| 26 | +del .\miniconda.exe |
| 27 | +``` |
| 28 | + |
| 29 | +2. Once installed, open the **Anaconda PowerShell Prompt** from your Start Menu |
| 30 | +3. Verify your installation by running: |
| 31 | + |
| 32 | +```bash |
| 33 | +conda --version |
| 34 | +``` |
| 35 | + |
| 36 | +You should see something like: |
| 37 | + |
| 38 | +```powershell |
| 39 | +(base) PS C:\Users\USER> conda --version |
| 40 | +conda 24.11.1 |
| 41 | +``` |
| 42 | + |
| 43 | +### Understanding Your Shell Prompt |
| 44 | + |
| 45 | +That `(base) PS C:\Users\USER>` line is your shell's prompt. Let's break it down: |
| 46 | + |
| 47 | +- `(base)`: Shows your active conda environment (base is default) |
| 48 | +- `PS`: Indicates you're in PowerShell |
| 49 | +- `C:\Users\USER>`: Shows your current directory |
| 50 | + |
| 51 | +## Managing Environments |
| 52 | + |
| 53 | +Environments are isolated spaces where you can install specific versions of packages without affecting other projects. Here's how to work with them: |
| 54 | + |
| 55 | +### Creating a New Environment |
| 56 | + |
| 57 | +```bash |
| 58 | +conda create --name my_env python=3.11 numpy scipy matplotlib |
| 59 | +``` |
| 60 | + |
| 61 | +This creates an environment called `my_env` with Python 3.11 and some essential scientific packages: `numpy`, `scipy` and `matplotlib`. |
| 62 | + |
| 63 | + |
| 64 | +### Activating and Deactivating |
| 65 | +The following commands are used to activate and desactivate the environment. Activating an environment means that exerucing |
| 66 | + |
| 67 | +```bash |
| 68 | +# Activate |
| 69 | +conda activate my_env |
| 70 | + |
| 71 | +# Deactivate when you're done |
| 72 | +conda deactivate |
| 73 | +``` |
| 74 | + |
| 75 | +Recall that the active environment is displayed in the prompt. If we had activated `(my_env)` the prompt would look like |
| 76 | + |
| 77 | +```powershell |
| 78 | +(my_env) PS C:\Users\USER> |
| 79 | +``` |
| 80 | + |
| 81 | +### Deleting environments |
| 82 | + |
| 83 | +If you wish to delete the environment `my_env` we created previously you first need to deactivate it (get out of it) with `conda deactivate`. Afterwards using the `conda remove` command you can delete it. For example: |
| 84 | + |
| 85 | +```bash |
| 86 | +# To delete the environment and all its associated packages |
| 87 | +conda remove --name my_env --all |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | +### Environment Management Commands |
| 92 | + |
| 93 | +```bash |
| 94 | +# List all environments |
| 95 | +conda env list |
| 96 | + |
| 97 | +# Export environment configuration |
| 98 | +conda env export > environment.yml |
| 99 | + |
| 100 | +# Recreate environment from file |
| 101 | +conda env create -f environment.yml |
| 102 | +``` |
| 103 | + |
| 104 | + |
| 105 | +## Package Management |
| 106 | + |
| 107 | +Managing packages in conda is straightforward: |
| 108 | + |
| 109 | +```bash |
| 110 | +# Install packages |
| 111 | +conda install pandas seaborn |
| 112 | + |
| 113 | +# Remove packages |
| 114 | +conda remove seaborn |
| 115 | + |
| 116 | +# Update packages |
| 117 | +conda update numpy |
| 118 | + |
| 119 | +# Search for packages |
| 120 | +conda search scikit-learn |
| 121 | +``` |
| 122 | + |
| 123 | +## Advanced Tips |
| 124 | + |
| 125 | +### Using mamba for Speed |
| 126 | + |
| 127 | +If you find conda slow, try mamba - it's a faster alternative: |
| 128 | + |
| 129 | +```bash |
| 130 | +# Install mamba |
| 131 | +conda install -n base -c conda-forge mamba |
| 132 | + |
| 133 | +# Use mamba instead of conda |
| 134 | +mamba install pandas |
| 135 | +``` |
| 136 | + |
| 137 | +### Setting Up conda-forge |
| 138 | + |
| 139 | +The conda-forge channel provides more up-to-date packages: |
| 140 | + |
| 141 | +```bash |
| 142 | +conda config --add channels conda-forge |
| 143 | +conda config --set channel_priority strict |
| 144 | +``` |
| 145 | + |
| 146 | +## Best Practices for Scientific Computing |
| 147 | + |
| 148 | +Although in the course we expanded a lot more on tools and practices, a quick overview of low hanging fruit recommendations to start implementing right away are: |
| 149 | + |
| 150 | +1. **Always use environment files**: Document your environment with `environment.yml` |
| 151 | +2. **Use specific versions**: Specify exact versions of critical packages like `matplotlib=1.4.3`. |
| 152 | +3. **Keep environments minimal**: Only install what you need |
| 153 | +4. **Document everything**: Include README files explaining your environment setup |
| 154 | +5. **Use version control**: Track your environment files using git, and when collaborating, GitHub. |
| 155 | + |
| 156 | + |
| 157 | +## Conclusion |
| 158 | + |
| 159 | +Using conda effectively is key to maintaining reproducible scientific computing workflows. By following these practices, you'll have a robust and reproducible environment for your research. |
| 160 | + |
| 161 | +## Additional Resources |
| 162 | + |
| 163 | +- [Official conda documentation](https://docs.conda.io/) |
| 164 | +- [conda-forge](https://conda-forge.org/) |
| 165 | +- [Miniconda installation guide](https://docs.anaconda.com/miniconda/) |
| 166 | + |
| 167 | +_Note: This tutorial was last updated on January 31, 2025. Please check the official documentation for the most recent information._ |
0 commit comments