Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 632
Bob Avatar asked Dec 22 '20 21:12

Bob


People also ask

How do you specify a new environment location for conda?

Create a new conda environment from a list of specified packages. To use the newly-created environment, use 'conda activate envname'. This command requires either the -n NAME or -p PREFIXoption.

Does conda work on M1 Mac?

We are very excited to announce that this newest Anaconda Distribution release will, in addition to existing supported architectures, feature native ARM64 support for M1 Macs!


2 Answers

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
like image 134
isuruf Avatar answered Oct 19 '22 14:10

isuruf


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
like image 1
Lloyd Hamilton Avatar answered Oct 19 '22 13:10

Lloyd Hamilton