Skip to content

Instantly share code, notes, and snippets.

@LeZuse
Last active April 22, 2024 15:06
Show Gist options
  • Save LeZuse/bf838718ff2689c5fc035c5a6825a11c to your computer and use it in GitHub Desktop.
Save LeZuse/bf838718ff2689c5fc035c5a6825a11c to your computer and use it in GitHub Desktop.
Install node on Apple Silicon M1 both ARM and x86 (Rosetta)

Node.js on Apple Silicon

Node Version Manager (https://github.com/nvm-sh/nvm) works perfectly across native node installations as well as emulated Rosetta installations. The trick I am using here is to install one LTS version of node under Rosetta and another stable version as native binary.

TODO

  • find a way how to run the same node version on both platforms
# Run the install command from a native (ARM) terminal
# See https://github.com/nvm-sh/nvm#installing-and-updating
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
# Alternatively install using brew (officially unsupported by nvm):
# brew install nvm
# Don't forget to source the command in your shell profile to make it available in new terminals!
# From Rosetta Terminal (tick checkbox in Terminal right click menu -> Get Info -> Open using Rosetta)
# To verify you are running in Rosetta run:
arch
-> i386
# Install node
nvm install lts/fermium
# Verify node installation
nvm use lts/fermium
node -e 'console.log(process.arch)'
-> x64
# From native Terminal
# To verify you are running natively:
arch
-> arm64
# Install node
nvm install stable
# Verify node installation
nvm use stable
node -e 'console.log(process.arch)'
-> arm64
# To make DX a bit better you can alias your installations:
nvm alias arm stable
# arm -> stable (-> v15.6.0)
nvm alias intel lts/fermium
# intel -> lts/fermium (-> v14.15.4)
# To test aliases:
nvm use arm
# Now using node v15.6.0 (npm v7.4.0)
node -e 'console.log(process.arch)'
-> arm64
nvm use intel
# Now using node v14.15.4 (npm v6.14.10)
node -e 'console.log(process.arch)'
-> x64
@GoonerBrian
Copy link

GoonerBrian commented Jan 21, 2023

Could someone elaborate on line 6?
# Don't forget to source the command in your shell profile to make it available in new terminals!

Does that mean to add something to my .zshrc? If so, what exactly would I be putting there? Based on https://github.com/nvm-sh/nvm#installing-and-updating shouldn't this be added already by running the curl command?

@JoshuaLelon
Copy link

JoshuaLelon commented Feb 5, 2023

I'm going to follow this guide, but I already have a few questions about step 1, so I'm assuming I'll run into a lot of trouble.

I'm also assuming others might have similar questions, so I'm going to read through the steps, post my questions, and then I'm going to follow the guide and try to answer them and/or post more questions.

01_install_nvm.sh

  1. Do I need to uninstall anything before I do this?

Run the install command from a native (ARM) terminal
See https://github.com/nvm-sh/nvm#installing-and-updating
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

  1. When you say "Run the install command" are you implying that the curl -o command is the "install command"? Or are there preliminary steps I should take that I will find in "See https://github.." and therefore I should read the github link first? If so should I read the whole thing?

Alternatively install using brew (officially unsupported by nvm):
brew install nvm

  1. Which one should I do? What're the tradeoffs?

Don't forget to source the command in your shell profile to make it available in new terminals!

  1. What does it look like to "source the command"? I know it's source <something>, but which directory should I be in? What exactly should I source? Should I follow what @serobalino did and do source ~/.nvm/nvm.sh?

02_install_x86.sh

Install node
nvm install lts/fermium

  1. What's fermium?

Verify node installation
nvm use lts/fermium
node -e 'console.log(process.arch)'
-> x64

  1. Isn't fermium supposed to be "non-M1"? Is i386 "non-M1"? Why does this say x64? Isn't that the opposite of what we're trying to install?

03_install_arm.sh

From native Terminal
To verify you are running natively:
arch
-> arm64

Install node
nvm install stable

Verify node installation
nvm use stable
node -e 'console.log(process.arch)'
-> arm64

  1. wait didn't we just install the M1 version of node? What's the difference between x64 and arm64? I'm assuming the latter is M1 node but then that makes me wonder why we're installing x64. I thought we were trying to install x86? What's the difference between x86 and i386?

04_handy_alias.sh

To make DX a bit better you can alias your installations:
nvm alias arm stable
arm -> stable (-> v15.6.0)
nvm alias intel lts/fermium
intel -> lts/fermium (-> v14.15.4)

To test aliases:
nvm use arm
Now using node v15.6.0 (npm v7.4.0)
node -e 'console.log(process.arch)'
-> arm64
nvm use intel
Now using node v14.15.4 (npm v6.14.10)
node -e 'console.log(process.arch)'
-> x64

  1. So arm is arm64, intel is x64. And the M1 macs use which one again? Isn't one of these supposed to be x32 or i386? How does Rosetta fit into all of this?

@JoshuaLelon
Copy link

Okay, I've overwhelmed myself so I won't answer all my own questions, but I did manage to follow this guide successfully, so here are a few answers:

  1. no I didn't need to uninstall anything
  2. yes, curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash is the install command, I verified that by reading the link, and it worked out for me
  3. I read something somewhere about using brew not being a good idea, so I used the curl command.
  4. what @serobalino did worked for me: source ~/.nvm/nvm.sh
  5. fermium is just a name for a version of node
  6. Fermium is non-m1 (x86_64), i386 is also non-M1, we want to install x64 version because that's the version of node we will run with Rosetta.
  7. now we are installing the m1 version. x64 is non M1, arm64 is M1. we are not trying to install x86 because that's 32bit and i have a 64 bit OS. the difference between x86 and i386 is irrelevant to me now
  8. M1 macs use arm64, Rosetta will allow us to run x64 (non-M1)

@GoonerBrian
Copy link

GoonerBrian commented Feb 5, 2023

These instructions seem to have worked for me but I'm still not able to connect to an oracledb using my M1 Mac. I created this simple repo. If anyone is willing to take a look and tell me where I'm going wrong, I would really appreciate it.

Update: I was able to resolve the issue and will update the repo I linked in the next few days.

@Leakageonthelamp
Copy link

This super helpful and tricky thank a lot.

@sridharcriteria
Copy link

thanks a lot brother!

@Arunaks007
Copy link

@GoonerBrian Thanks a lot brother, I've been trying to fix this issue for the past 12hrs in my mac m1. Nothing really worked until i saw your repo. I really appreciate it❤️, its 3am here I can finally sleep now!!

@GoonerBrian
Copy link

@AmrMekkawy glad I could help! Sleep well.

@dipenparmar12
Copy link

Thank you, working expected.

@cbess
Copy link

cbess commented Dec 29, 2023

As a note, this is not needed if you are installing node 16+

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