1、下载源码,你需要在https://nodejs.org/en/download/下载最新的Nodejs版本,本文以v8.11.1为例:
cd /usr/local/src/ wget https://nodejs.org/dist/v8.11.1/node-v8.11.1.tar.gz
2、解压源码
tar zxvf node-v8.11.1.tar.gz
3、 编译安装
cd node-v8.11.1 ./configure --prefix=/usr/local/node/v8.11.1 make make install
注意:
./configure --prefix=/usr/local/node/v8.11.1
可能报错:
1. WARNING: C++ compiler too old, need g++ 4.9.4 or clang++ 3.4.2 (CXX=g++)
提示gcc版本过低,需要4.9.4以上版本
2.WARNING: warnings were emitted in the configure phase
提示在配置阶段发出警告
解决办法
下载gcc
wget http://ftp.gnu.org/gnu/gcc/gcc-4.9.4/gcc-4.9.4.tar.bz2
解压gcc
tar -xvf gcc-4.9.4.tar.bz2
cd gcc-4.9.4
./contrib/download_prerequisites 下载依赖项或者离线下载
wget http://ftp.vim.org/languages/gcc/infrastructure/mpc-1.0.3.tar.gz
wget http://ftp.vim.org/languages/gcc/infrastructure/mpfr-3.1.4.tar.bz2
wget http://ftp.vim.org/languages/gcc/infrastructure/gmp-4.3.2.tar.bz2
wget http://ftp.vim.org/languages/gcc/infrastructure/cloog-0.18.1.tar.gz
wget http://ftp.vim.org/languages/gcc/infrastructure/isl-0.14.tar.bz2
cd gcc-4.9.4(全部解压到目录编译安装一下)
tar -xvf gmp-4.3.2.tar.bz2
cd gmp-4.3.2
./configure
make && make install
cd ..
tar -zxvf cloog-0.18.1.tar.gz
cd cloog-0.18.1
./configure
make && make install
cd ..
tar -xvf isl-0.14.tar.bz2
cd isl-0.14
./configure
make && make install
cd ..
tar -xvf mpfr-3.1.4.tar.bz2
cd mpfr-3.1.4
./configure
make && make install
cd ..
tar -zxvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure configure: error: libmpfr not found or uses a different ABI (including static vs shared).配置:错误:libmpfr未找到或使用不同的ABI(包括静态vs共享)。
make && make install make: *** No targets specified and no makefile found. Stop. 使:***没有指定目标,也没有发现makefile。停止。
/root下建立编译输出目录
mkdir gcc-build-4.9.4
进入输出目录,执行命令,并生成makefile
cd gcc-build-4.9.4
../gcc-4.9.4/configure --enable-checking=release --enable-languages=c,c++ --disable-multil
make && make install
查看版本
gcc -v g++ -v 或者 gcc --version g++ --version
检查动态库:
strings /usr/lib64/libstdc++.so.6 | grep GLIBC
4、 配置NODE_HOME,进入profile编辑环境变量
vim /etc/profile
设置nodejs环境变量,在 export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL 一行的上面添加如下内容:
#set for nodejs export NODE_HOME=/usr/local/node/0.10.24 export PATH=$NODE_HOME/bin:$PATH
:wq保存并退出,编译/etc/profile 使配置生效
source /etc/profile
验证是否安装配置成功
node -v
输出 v8.11.1 表示配置成功
npm模块安装路径
/usr/local/node/0.10.24/lib/node_modules/
注:Nodejs 官网提供了编译好的Linux二进制包,你也可以下载下来直接应用。