How to specify the architecture or platform for a new conda environment? (Apple Silicon)
Asked Answered
N

3

40

Is there a way to specify the architecture/platform when creating a new conda environment? Alternatively, how does conda detect its current architecture/platform when its run?

What I'm aiming to do is this: I'm running on an Apple Silicon laptop. My pre-existing environments are running fine through Rosetta2, but I'd like to start experimenting with python running natively on Apple Silicon. miniforge provides a conda-forge repository with Apple Silicon builds, and I can tell conda to use the conda-forge channel when I create an environment. But I'm not seeing a way to specify that I'd like this to be an arm64 environment rather than an x86_64 environment, other than starting from miniforge's installer.

Thanks in advance.

Nobody answered 22/12, 2020 at 21:26 Comment(4)
It should detect it from the OS.Contuse
I'm sure that's true, its detecting from the OS. But perhaps you could share precisely how its doing that, or where, or by what variable, or how that detection might be overridden?Nobody
You can try setting subdir constraints which is effectively how Conda segregates different platforms (win/osx/linux) and architectures (32/64).Maratha
@Maratha Thanks! That looks like its on the right path to an answer. I think I'd have to combine it with the conda commands to specify a channel and not to use channel fallbacks. May work. If you want to take your comment and turn it into a complete answer, I'll try it and select it as correct.Nobody
L
51

CONDA_SUBDIR=osx-arm64 conda create -n native numpy -c conda-forge will get you a osx-arm64 native env.

To make it permanent, do,

conda activate native 
conda config --env --set subdir osx-arm64
Leverrier answered 24/12, 2020 at 1:18 Comment(7)
Thanks! I got it to work with CONDA_SUBDIR=osx-arm64 conda create -n py39_native python=3.9 -c conda-forge --override-channels I'm pretty sure you need the last bit. If you want to add that to your answer I'll accept it as correct.Nobody
--override-channels is not strictly necessary. -c conda-forge is not needed if you have it already in your channels, but added it anyways.Leverrier
This should be run in the new environment after creation: conda env config vars set CONDA_SUBDIR=osx-arm64 Otherwise future installs will revert to osx_64Nobody
@Leverrier the --override-channels can safeguard against situations where users have other subdirs set in their configuration settings. Without it, the default channel_priority setting of "flexible", leaves users open to Conda solving for packages from outside the specified subdir. Alternatively, using "strict" priority for channels can also work. So yeah, not strictly necessary, but it is a good defensive suggestion.Maratha
@Nobody Thanks, this solved my problem.Industry
How do you specify rosetta2? osx-intel64?Greenock
It should be osx-64.Leverrier
U
12

How to configure python conda Environments for both arm64 and x86_64 on M1 Apple Silicon

Adding to the answers, it is possible to configure conda to use both osx-arm64(arm64) and osx-64(x86_64) architectures.

I found adding conda config --env --set subdir osx-arm64 changes the option globally which created issues for me. Some of my projects relied on python dependencies that were only supported in either one or the other architecture not both: specifically tensorflow.

Install Xcode:

xcode-select --install

Install miniforge3

Install miniforge3 by downloading the shell script from here: https://github.com/conda-forge/miniforge. Ensure you select arm64 (Apple Silicon) architecture. You may need to enable execution of the shell script with:

chmod +x Miniforge3-MacOSX-arm64.sh

Then execute is with:

sh Miniforge3-MacOSX-arm64.sh

Add shortcuts to ~/.zshrc or ~/.bashrc:

Add the following code to ~/.zshrc.

The code will add two shortcut functions to create either an osx-64 or osx-arm64 conda environment.

# Create x86 conda environment
create_x86_conda_environment () {

  # example usage: create_x86_conda_environment myenv_x86 python=3.9
  CONDA_SUBDIR=osx-64 conda create -n $@
  conda activate $1

}

# Create ARM conda environment
create_ARM_conda_environment () {

  # example usage: create_ARM_conda_environment myenv_x86 python=3.9
  CONDA_SUBDIR=osx-arm64 conda create -n $@
  conda activate $1

}

Create conda environment

Now to create a python 3.9.13 osx-64(x86_64) environment with the name env_x86:

create_x86_conda_environment env_x86 python=3.9.13

Alternatively for osx-arm64 (arm64) environment:

create_ARM_conda_environment env_ARM python3.9.13

Pip install packages

Once activated you can install packages accordingly. In my case I needed an arm64 environment to install tensorflow-macos.

conda install -c apple tensorflow-deps
pip install tensorflow-macos tensorflow-metal
Undying answered 24/9, 2022 at 17:23 Comment(0)
D
2

Prerequisites

  • Have Conda installed (anaconda / miniconda / miniforge3)
  • Have a terminal (Terminal or iTerm app) running with rosetta (for x64)

Add shortcut to create the environment

what does this do?

  • This function checks which architecture your current shell is using.
    • e.g. if you're running a terminal with rosetta, it will run with x86
  • Base on the architecture, it will add a prefix to your environment name
    • x86: x86_{name}
    • arm64: arm64_{name}
  • Creates the Environment & Activates it

function

add the following function to ~/.zshrc or ~/.bashrc:

create_conda_env () {
    arch_name="$(uname -m)"
    echo "Creating $arch_name environment"
    if [ "${arch_name}" = "x86_64" ]; then
        arch_type=osx-64
        env_prefix="x86_"
    elif [ "${arch_name}" = "arm64" ]; then
        arch_type=osx-arm64
        env_prefix="arm64_"
    else
        echo "Unknown architecture: $arch_name"
        exit -1
    fi
    echo "New environment name: $env_prefix$1"
    CONDA_SUBDIR=$arch_type conda create -n $env_prefix$@
    conda activate $env_prefix$1
}

How to use?

  • create_conda_env {name} python={version} {aditional packages (optional)}
  • replace name, version and add any additional packages
Decoteau answered 15/2, 2023 at 4:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.