在 Ubuntu 12.04 安裝 Node.js

  • 安裝nvm並加入環境變數中
  • 用nvm安裝nodejs v5.1.0
  • 更新gcc, g++版本
#!/bin/bash
test "$(grep 'NVM_NODEJS_ORG_MIRROR' /etc/bash.bashrc)" || echo 'export NVM_NODEJS_ORG_MIRROR=http://nodejs.org/dist' >> /etc/bash.bashrc
test "$(grep 'NVM_DIR' /etc/bash.bashrc)" || echo 'export NVM_DIR="/etc/nvm"' >> /etc/bash.bashrc
test "$(grep 'nvm.sh' /etc/bash.bashrc)" || echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> /etc/bash.bashrc
NVM_DIR="/etc/nvm"
NVM_NODEJS_ORG_MIRROR="http://nodejs.org/dist"
git clone https://github.com/creationix/nvm.git $NVM_DIR && cd $NVM_DIR && git checkout `git describe --abbrev=0 --tags`

. $NVM_DIR/nvm.sh
nvm install v5.1.0
nvm alias default 5.1

echo 'deb http://cz.archive.ubuntu.com/ubuntu trusty main' >> /etc/apt/sources.list
apt-get update; apt-get install -y gcc-4.8 g++-4.8
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 10
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 10