Skip to content

Instantly share code, notes, and snippets.

@shirakaba
Last active May 10, 2021 21:04
Show Gist options
  • Save shirakaba/87aabee6e2ec70e298e3d7c42f701675 to your computer and use it in GitHub Desktop.
Save shirakaba/87aabee6e2ec70e298e3d7c42f701675 to your computer and use it in GitHub Desktop.
Running NativeScript on Apple Silicon via Rosetta (x86 emulation)

This guide will show you how to provision an Apple Silicon machine to run a NativeScript app via Rosetta (x86 emulation). It may soon become possible to run it natively, but I believe that's pending this PR, so I had to go with Rosetta for the time being.

I did this setup on a borrowed M1 Mac Mini, so a few things had been provisioned already, but I'll do my best to retrace setup steps below.

I referenced the iOS-related setup steps in the NativeScript Advanced Setup: macOS docs. They're still up-to-date, except for referring to an older version of Node.js. Specifically, what I did was:

1) Install Homebrew:

x86 Homebrew was provisioned already on the machine, like so:

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

2) No need to install Ruby

I just used Apple's stock Ruby (2.6.3p62, which is only for x86).

3) Install Node for x86

Sidenote: I did experimentally try to run my NativeScript app using Node for arm64, but things ultimately failed once I reached the tns run ios command, as it produced the error Error: spawn /usr/local/lib/node_modules/nativescript/node_modules/ios-device-lib/bin/darwin/arm64/ios-device-lib ENOENT. This may shortly be solved once this PR lands, however.

arch -x86_64 brew update
arch -x86_64 brew install node@15

I also updated my .zshenv with the following path to ensure that node was on my path:

export PATH=/usr/local/bin:/bin:/sbin:/usr/bin:/usr/local/sbin:$PATH

4) Install Xcode

I just downloaded and installed it via the App Store app.

I then opened it, and accepted when it prompted me whether I'd like to install additional tools.

Next, I entered Preferences -> Locations -> Command Line Tools and selected "Xcode 12.3 (12C33)" (the only option available).

I also took this moment to log into my Xcode developer account in Preferences.

5) Install the remaining dependencies

Nothing special needed for these steps.

sudo gem install xcodeproj
sudo gem install cocoapods
pod setup
sudo easy_install pip
pip install six

6) Configure the Terminal app to open in Rosetta mode

This step is necessary for the Cocoapods installation step to work, as the native Terminal encounters an issue with the ffi gem otherwise, as documented in this GitHub issue.

See this GitHub Issue comment to show how to open Terminal in Rosetta mode.

7) Build the app

Now you must reopen the Terminal app (if you haven't already) to ensure that it's in Rosetta (x86) mode. It's needed for the ns run ios step to successfully perform the Pod install step.

ns create --react mycoolapp
cd mycoolapp
ns run ios

Your iOS app should now run in the simulator!

@shirakaba
Copy link
Author

To run in arm64, after this PR lands, I think the steps would need to be modified to:

  • remove any mentions of arch -x86_64 from the shell commands
  • tell the Terminal app to run in native mode rather than Rosetta mode
  • use a native build of Node 15

@tonylindsay00
Copy link

I've just spent a futile evening trying to get native script up and running on the mac M1
At 3) Install Node for x86 did you get past the error:
"Error: spawn /usr/local/lib/node_modules/nativescript/node_modules/ios-device-lib/bin/darwin/arm64/ios-device-lib ENOENT"
using

arch -x86_64 brew update
arch -x86_64 brew install node@15
?

@shirakaba
Copy link
Author

@tonylindsay00 That error indicates that the NativeScript iOS runtime doesn't yet support arm64.

You need to run Terminal in Rosetta mode, then call ns run ios from that same terminal. To my understanding, NativeScript CLI will then assume that you're on an x86 instruction set instead, and will accordingly use the x86 ios-device-lib.

You may also need to configure Xcode to run in Rosetta mode (I can't remember). Try the steps one-at-a-time. To configure Xcode to run in Rosetta mode, select Xcode in Finder and press Cmd+I to open the Info menu, from which you can check the relevant checkbox to make it run in Rosetta mode.

@andresilva-cc
Copy link

andresilva-cc commented Mar 4, 2021

I already have Homebrew and Node.js running natively. If I follow the steps you mentioned, will they work together or it might bug/override my native versions?

@shirakaba
Copy link
Author

shirakaba commented Mar 5, 2021

@andresilva-cc It should be fine for Homebrew to be installed natively as long as you have a way to install both x86 and ARM editions of Node, and to ensure that the x86 edition is the one on your path when running in Rosetta mode.

I believe you then need to run your terminal in Rosetta mode, mainly so that it runs Node in x86 mode, which in turn runs NativeScript CLI in x86 mode, and thus selects the x86 ios-device-lib (because the ARM equivalent isn’t available yet – or at least wasn’t at the time of writing). At least that’s my understanding.

@anarnoli
Copy link

@shirakaba I'm trying to setup NS 8 on M1 and followed Environment Setup steps for MacOS + iOS. I also tried to follow "Additional notes for M1 based machines" recommendation:

softwareupdate --install-rosetta
Successfully installed

sudo arch -x86_64 gem install ffi
Throws error arch: posix_spawnp: gem: Bad CPU type in executable

arch -x86_64 pod install
Throws error arch: posix_spawnp: pod: Bad CPU type in executable

I'm seeking help for setting up environment. Appreciating your help.

@shirakaba
Copy link
Author

@anarnoli rather than passing arch -x86_64, I opened my terminal in Rosetta mode for that step (see step 6). I haven’t checked how well my approach lines up with the official instructions.

@anarnoli
Copy link

@shirakaba I followed step 6 and ns run ios failed with below error.

webpack 5.31.2 compiled successfully in 10151 ms
Webpack compilation complete. Watching for file changes.
Installing pods...
arch: posix_spawnp: pod: Bad CPU type in executable
'arch install' command failed.

@shirakaba
Copy link
Author

@anarnoli

To be clear, I did all of these steps when running the terminal in native (normal) mode:

sudo gem install xcodeproj
sudo gem install cocoapods
pod setup
sudo easy_install pip
pip install six

And I did these steps when running the terminal in Rosetta mode:

ns create --react mycoolapp
cd mycoolapp
ns run ios

My instructions detail only how to run NativeScript apps in Rosetta mode, whereas the NativeScript additional notes for M1 based machines (which were written later, once M1 support was fully implemented) concern how to run NativeScript apps natively in M1 mode. It may not be possible to mix our two sets of instructions successfully.

@anarnoli
Copy link

Thanks @shirakaba.

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