Kudos
Collect
Twiiter
Facebook
Share
Develop somethings, meditation, reading and thinking...

Mac 에 Homebrew 를 통하여 nvm 으로 Node.js 설치하기

Last updated over 3 years ago
2 0 0 0

왜 이렇게 복잡한 과정을 거쳐 Node.js 를 설치하는가 하면, 모든 설치를 패키지 매니저를 통해서 설치하고 싶기 때문이다. 패키지 매니저를 이용하면 알아서 관련 패키지를 설치해 주는 것은 물론, 삭제를 할 경우에도 자동으로 삭제를 해 주기 때문에 세세한 부분까지 신경쓰기는 싫지만 어느정도 시스템을 깨끗하게 유지하고 싶은 분들에게 추천하는 방법이다.

또, Node.js 의 경우 프로젝트별로 다른 Node.js 를 설치해 사용하는 경우가 많기 때문에, nvm 을 이용한 설치가 많이 이용된다.

다음은 Homebrew 를 이용해서 nvm 을 설치하고, nvm 을 이용해서 Node.js 를 설치하는 과정이다.

Homebrew 설치

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

nvm 설치

$ brew install nvm
==> Downloading https://github.com/creationix/nvm/archive/v0.34.0.tar.gz
==> Downloading from https://codeload.github.com/creationix/nvm/tar.gz/v0.34.0
######################################################################## 100.0%
==> Caveats
Please note that upstream has asked us to make explicit managing
nvm via Homebrew is unsupported by them and you should check any
problems against the standard nvm install method prior to reporting.

You should create NVM's working directory if it doesn't exist:

  mkdir ~/.nvm

Add the following to ~/.bash_profile or your desired shell
configuration file:

  export NVM_DIR="$HOME/.nvm"
  [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/usr/local/opt/nvm/etc/bash_completion" ] && . "/usr/local/opt/nvm/etc/bash_completion"  # This loads nvm bash_completion

You can set $NVM_DIR to any location, but leaving it unchanged from
/usr/local/opt/nvm will destroy any nvm-installed Node installations
upon upgrade/reinstall.

~/.nvm 디렉토리 생성

$ mkdir ~/.nvm

~/.bash_profile 파일 설정 (nvm)

export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion" ] && . "/usr/local/opt/nvm/etc/bash_completion"  # This loads nvm bash_completion

zsh 의 경우에는 vi .zshrc. 설정하고 나면 nvm 실행 가능.

nvm 설치 에러를 만났다면

Mojave 등의 최신 macOS 는 보안에 신경을 많이 쓰는 듯 하다. 다음과 같은 에러가 표시되면 하라는대로 man 페이지에 대한 퍼미션을 추가해 준다.

$ brew install nvm
Error: The following directories are not writable by your user:
/usr/local/share/man/man3
/usr/local/share/man/man5
/usr/local/share/man/man7

You should change the ownership of these directories to your user.
  sudo chown -R $(whoami) /usr/local/share/man/man3 /usr/local/share/man/man5 /usr/local/share/man/man7

And make sure that your user has write permission.
  chmod u+w /usr/local/share/man/man3 /usr/local/share/man/man5 /usr/local/share/man/man7

$ sudo chown -R $(whoami) /usr/local/share/man/man3 /usr/local/share/man/man5 /usr/local/share/man/man7
Password:
$ chmod u+w /usr/local/share/man/man3 /usr/local/share/man/man5 /usr/local/share/man/man7

Node.js 설치

$ nvm install v10.15.3 # LTS
$ node -v
v10.15.3

/usr/bin/node 링크 만들기

$ sudo ln -s /Users/aki/.nvm/versions/node/v10.15.3/bin/node /usr/bin/node

Sierra 부터는 SIP 설정을 꺼야 링크를 만들수 있다. (SIP 설정을 끄는 방법은 How to set Gatekeeper and SIP disabled 를 참고.)

macOS Catalina 에서는 Read only directory 라는 메시지와 함께 설정이 불가능한 것 같다. 필요할 경우에는 직접 디렉토리를 지정하는 방법으로 대응.

참고문헌

Hi, my name is Richard. I’m a developer wants to make the world better with logic power. Mainly I use Linux, Nginx, MySQL, PHP and JavaScript . I want to share my knowledge with someone that it was also based from some great persons via LYNMP. 👨‍💻

Related Articles
Essedrop - Make your file online instantly
 

Responses

Leave a response to @richard

Please sign in to comment.
Markdown is also available in comment.