linux下编译安装Nodejs
GCC和G++分别是GNU的C和C++编译器。它们在执行编译工作的时候把源代码通过预处理转化成汇编语言生成.i后缀的文件,再由汇编变成目标机器代码,最后连接目标代码生成可执行程序。就可以在命令行里面通过系统路径来直接调用了。
所以呢要安装Nodejs首先就要先扫平这些环境问题。比Windows上傻瓜式的安装要复杂一些。
我演示用的服务器是托管在登记在DigitalOcean身上的CentOS。其他的linux系统安装也大同小异。
首先我需要查看当前系统的发行版本。CentOS是通过Red Hat社区发布的。
Nodejs源码主要是通过C++代码和JavaScript代码构成的。但是它是通过gyp这个跨平台构建工具管理你的项目源码。gyp是用Python语言写的,因此在安装Nodejs的时候是需要Python 2.6或者是2.7的版本。但是官方并不推荐使用3.0的版本。
有时候你安装失败往往是由于Python的版本太旧或者它在系统中的安装路径不对。
[root@www ~]# cat /etc/redhat-release CentOS release 6.9 (Final) You have new mail in /var/spool/mail/root [root@www ~]# rpm -q gcc rpm -q gcc-c++ gcc-4.4.7-18.el6.x86_64 rpm-4.8.0-55.el6.x86_64 gcc-c++-4.4.7-18.el6.x86_64 [root@www ~]#
[root@www ~]# python -V Python 2.6.6
[root@www node-v9.7.1]# yum -y install gcc gcc-c++ kernel-devel Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Determining fastest mirrors Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os&infra=stock error was 12: Timeout on http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os&infra=stock: (28, 'Operation too slow. Less than 1 bytes/sec transfered the last 30 seconds') * base: mirrors.aliyun.com * extras: mirrors.163.com * updates: mirrors.163.com base | 3.7 kB 00:00 extras | 3.4 kB 00:00 extras/primary_db | 30 kB 00:00 updates | 3.4 kB 00:00 updates/primary_db | 6.4 MB 00:13 Package gcc-4.4.7-18.el6.x86_64 already installed and latest version Package gcc-c++-4.4.7-18.el6.x86_64 already installed and latest version Resolving Dependencies --> Running transaction check ---> Package kernel-devel.x86_64 0:2.6.32-696.20.1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: kernel-devel x86_64 2.6.32-696.20.1.el6 updates 11 M Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 11 M Installed size: 26 M Downloading Packages: kernel-devel-2.6.32-696.20.1.el6.x86_64.rpm | 11 MB 00:22 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : kernel-devel-2.6.32-696.20.1.el6.x86_64 1/1 Verifying : kernel-devel-2.6.32-696.20.1.el6.x86_64 1/1 Installed: kernel-devel.x86_64 0:2.6.32-696.20.1.el6 Complete! [root@www node-v9.7.1]#
https://github.com/nodejs/node
在Nodejs的项目根目录下就能看到一个Makefile文件。
到Nodejs的官网复制一下源码的链接,我们要用源码编译的方式来安装。
https://nodejs.org/en/download/current/
https://nodejs.org/dist/v9.7.1/node-v9.7.1.tar.gz
[root@www ~]# cd /usr/src [root@www src]# wget https://nodejs.org/dist/v9.7.1/node-v9.7.1.tar.gz --2018-03-05 13:45:51-- https://nodejs.org/dist/v9.7.1/node-v9.7.1.tar.gz Resolving nodejs.org... failed: Temporary failure in name resolution. wget: unable to resolve host address “nodejs.org” [root@www src]# wget https://nodejs.org/dist/v9.7.1/node-v9.7.1.tar.gz --2018-03-05 13:46:37-- https://nodejs.org/dist/v9.7.1/node-v9.7.1.tar.gz Resolving nodejs.org... 104.20.23.46, 104.20.22.46, 2400:cb00:2048:1::6814:172e, ... Connecting to nodejs.org|104.20.23.46|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 31453341 (30M) [application/gzip] Saving to: “node-v9.7.1.tar.gz” 100%[======================================>] 31,453,341 385K/s in 79s 2018-03-05 13:48:01 (386 KB/s) - “node-v9.7.1.tar.gz” saved [31453341/31453341]
[root@www src]# tar -xf node-v9.7.1.tar.gz [root@www src]# ls Chrome53_gop_v2017.9.2.7z git-2.5.0.tar.gz node-v9.7.1 debug kernels node-v9.7.1.tar.gz git-2.5.0 lantern-installer-64-bit.deb [root@www src]# cd node-v9.7.1.tar.gz bash: cd: node-v9.7.1.tar.gz: Not a directory [root@www src]# cd node-v9.7.1 [root@www node-v9.7.1]# ./configure which: no python2.7 in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin:/usr/local/git/bin:/usr/local/git/bin:/usr/local/git/bin) WARNING: C++ compiler too old, need g++ 4.9.4 or clang++ 3.4.2 (CXX=g++) creating icu_config.gypi * Using ICU in deps/icu-small creating icu_config.gypi { 'target_defaults': { 'cflags': [], 'default_configuration': 'Release', 'defines': [], 'include_dirs': [], 'libraries': []}, 'variables': { 'asan': 0, 'coverage': 'false', 'debug_http2': 'false', 'debug_nghttp2': 'false', 'force_dynamic_crt': 0, 'gas_version': '2.20', 'host_arch': 'x64', 'icu_data_in': '../../deps/icu-small/source/data/in/icudt60l.dat', 'icu_endianness': 'l', 'icu_gyp_path': 'tools/icu/icu-generic.gyp', 'icu_locales': 'en,root', 'icu_path': 'deps/icu-small', 'icu_small': 'true', 'icu_ver_major': '60', 'llvm_version': 0, 'node_byteorder': 'little', 'node_enable_d8': 'false', 'node_enable_v8_vtunejit': 'false', 'node_install_npm': 'true', 'node_module_version': 59, 'node_no_browser_globals': 'false', 'node_prefix': '/usr/local', 'node_release_urlbase': '', 'node_shared': 'false', 'node_shared_cares': 'false', 'node_shared_http_parser': 'false', 'node_shared_libuv': 'false', 'node_shared_nghttp2': 'false', 'node_shared_openssl': 'false', 'node_shared_zlib': 'false', 'node_tag': '', 'node_target_type': 'executable', 'node_use_bundled_v8': 'true', 'node_use_dtrace': 'false', 'node_use_etw': 'false', 'node_use_lttng': 'false', 'node_use_openssl': 'true', 'node_use_perfctr': 'false', 'node_use_v8_platform': 'true', 'node_without_node_options': 'false', 'openssl_fips': '', 'openssl_no_asm': 0, 'shlib_suffix': 'so.59', 'target_arch': 'x64', 'v8_enable_gdbjit': 0, 'v8_enable_i18n_support': 1, 'v8_enable_inspector': 1, 'v8_no_strict_aliasing': 1, 'v8_optimized_debug': 0, 'v8_promise_internal_field_count': 1, 'v8_random_seed': 0, 'v8_trace_maps': 0, 'v8_use_snapshot': 'true', 'want_separate_host_toolset': 0}} creating config.gypi creating config.mk WARNING: warnings were emitted in the configure phase [root@www node-v9.7.1]#