Setting up Apple’s Custom Silicon Devices for Data Science | by Pascal Janetzky | Jul, 2022
Prepare any M1 and M2 machine in less than two minutes
With the release of the second iteration of its custom System-on-a-Chip, Apple once again increased the compute power. The new M2 chip supports up to 24 GB of main memory and can offer up to 10 GPU cores. While this currently is small compared to the M1 Pro, Ultra, and Max models, the second iteration is also expected to feature more performant versions later.
Until then, we can use the current computing power to the maximum. However, after the switch from Intel-based systems to custom chips, setting up the Mac computers has become a bit more complicated. In an earlier version of this guide, I manually guided you through the process step by step. Since the publication of this post, I have received feedback and gained more insights into the setup. With more knowledge comes more possibilities, and in this post, I will show you a script that automates the installation process. The script can be run on any M* device, be it the original M1 chip, its siblings, or the all-new M2 version.
Running the full script
The script lets you solve machine learning problems on my setup in less than two minutes. It has 20 lines of code, of which nine lines — ~50% — are comments that keep us up-to-date on the installation process.
If you have only two minutes, here’s how: To run the script, download the source code. To do so, open a new Terminal window (Spotlight → Terminal) and enter
wget https://github.com/phrasenmaeher/apple__ml_setup/blob/main/apple_setup.sh
This downloads the setup script to your machine. Next, type chmod +x apple_setup.sh, which makes the script executable. Finally, type ./apple_setup.sh to start the installation.
The entire script is shown below. Whenever I mention line numbers in the remainder of this post, I refer to the lines of this full script:
Installing MiniForge
In the first four lines, we download and install the latest version of MiniForge, a minimal Anaconda version suited for Apple systems. Minimal means it is not bloated, and only the essential features are included. This is enough for our use case; we’ll manually install all we need. Generally, we need package manager software to handle all the different tools we need for our machine learning tasks. Put most simply, the software allows us to install all the packages — e.g., TensorFlow and PyTorch — that we need to get started.
Create a new virtual environment
In line number five (one in the snippet above), we activate our newly MiniForge package manager.
This subsequently lets us create a new environment specially designed for machine learning tasks. We name this environment machine_learning. Each virtual environment can be seen as a box, and each project usually uses its own box. We can drastically reduce undesired interference between projects by using separate boxes for each project. That is Anaconda’s and MiniForge’s primary job.
Installing TensorFlow
Beginning in line nine (one in the snippet above), we activate our new environment and proceed to install TensorFlow. For this, we need a particular package, tensorflow-deps, that we get from Apple’s software library (that is the -c apple in line 14; line one above). Afterward, we install the actual TensorFlow package (line 15; line two above).
Installing PyTorch and Scikit-Learn
After installing TensorFlow, we also take the chance and install scikit-learn (often called sklearn) and PyTorch.
That’s it; you now have an environment ready for machine learning! To activate it in your Terminal application, type
source ~/miniforge3/bin/activate
and then enter
conda activate machine_learning
to activate the new environment. In the IDE of your choice, you might need to point the interpreter (the thing that lets you run your code) to /Users/<replace_with_your_username>/miniforge3/envs/machine_learning
For a visualization of this process, see my earlier post and insert the above path appropriately.
Summary
In this blog, I guided you through a short installation script that gets you ready for Machine Learning in under two minutes. It uses MiniForge to manage the required packages and automatically installs TensorFlow and PyTorch. To repeat what I wrote in the beginning: to run the script, download the source code by opening a new Terminal window (Spotlight->Terminal) and entering
wget https://github.com/phrasenmaeher/apple__ml_setup/blob/main/apple_setup.sh
This downloads the setup script to your machine. Next, type chmod +x apple_setup.sh, which makes the script executable. Finally, type ./apple_setup.sh to start the installation.
Prepare any M1 and M2 machine in less than two minutes
With the release of the second iteration of its custom System-on-a-Chip, Apple once again increased the compute power. The new M2 chip supports up to 24 GB of main memory and can offer up to 10 GPU cores. While this currently is small compared to the M1 Pro, Ultra, and Max models, the second iteration is also expected to feature more performant versions later.
Until then, we can use the current computing power to the maximum. However, after the switch from Intel-based systems to custom chips, setting up the Mac computers has become a bit more complicated. In an earlier version of this guide, I manually guided you through the process step by step. Since the publication of this post, I have received feedback and gained more insights into the setup. With more knowledge comes more possibilities, and in this post, I will show you a script that automates the installation process. The script can be run on any M* device, be it the original M1 chip, its siblings, or the all-new M2 version.
Running the full script
The script lets you solve machine learning problems on my setup in less than two minutes. It has 20 lines of code, of which nine lines — ~50% — are comments that keep us up-to-date on the installation process.
If you have only two minutes, here’s how: To run the script, download the source code. To do so, open a new Terminal window (Spotlight → Terminal) and enter
wget https://github.com/phrasenmaeher/apple__ml_setup/blob/main/apple_setup.sh
This downloads the setup script to your machine. Next, type chmod +x apple_setup.sh, which makes the script executable. Finally, type ./apple_setup.sh to start the installation.
The entire script is shown below. Whenever I mention line numbers in the remainder of this post, I refer to the lines of this full script:
Installing MiniForge
In the first four lines, we download and install the latest version of MiniForge, a minimal Anaconda version suited for Apple systems. Minimal means it is not bloated, and only the essential features are included. This is enough for our use case; we’ll manually install all we need. Generally, we need package manager software to handle all the different tools we need for our machine learning tasks. Put most simply, the software allows us to install all the packages — e.g., TensorFlow and PyTorch — that we need to get started.
Create a new virtual environment
In line number five (one in the snippet above), we activate our newly MiniForge package manager.
This subsequently lets us create a new environment specially designed for machine learning tasks. We name this environment machine_learning. Each virtual environment can be seen as a box, and each project usually uses its own box. We can drastically reduce undesired interference between projects by using separate boxes for each project. That is Anaconda’s and MiniForge’s primary job.
Installing TensorFlow
Beginning in line nine (one in the snippet above), we activate our new environment and proceed to install TensorFlow. For this, we need a particular package, tensorflow-deps, that we get from Apple’s software library (that is the -c apple in line 14; line one above). Afterward, we install the actual TensorFlow package (line 15; line two above).
Installing PyTorch and Scikit-Learn
After installing TensorFlow, we also take the chance and install scikit-learn (often called sklearn) and PyTorch.
That’s it; you now have an environment ready for machine learning! To activate it in your Terminal application, type
source ~/miniforge3/bin/activate
and then enter
conda activate machine_learning
to activate the new environment. In the IDE of your choice, you might need to point the interpreter (the thing that lets you run your code) to /Users/<replace_with_your_username>/miniforge3/envs/machine_learning
For a visualization of this process, see my earlier post and insert the above path appropriately.
Summary
In this blog, I guided you through a short installation script that gets you ready for Machine Learning in under two minutes. It uses MiniForge to manage the required packages and automatically installs TensorFlow and PyTorch. To repeat what I wrote in the beginning: to run the script, download the source code by opening a new Terminal window (Spotlight->Terminal) and entering
wget https://github.com/phrasenmaeher/apple__ml_setup/blob/main/apple_setup.sh
This downloads the setup script to your machine. Next, type chmod +x apple_setup.sh, which makes the script executable. Finally, type ./apple_setup.sh to start the installation.