Return to Course Home Page


conda installation and usage

coda installation and usage

Installing conda

Installing Miniconda on Windows:

  1. Download Miniconda Installer:
    • Go to the Miniconda download page.
    • Download the Miniconda installer for Windows. Make sure to choose the appropriate version (Python 3.x) based on your preference.
  2. Run the Installer:
    • Locate the downloaded installer and double-click on it to start the installation process.
    • Follow the on-screen instructions. Here are some recommendations for the installation:
      • Choose “Just Me” unless you’re sure about the “All Users” option.
      • Use the default installation location unless you have a specific need to change it.
      • It’s recommended to check the box “Add Miniconda to my PATH environment variable” for easier command-line use. However, be aware that this can interfere with other Python installations.
  3. Verify Installation:
    • Open the Command Prompt (you can search for cmd in the Windows search bar).
    • Type conda --version and press Enter. This should display the version of conda installed, confirming that the installation was successful.

Installing Miniconda on MacOS:

  1. Download Miniconda Installer:
    • Go to the Miniconda download page.
    • Download the Miniconda installer for MacOS. Make sure to choose the appropriate version (Python 3.x) based on your preference.
  2. Run the Installer:
    • Open a terminal.

    • Navigate to the directory where the installer was downloaded.

    • Make the installer executable with the command:

      chmod +x Miniconda3-latest-MacOSX-x86_64.sh
    • Start the installation process with:

      ./Miniconda3-latest-MacOSX-x86_64.sh
    • Follow the on-screen instructions to complete the installation.

  3. Verify Installation:
    • In the terminal, type:

      conda --version
    • This should display the version of conda installed, confirming that the installation was successful.

Installing Miniconda on Linux:

  1. Download Miniconda Installer:
    • Go to the Miniconda download page.
    • Download the Miniconda installer for Linux. Make sure to choose the appropriate version (Python 3.x) based on your preference.
  2. Run the Installer:
    • Open a terminal.

    • Navigate to the directory where the installer was downloaded.

    • Make the installer executable with the command:

      chmod +x Miniconda3-latest-Linux-x86_64.sh
    • Start the installation process with:

      ./Miniconda3-latest-Linux-x86_64.sh
    • Follow the on-screen instructions to complete the installation.

  3. Verify Installation:
    • In the terminal, type:

      conda --version
    • This should display the version of conda installed, confirming that the installation was successful.

Initializing Conda

Now that conda is installed, we just need to initialize it in our shell so it will work well with other programs (like the IDE we will soon install). The first step for intializing conda is to determine which shell (command line) your system uses. The process is different for each operating system:

Identifying your shell on Windows

On Windows, if you’re using the Command Prompt, your shell is cmd. If you’re using the newer Windows Terminal or PowerShell (you probably will be), it’s powershell.

However, if you’ve set up something like Git Bash, Cygwin, or the Windows Subsystem for Linux (WSL), then you might be using bash or another shell.

To check in PowerShell, you can use:

$PSVersionTable.PSVersion

Identifying your shell on a Mac

By default, newer Macs (since Catalina) use zsh as the default shell, while older versions use bash.

To determine which shell you’re using:

  1. Open the Terminal.
  2. Enter:
echo $SHELL

This command will display the path to the shell. The output might be something like /bin/zsh or /bin/bash.

Identifying your shell on Linux

Most Linux distributions come with bash as the default shell, but this can vary.

To determine the shell in use:

  1. Open your terminal.
  2. Enter:
echo $SHELL

Again, the output will show the path to the shell, like /bin/bash or /bin/zsh.

Initializing Conda for Your Shell:

Once you’ve identified your shell, you can initialize conda for it:

conda init <shell_name>

Replace <shell_name> with the name of the shell (bash, zsh, cmd, powershell, etc.)

Note: If for any reason conda is not recognized as a command, it’s possible the conda binary path isn’t in your system’s PATH variable. You’ll need to add it manually, and the process will depend on which OS and shell you’re using.

Updating conda

You won’t need to do this very often, but it’s a good idea to update conda periodically to ensure you have the latest version.

  1. Windows:
    1. Open the a PowerShell from the Start Menu.

    2. Run the following command to update conda:

      conda update conda
  2. MacOS/Linux:
    1. Open the Terminal.

    2. Run the following command to update conda:

      conda update conda

Working with conda environments

Creating a new conda environment

Conda can create new environments using a markup language specification called yaml. We will use an environment file created for our course to create an eds217_2023 environment on your local machine.

You can create a new environment using the following command:

    conda create -n my_cool_environment

This will create a new environment named my_cool_environment

Often you will want to specify the python that you’d like the environment to use. You do that by adding a python=<version>. Here is a command that would create an environment that had the python 3.10 interpreter installed:

    conda create -n my_py310_environment python=3.10

Finally, you probably noticed that conda prompts you for permission before installing new packages. Sometimes you need to pay attention to these prompts, especially when you are adding packages to very complex environments. But on creation, you may want to speed things up. The --yes flag allows you to bypass approval and go straight to installation:

    conda create --yes -n my_py310_environment python=3.10

Activating an environment

You can activate an environment using the following command:

    conda activate my_cool_environment

Deactivating an environment

You can deactivate an environment using the following command:

    conda deactivate

There is no need to deactivate an environment before activating a different environment.

Removing an environment (generally, you won’t want to do this!)

You can remove an environment using the following command:

    conda env remove -n my_cool_environment

Listing all available environments

Eventually you are going to have a lot of different environments, and you may not remember all of them, or the exact name of the one you are looking to activate. In this case, conda env list (or conda info --envs) is your friend.

    conda env list

This will display a list of all the conda environments you have created. The currently active environment (if any) will be marked with an asterisk (*) next to its name.

Installing the EDS217_2023 Python Environment.

Prerequisites:

  1. Ensure you have already installed conda (see above).

Instructions:

1. Fork the eds217_2023 Repository:

Go to the eds217_2023 repository and click the “Fork” button in the upper right corner of the page. This will create a copy of the repository in your GitHub account.

1. Clone Your Forked Repository:

Open a terminal (Command Prompt on Windows, Terminal on MacOS/Linux) and navigate to the directory where you want to clone the repository.

git clone https://github.com/YOUR_USERNAME/eds217_2023.git
cd eds217_2023

Replace YOUR_USERNAME with your GitHub username.

2. Create the Conda Environment:

Windows:

conda env create -f environment.yml
conda activate eds217_2023

MacOS/Linux:

conda env create -f environment.yml
source activate eds217_2023

3. Set Up the eds217_2023 Environment as an ipykernel:

In order to make sure our python environment is used when running code within an interactive editor such as a Jupyter notebook or in an IDE, you need ensure that the correct kernel is used to run our code. A python kernel is a program that runs and introspects the user’s code. Interactive tools such as Jupyter and IDEs use the kernel to execute code when you run a cell in the editor. `ipykernel`` is a tool that allows you to use specific python kernels when running code interactively within Jupyter notebooks and IDEs.

After activating the eds217_2023 environment, run the following command:

python -m ipykernel install --user --name eds217_2023 --display-name "Python (eds217_2023)"

This will allow you to select the eds217_2023 environment as a kernel in Jupyter or in your IDE, which we will install next.