Skip to content

Instantly share code, notes, and snippets.

Last active April 22, 2024 21:04
Show Gist options
  • Save juliasilge/035d54c59436604d6142ebebf29aa224 to your computer and use it in GitHub Desktop.
Save juliasilge/035d54c59436604d6142ebebf29aa224 to your computer and use it in GitHub Desktop.
Installing R + Tensorflow on M1

Install native ARM R:

Install RStudio (probably preview) which is not native as of today:

This blog post is helpful for getting Tensorflow working, but it is older, before the ARM version of R and the RStudio version that supports it was available.

This other blog post has specific steps outlined, but some of it has also been superseded.

Looks to me like Apple's instructions are the best as of today:

Here is what I think you need to do:

  • Make sure you have Python 3.8 or Python 3.9
  • Install Miniforge for ARM, using reticulate::install_miniconda() from within R
  • Create conda environment for your TensorFlow install (I called it tf_env and have had better luck doing it from the command line)
  • Install packages like numpy, pandas, etc for ARM (try it with conda install -c apple tensorflow-deps from Apple's instructions? is that good enough?)
  • Install base Tensorflow (I have not tried the tensorflow-metal yet)

You can turn it on (in the terminal) with conda activate tf_env and turn it off with conda deactivate. I have this in my .Renviron:

RETICULATE_PYTHON = "~/miniforge3/envs/tf_env/bin/python"
Copy link

t-kalinowski commented Dec 2, 2021

Here is a terminal snippet to do the above:

brew install --cask R
curl -OL
chmod +x
sh -b
source ~/miniforge3/bin/activate
conda update -y -n base conda
conda create -y --name tf-2.6 python=3.9
conda activate tf-2.6
conda install -y -c apple tensorflow-deps
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal
echo "export RETICULATE_PYTHON=~/miniforge3/env/tf-2.6/bin/python" >> ~/.Renviron

Copy link

hcarstens commented Apr 28, 2022

The 'terminal snippet' worked for me: Mac mini


Copy link

Hello - I cannot get tensorflow to work in R Studio. I am running a

I executed all of the steps in the terminal snippet successfully. I then started R Studio and selected what I thought was the correct python interpreter in R Studio via preferences: /usr/local/bin/python3.10 I also checked the "automatically activate ... " button. I also installed the reticulate package. When I then try to install tensorflow I get the following:

> tensorflow::install_tensorflow()
Installing: tensorflow-deps
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done

All requested packages already installed.

Installing: tensorflow-macos
Activated conda python: /usr/local/bin/bin/python
ERROR: Could not find a version that satisfies the requirement tensorflow-macos (from versions: none)
ERROR: No matching distribution found for tensorflow-macos
Error: Error installing package(s): "'tensorflow-macos'"

My environment is as follows:
Environment: macOS Version 12.3.1 Monterey R Studio: 2021.09.2 Build 382 platform x86_64-apple-darwin17.0
arch x86_64
os darwin17.0
system x86_64, darwin17.0
major 4
minor 1.2
year 2021
month 11
day 01
svn rev 81115
language R
version.string R version 4.1.2 (2021-11-01) nickname Bird Hippie

Any suggestions would be much appreciated - I have been trying to get tensorflow working on the mac for about a month now ...

Copy link

@pmgarafola Can you please file an issue on
The double 'bin/bin' in /usr/local/bin/bin/python indicates something is very wrong with your python setup.

In most situations, this is all you need these days to get Tensorflow working in reticulate on an M1 Mac:

remotes::install_github(sprintf("rstudio/%s", c("reticulate", "tensorflow", "keras")))
reticulate::miniconda_uninstall() # start with a blank slate

Copy link

hcarstens commented May 1, 2022 via email

Copy link

oh, thank you @hcarstens, good catch. It looks like the "tensorflow-macos" pip package is installed, but it fails because R and miniconda are running under Rosetta. @pmgarafola, you need to install the arm64 build of R

Copy link

ok, Installed the arm64 version of R. After repeating the steps above starting with Keras install still does not work. I wonder if this is a pathing issue or did I install the mac-os version of tensor-flow in the wrong place? I will file an issue per your request. Thank you for the help.

Copy link

denvercal1234GitHub commented Mar 21, 2023

Hi @t-kalinowski - Thanks for providing the steps above. I ran them successfully in my mac m1. And, I am now running some models in RStudio (ebecht/infinityFlow#13).

In my Activity Monitor, it does show GPU % below and when I tried to run some functions in R-Studio, it shows below. Does that means it is working, even though the warning "Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support" is still there?

Screenshot 2023-03-22 at 12 30 26

Screenshot 2023-03-22 at 12 31 07

Thank you for your help!

Related: rstudio/tensorflow#555

Copy link

If you review the issue on git hub cited below you will see that I was able to successfully install tensorflow on the mac using instructions provided in the issue thread, and the issue is closed.

Unable to install tensor-flow in R-Studio on m1 mac #530

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment