1 node源码下载
https://nodejs.org/dist/v8.9.2/node-v8.9.2-linux-x64.tar.xz
2 所有的操作都需要超级权限,所以解压语句为
cd /home/flq1/Downloads && sudo tar xf node-v8.9.2-linux-x64.tar.xz -C /usr/local/
执行完上面的命令程序安装路径在 /usr/local/node-v8.9.2-linux-x64/bin
3:新建文件
vim /etc/profile.d/node.sh
增加这一条 export PATH=$PATH:/usr/local/node-v8.9.2-linux-x64/bin
4 这是设置环境变量,然后 执行 source /etc/profile 使环境变量生效
which node 一下就看到node安装的目录。node -version可以查看node的版本
如果只是安装的话就结束了。监于每次安装都是遇到麻烦,知其然不知其所以然,我们再来分析一下里面的文件结构
flq1@ubuntu:/usr/local/node-v8.9.2-linux-x64/bin$ tree /etc/ |grep profile
│ │ │ ├── change_profile
│ └── profile
├── profile
├── profile.d
│ │ ├── 65compiz_profile-on-session
│ │ ├── a11y-profile-manager-indicator-autostart.desktop
在/etc/下面有一个文件profile和一个目录profile.d ,profile里面定义这个规则
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
是检测目录profile.d下面的*.sh文件,然后加载的
/etc/profile.d/
├── bash_completion.sh
├── cedilla-portuguese.sh
└── node.sh
我们在执行source /etc/profile的时候是把/etc/profile.d/下面的所有文件设置的环境变量加载进来了