如何从 GCP Cloud Shell(全局安装)中的 bash 文件 运行 nvm 命令?
How to run nvm command from bash file in GCP Cloud Shell (global install)?
我们正在努力做什么
我们在 GCP 上使用云 Shell 编辑器 (IDE)。
根据 documentation for Environment customization script:
Cloud Shell automatically runs the script,
$HOME/.customize_environmen
t, when your instance boots up. Unlike
.profile
or .bashrc
, this script runs once when Cloud Shell boots
(rather than once for each shell login).
This script runs as root and you can install any packages that you
want to exist in each Cloud Shell session using Debian package
management commands.
For example, if you'd like to have erlang installed on Cloud Shell,
your .customize_environment
file will look like this:
#!/bin/sh
apt-get update
apt-get -y install erlang
我们正在尝试使用 nvm
.
为我们的开发人员预安装节点版本和自动配置
我们遇到问题的地方
我们研究了几篇关于从 bash 脚本文件(.customize_environment
文件)中制作 nvm
可调用 的文章:
- nvm command not available in bash script
- Can't use nvm from bash script
- Node Version Manager install - nvm command not found
None 这些答案似乎解决了我们的问题。
GCP nvm
详细信息
云 Shell 预安装 nvm
(全局);它位于 /usr/local/nvm/nvm.sh
.
没有 ~/.nvm
文件夹。
我可以 运行 nvm
直接从 Cloud Shell(命令行)没有问题。
我不能从 bash 文件 运行 nvm
(也不能使用 for 路径)。
最接近的尝试
运行 以下接近,但由于云 Shell 的限制,我限制了你无法 sudo
的权限:
. /usr/local/nvm/nvm.sh
nvm install 14
结果:
Downloading and installing node v14.15.4...
mkdir: cannot create directory ‘/usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64’: Permission denied
creating directory /usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64/files failed
Downloading https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz...
Warning: Failed to create the file
Warning: /usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64/node-v14.15.4-linux-
Warning: x64.tar.xz: No such file or directory
curl: (23) Failed writing body (0 != 966)
Binary download from https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz failed, trying source.
grep: /usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64/node-v14.15.4-linux-x64.tar.xz: No such file or directory
Provided file to checksum does not exist.
Binary download failed, trying source.
Detected that you have 4 CPU core(s)
Running with 3 threads to speed up the build
mkdir: cannot create directory ‘/usr/local/nvm/.cache/src’: Permission denied
creating directory /usr/local/nvm/.cache/src/node-v14.15.4/files failed
Downloading https://nodejs.org/dist/v14.15.4/node-v14.15.4.tar.xz...
Warning: Failed to create the file
Warning: /usr/local/nvm/.cache/src/node-v14.15.4/node-v14.15.4.tar.xz: No such
Warning: file or directory
curl: (23) Failed writing body (0 != 965)
Binary download from https://nodejs.org/dist/v14.15.4/node-v14.15.4.tar.xz failed, trying source.
grep: /usr/local/nvm/.cache/src/node-v14.15.4/node-v14.15.4.tar.xz: No such file or directory
Provided file to checksum does not exist.
重要行:mkdir: cannot create directory ‘...’: Permission denied
我不能sudo
绕过它,运行usermod
(例如usermod -a -G staff $(whoami)
),尽管Google's documentation says I should be able to:
When you set up a Cloud Shell session, you get a regular Unix user account with a username based on your email address. With this access, you have full root privileges on your allocated VM and can even run sudo
commands, if you need to.
问题
我们如何在 GCP 云 Shell 的 bash 脚本文件中使用 nvm
?
我在 @Kolban and @HarshManvar 的评论的帮助下设法解决了这个问题。
我认为此解决方案也适用于无法处理来自 .customize_environment
.
的直接调用的任何其他脚本
下面的示例受 answer found here 的影响。该代码安装特定节点版本。
注意:nvm
默认全局安装在 Cloud Shell 中。
#!/usr/bin/bash
sudo su -c '. /usr/local/nvm/nvm.sh && nvm install 14'
我们正在努力做什么
我们在 GCP 上使用云 Shell 编辑器 (IDE)。
根据 documentation for Environment customization script:
Cloud Shell automatically runs the script,
$HOME/.customize_environmen
t, when your instance boots up. Unlike.profile
or.bashrc
, this script runs once when Cloud Shell boots (rather than once for each shell login).This script runs as root and you can install any packages that you want to exist in each Cloud Shell session using Debian package management commands.
For example, if you'd like to have erlang installed on Cloud Shell, your
.customize_environment
file will look like this:#!/bin/sh apt-get update apt-get -y install erlang
我们正在尝试使用 nvm
.
我们遇到问题的地方
我们研究了几篇关于从 bash 脚本文件(.customize_environment
文件)中制作 nvm
可调用 的文章:
- nvm command not available in bash script
- Can't use nvm from bash script
- Node Version Manager install - nvm command not found
None 这些答案似乎解决了我们的问题。
GCP nvm
详细信息
云 Shell 预安装 nvm
(全局);它位于 /usr/local/nvm/nvm.sh
.
没有 ~/.nvm
文件夹。
我可以 运行 nvm
直接从 Cloud Shell(命令行)没有问题。
我不能从 bash 文件 运行 nvm
(也不能使用 for 路径)。
最接近的尝试
运行 以下接近,但由于云 Shell 的限制,我限制了你无法 sudo
的权限:
. /usr/local/nvm/nvm.sh
nvm install 14
结果:
Downloading and installing node v14.15.4...
mkdir: cannot create directory ‘/usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64’: Permission denied
creating directory /usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64/files failed
Downloading https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz...
Warning: Failed to create the file
Warning: /usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64/node-v14.15.4-linux-
Warning: x64.tar.xz: No such file or directory
curl: (23) Failed writing body (0 != 966)
Binary download from https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz failed, trying source.
grep: /usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64/node-v14.15.4-linux-x64.tar.xz: No such file or directory
Provided file to checksum does not exist.
Binary download failed, trying source.
Detected that you have 4 CPU core(s)
Running with 3 threads to speed up the build
mkdir: cannot create directory ‘/usr/local/nvm/.cache/src’: Permission denied
creating directory /usr/local/nvm/.cache/src/node-v14.15.4/files failed
Downloading https://nodejs.org/dist/v14.15.4/node-v14.15.4.tar.xz...
Warning: Failed to create the file
Warning: /usr/local/nvm/.cache/src/node-v14.15.4/node-v14.15.4.tar.xz: No such
Warning: file or directory
curl: (23) Failed writing body (0 != 965)
Binary download from https://nodejs.org/dist/v14.15.4/node-v14.15.4.tar.xz failed, trying source.
grep: /usr/local/nvm/.cache/src/node-v14.15.4/node-v14.15.4.tar.xz: No such file or directory
Provided file to checksum does not exist.
重要行:mkdir: cannot create directory ‘...’: Permission denied
我不能sudo
绕过它,运行usermod
(例如usermod -a -G staff $(whoami)
),尽管Google's documentation says I should be able to:
When you set up a Cloud Shell session, you get a regular Unix user account with a username based on your email address. With this access, you have full root privileges on your allocated VM and can even run
sudo
commands, if you need to.
问题
我们如何在 GCP 云 Shell 的 bash 脚本文件中使用 nvm
?
我在 @Kolban and @HarshManvar 的评论的帮助下设法解决了这个问题。
我认为此解决方案也适用于无法处理来自 .customize_environment
.
下面的示例受 answer found here 的影响。该代码安装特定节点版本。
注意:nvm
默认全局安装在 Cloud Shell 中。
#!/usr/bin/bash
sudo su -c '. /usr/local/nvm/nvm.sh && nvm install 14'