The Ultimate Python and Tensorflow Set-Up Guide for Apple Silicon Macs (M1 & M2) | by Pol Marin | Feb, 2023


Step-by-step guide

Installing TensorFlow and Python on ARM Macs was never easier

Photo by Ales Nesetril on Unsplash

I switched jobs one year ago and I found myself with a brand new M1 MacBook Pro to work with. I had to install everything a data scientist needs, which was a real pain.

To make things worse, it turns out that I didn’t set it up properly and it was chaos.

But a coworker shared a set of steps and, after following them and being able to set it all up successfully, I thought I could share it with you all. So I summarized and expanded it with extra info and steps for completeness. As I haven’t found anything like this on the Internet, or at least when I searched, I decided to make it public so everyone can benefit from it.

In this story, you’ll find a step-by-step guide on how to successfully install Python and Tensorflow in M1 and M2 Macs without going through the pain of trying to set it all up on your own.

The workflow is relatively straightforward:

  1. We’ll first install the basic requirements any developer needs on a Mac (if you’re not using a new Mac, chances are that you already have those covered).
  2. Then move on to the Python installation using pyenv.
  3. Finally, install and set up Tensorflow properly for an M1 or M2 Mac.

All you need is an ARM Mac and you’re ready to go!

Macs nowadays already come with Python installed, at least Python2, but I believe there are better and recommended ways of working with Python in an arm64 like your M1 or M2 MacBook.

XCode Command line tools

First, you’ll need to install XCode Command line tools:

~> xcode-select --install
~> xcrun --show-sdk-path
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk

These tools are for software developers that run on the command line, in the Terminal application. Since before Apple’s beginnings, this assortment of Unix-based tools has been the foundation of almost all software development.

Homebrews

Once we have the command line tools installed, we’ll need to install two homebrews. Why two? We want one native (Apple Silicon) and one for Intel (also named Rosetta). To do so, we’ll have to have one Terminal/iTerm2 for each architecture. It’s easy.

I recommend using iTerm2[1] as it is an improved terminal for macOS, but it works the same way if you prefer to stick to the built-in terminal.

Go to your Applications folder, duplicate the “iTerm” and rename the copy to “intel iTerm” (or similar). For that new app, right-click and select “Get Info”. In the General section, check “Open using Rosetta”.

Now we have to set up Homebrew [2].

From the Intel Terminal, run:

~> arch --x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

If you don’t know what terminal you’re using, use uname -m and see the output:

– If you see x86_64, then you’re in the Intel/Rosetta one.

– But if the output shows arm64, you’re in the native one (M1/M2).

Now open the native Terminal (the default iTerm or Terminal), and run the next set of instructions:

~> cd ~/Downloads
~> mkdir homebrew
~> curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
~> sudo mv homebrew /opt/homebrew

Now check, on both terminals, if they are both properly installed.

~> brew --prefix
/usr/local
~> brew list
<list of installed packages>
~> brew --prefix
/opt/homebrew
~> brew list
<list of installed packages (different than x86_64)>

Now, to properly manage both environments, we need to set them up properly. Just add this configuration in your ~/.zshrc file:

... rest of the file above ...

#
# homebrew setup, following https://noahpeeters.de/posts/apple-silicon/homebrew-setup/
#
if [ -d "/opt/homebrew/bin" ]; then
export PATH="/opt/homebrew/bin:$PATH"
fi

function ibrew() {
arch --x86_64 /usr/local/bin/brew $@
}

# variables needed to properly install things under intel or m1

ARCH="$(uname -m)"
case ARCH in
i386) ARCH="386" ;;
i686) ARCH="386" ;;
x86_64) ARCH="amd64" ;;
arm) dpkg --print-architecture | grep -q "arm64" && ARCH="arm64" || ARCH="arm" ;;
esac

if [[ "${ARCH}" == "arm64" ]]; then
PREFIX="/opt/homebrew"
else
PREFIX="/usr/local"
fi

# https://github.com/pyenv/pyenv/issues/1768
SDK_PATH="$(xcrun --show-sdk-path)"

echo $PREFIX
echo $SDK_PATH

export CPATH="${SDK_PATH}/usr/include"
export CFLAGS="-I${SDK_PATH}/usr/include/sasl $CFLAGS"
export CFLAGS="-I${SDK_PATH}/usr/include $CFLAGS"
export CFLAGS="-I${PREFIX}/include $CFLAGS"
export LDFLAGS="-L${SDK_PATH}/usr/lib $LDFLAGS"
export LDFLAGS="-L${PREFIX}/lib $LDFLAGS"
export LDFLAGS="-L${PREFIX}/opt/openssl/lib $LDFLAGS"
export CPPFLAGS="-I${PREFIX}/opt/openssl/include $CPPFLAGS"

For brew to work properly, only the first part is needed… But the other flags added afterward will probably be helpful (maybe needed?) as well.

For the record: we’ll always try to install packages using the native brew first. Only if some package fails to install and after being sure it’s not able through the native version, then we’ll use the Intel brew.

Pyenv

Ok so now we are in a position to start installing Python-related stuff. We’ll start with pyenv, which will make the process of installing various Python versions extremely smooth.

Open your native terminal and simply type the command below:

~> brew install pyenv

Build dependencies

For pyenv to work properly, we need to install several dependencies[3]. In our case:

~> brew install openssl readline sqlite3 xz zlib tcl-tk libffi

Additionally, we want to add the same dependencies in our intel homebrew so:

~> ibrew install openssl readline sqlite3 xz zlib tcl-tk libffi

At this point, you can already start installing Python versions as you want. Here’s an example you’d use if you wanted to install Python 3.9.9 and set it as default:

~> pyenv install 3.9.9
~> pyenv global 3.9.9

And, if you want to see your currently installed versions, just use pyenv versions.

Because with this kind of process a double check is always worth it, so run the next command: ~/.pyenv/versions/3.9.9/bin/python replacing 3.9.9 with the version you decided to install and set as global. Once in the shell, try to import ctypes:

Python 3.9.9 (main, Feb 15 2023, 11:25:42)
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>>

If it doesn’t break, then it’s properly installed.

Pipenv

While having it installed isn’t mandatory, it’s always a good practice to use environments for your projects. That’s why I also decided to put it in here because it’s easy and good practice. To install it:

~> brew install pipenv

And now you can just use it in any project as you will. You can check the basic usage of pipenv on their official documentation[4] or just find online tutorials.

On to the second part now. Installing TensorFlow on an M1 or M2 Mac has been a nightmare for many of us, but it doesn’t have to anymore.

You will recall that we edited the ~/.zshrc file a while ago. Now, we’ll add this to the end:

# tensorflow grpcio https://github.com/grpc/grpc/issues/25082
export GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1
export GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1
export CFLAGS="-I/opt/homebrew/opt/openssl/include"
export LDFLAGS="-L/opt/homebrew/opt/openssl/lib"

Once that’s added, we’ll have to download miniconda right into our ~/ directory. You can directly download it from https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh and manually move it to the home folder.

Once downloaded, my advice is to quit and reopen the native terminal and then run:

~> bash ~/Miniconda3-latest-MacOSX-arm64.sh -b -p $HOME/miniconda

Now that we have conda installed, we’ll activate the environment and install some dependencies for Tensorflow:

~> source ~/miniconda/bin/activate
(base) ~> conda install -c apple tensorflow-deps

Again, restart your terminal by quitting (Cmd + Q) and reopening it, and you can now install Tensorflow. I’ll do it in a dedicated environment this way:

  1. Go to your project folder: for examplecd Documents/project
  2. Activate the environment: pipenv shell
  3. Install Tensorflow: pipenv install tensorflow-macos

Et voilà! You should now be ready to use TensorFlow properly on your M1 or M2 Mac.

See how there’s a package that I installed called tensorflow-metal[5] to accelerate the training of our models in our Mac’s GPU so you could consider installing it with

~> pipenv install tensorflow-metal

Now you know how to go through the pain of setting up a brand new Mac for data science and get the most out of its new chips.

I hope this was helpful enough and any doubts or problems you might encounter, feel free to comment on this story — I or another expert will surely help you.

I don’t want to leave without kindly thanking you for going through my story. I frequently post on Medium and, if you enjoyed this post, feel free to follow me. It helps a lot.

[1] iTerm2 — macOS Terminal Replacement

[2] Noah Peters, Setup Homebrew on Apple Silicon

[3] Pyenv wiki

[4] Basic Usage of Pipenv

[5] Official PyPi — Tensorflow-metal package


Step-by-step guide

Installing TensorFlow and Python on ARM Macs was never easier

Photo by Ales Nesetril on Unsplash

I switched jobs one year ago and I found myself with a brand new M1 MacBook Pro to work with. I had to install everything a data scientist needs, which was a real pain.

To make things worse, it turns out that I didn’t set it up properly and it was chaos.

But a coworker shared a set of steps and, after following them and being able to set it all up successfully, I thought I could share it with you all. So I summarized and expanded it with extra info and steps for completeness. As I haven’t found anything like this on the Internet, or at least when I searched, I decided to make it public so everyone can benefit from it.

In this story, you’ll find a step-by-step guide on how to successfully install Python and Tensorflow in M1 and M2 Macs without going through the pain of trying to set it all up on your own.

The workflow is relatively straightforward:

  1. We’ll first install the basic requirements any developer needs on a Mac (if you’re not using a new Mac, chances are that you already have those covered).
  2. Then move on to the Python installation using pyenv.
  3. Finally, install and set up Tensorflow properly for an M1 or M2 Mac.

All you need is an ARM Mac and you’re ready to go!

Macs nowadays already come with Python installed, at least Python2, but I believe there are better and recommended ways of working with Python in an arm64 like your M1 or M2 MacBook.

XCode Command line tools

First, you’ll need to install XCode Command line tools:

~> xcode-select --install
~> xcrun --show-sdk-path
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk

These tools are for software developers that run on the command line, in the Terminal application. Since before Apple’s beginnings, this assortment of Unix-based tools has been the foundation of almost all software development.

Homebrews

Once we have the command line tools installed, we’ll need to install two homebrews. Why two? We want one native (Apple Silicon) and one for Intel (also named Rosetta). To do so, we’ll have to have one Terminal/iTerm2 for each architecture. It’s easy.

I recommend using iTerm2[1] as it is an improved terminal for macOS, but it works the same way if you prefer to stick to the built-in terminal.

Go to your Applications folder, duplicate the “iTerm” and rename the copy to “intel iTerm” (or similar). For that new app, right-click and select “Get Info”. In the General section, check “Open using Rosetta”.

Now we have to set up Homebrew [2].

From the Intel Terminal, run:

~> arch --x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

If you don’t know what terminal you’re using, use uname -m and see the output:

– If you see x86_64, then you’re in the Intel/Rosetta one.

– But if the output shows arm64, you’re in the native one (M1/M2).

Now open the native Terminal (the default iTerm or Terminal), and run the next set of instructions:

~> cd ~/Downloads
~> mkdir homebrew
~> curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
~> sudo mv homebrew /opt/homebrew

Now check, on both terminals, if they are both properly installed.

~> brew --prefix
/usr/local
~> brew list
<list of installed packages>
~> brew --prefix
/opt/homebrew
~> brew list
<list of installed packages (different than x86_64)>

Now, to properly manage both environments, we need to set them up properly. Just add this configuration in your ~/.zshrc file:

... rest of the file above ...

#
# homebrew setup, following https://noahpeeters.de/posts/apple-silicon/homebrew-setup/
#
if [ -d "/opt/homebrew/bin" ]; then
export PATH="/opt/homebrew/bin:$PATH"
fi

function ibrew() {
arch --x86_64 /usr/local/bin/brew $@
}

# variables needed to properly install things under intel or m1

ARCH="$(uname -m)"
case ARCH in
i386) ARCH="386" ;;
i686) ARCH="386" ;;
x86_64) ARCH="amd64" ;;
arm) dpkg --print-architecture | grep -q "arm64" && ARCH="arm64" || ARCH="arm" ;;
esac

if [[ "${ARCH}" == "arm64" ]]; then
PREFIX="/opt/homebrew"
else
PREFIX="/usr/local"
fi

# https://github.com/pyenv/pyenv/issues/1768
SDK_PATH="$(xcrun --show-sdk-path)"

echo $PREFIX
echo $SDK_PATH

export CPATH="${SDK_PATH}/usr/include"
export CFLAGS="-I${SDK_PATH}/usr/include/sasl $CFLAGS"
export CFLAGS="-I${SDK_PATH}/usr/include $CFLAGS"
export CFLAGS="-I${PREFIX}/include $CFLAGS"
export LDFLAGS="-L${SDK_PATH}/usr/lib $LDFLAGS"
export LDFLAGS="-L${PREFIX}/lib $LDFLAGS"
export LDFLAGS="-L${PREFIX}/opt/openssl/lib $LDFLAGS"
export CPPFLAGS="-I${PREFIX}/opt/openssl/include $CPPFLAGS"

For brew to work properly, only the first part is needed… But the other flags added afterward will probably be helpful (maybe needed?) as well.

For the record: we’ll always try to install packages using the native brew first. Only if some package fails to install and after being sure it’s not able through the native version, then we’ll use the Intel brew.

Pyenv

Ok so now we are in a position to start installing Python-related stuff. We’ll start with pyenv, which will make the process of installing various Python versions extremely smooth.

Open your native terminal and simply type the command below:

~> brew install pyenv

Build dependencies

For pyenv to work properly, we need to install several dependencies[3]. In our case:

~> brew install openssl readline sqlite3 xz zlib tcl-tk libffi

Additionally, we want to add the same dependencies in our intel homebrew so:

~> ibrew install openssl readline sqlite3 xz zlib tcl-tk libffi

At this point, you can already start installing Python versions as you want. Here’s an example you’d use if you wanted to install Python 3.9.9 and set it as default:

~> pyenv install 3.9.9
~> pyenv global 3.9.9

And, if you want to see your currently installed versions, just use pyenv versions.

Because with this kind of process a double check is always worth it, so run the next command: ~/.pyenv/versions/3.9.9/bin/python replacing 3.9.9 with the version you decided to install and set as global. Once in the shell, try to import ctypes:

Python 3.9.9 (main, Feb 15 2023, 11:25:42)
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>>

If it doesn’t break, then it’s properly installed.

Pipenv

While having it installed isn’t mandatory, it’s always a good practice to use environments for your projects. That’s why I also decided to put it in here because it’s easy and good practice. To install it:

~> brew install pipenv

And now you can just use it in any project as you will. You can check the basic usage of pipenv on their official documentation[4] or just find online tutorials.

On to the second part now. Installing TensorFlow on an M1 or M2 Mac has been a nightmare for many of us, but it doesn’t have to anymore.

You will recall that we edited the ~/.zshrc file a while ago. Now, we’ll add this to the end:

# tensorflow grpcio https://github.com/grpc/grpc/issues/25082
export GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1
export GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1
export CFLAGS="-I/opt/homebrew/opt/openssl/include"
export LDFLAGS="-L/opt/homebrew/opt/openssl/lib"

Once that’s added, we’ll have to download miniconda right into our ~/ directory. You can directly download it from https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh and manually move it to the home folder.

Once downloaded, my advice is to quit and reopen the native terminal and then run:

~> bash ~/Miniconda3-latest-MacOSX-arm64.sh -b -p $HOME/miniconda

Now that we have conda installed, we’ll activate the environment and install some dependencies for Tensorflow:

~> source ~/miniconda/bin/activate
(base) ~> conda install -c apple tensorflow-deps

Again, restart your terminal by quitting (Cmd + Q) and reopening it, and you can now install Tensorflow. I’ll do it in a dedicated environment this way:

  1. Go to your project folder: for examplecd Documents/project
  2. Activate the environment: pipenv shell
  3. Install Tensorflow: pipenv install tensorflow-macos

Et voilà! You should now be ready to use TensorFlow properly on your M1 or M2 Mac.

See how there’s a package that I installed called tensorflow-metal[5] to accelerate the training of our models in our Mac’s GPU so you could consider installing it with

~> pipenv install tensorflow-metal

Now you know how to go through the pain of setting up a brand new Mac for data science and get the most out of its new chips.

I hope this was helpful enough and any doubts or problems you might encounter, feel free to comment on this story — I or another expert will surely help you.

I don’t want to leave without kindly thanking you for going through my story. I frequently post on Medium and, if you enjoyed this post, feel free to follow me. It helps a lot.

[1] iTerm2 — macOS Terminal Replacement

[2] Noah Peters, Setup Homebrew on Apple Silicon

[3] Pyenv wiki

[4] Basic Usage of Pipenv

[5] Official PyPi — Tensorflow-metal package

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@technoblender.com. The content will be deleted within 24 hours.
Ai NewsAppleartificial intelligenceFebGuidemachine learningMacsMarinPOLpythonSetupSiliconTensorFlowUltimate
Comments (0)
Add Comment