解决:Linux:bash: git: command not found

1. 问题

今天打算在Linux使用git拉取github上的代码,结果出现如下所示的问题:

(pytorch) root@dev-zzx-share:~/wyf/ModelStealing# git clone https://github.com/zlh-thu/StealingVerification.git
bash: git: command not found

于是下面就开始我的解决问题之旅。

2. 解决方案

2.1 安装git

上面提示的错误就是我没有在Linux上安装git,所以使用下面命令即可:

apt-get install git -y

安装完成后又出现了下面错误:

(pytorch) root@dev-zzx-share:~/wyf/ModelStealing#  apt-get install git -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package git is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'git' has no installation candidate

2.2 升级 apt-get

网上查了些资料说我的apt-get 版本太低,所以需要升级下,输入下面这句命令:

apt-get update

2.3 重新安装git

输入下面这句命令:

apt-get install git -y

出现下面这些提示就大功告成了:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  git-man less libcurl3-gnutls liberror-perl
Suggested packages:
  git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn
The following NEW packages will be installed:
  git git-man less libcurl3-gnutls liberror-perl
0 upgraded, 5 newly installed, 0 to remove and 235 not upgraded.
Need to get 5079 kB of archives.
After this operation, 34.9 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 less amd64 487-0.1 [112 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libcurl3-gnutls amd64 7.58.0-2ubuntu3.16 [218 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic/main amd64 liberror-perl all 0.17025-1 [22.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 git-man all 1:2.17.1-1ubuntu0.9 [803 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 git amd64 1:2.17.1-1ubuntu0.9 [3923 kB]
Fetched 5079 kB in 4s (1449 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package less.
(Reading database ... 84893 files and directories currently installed.)
Preparing to unpack .../less_487-0.1_amd64.deb ...
Unpacking less (487-0.1) ...
Selecting previously unselected package libcurl3-gnutls:amd64.
Preparing to unpack .../libcurl3-gnutls_7.58.0-2ubuntu3.16_amd64.deb ...
Unpacking libcurl3-gnutls:amd64 (7.58.0-2ubuntu3.16) ...
Selecting previously unselected package liberror-perl.
Preparing to unpack .../liberror-perl_0.17025-1_all.deb ...
Unpacking liberror-perl (0.17025-1) ...
Selecting previously unselected package git-man.
Preparing to unpack .../git-man_1%3a2.17.1-1ubuntu0.9_all.deb ...
Unpacking git-man (1:2.17.1-1ubuntu0.9) ...
Selecting previously unselected package git.
Preparing to unpack .../git_1%3a2.17.1-1ubuntu0.9_amd64.deb ...
Unpacking git (1:2.17.1-1ubuntu0.9) ...
Setting up git-man (1:2.17.1-1ubuntu0.9) ...
Setting up less (487-0.1) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
Setting up liberror-perl (0.17025-1) ...
Setting up libcurl3-gnutls:amd64 (7.58.0-2ubuntu3.16) ...
Setting up git (1:2.17.1-1ubuntu0.9) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

3. 参考文献

1.解决: Linux – git: command not found
2. 安装git提示E: Package ‘git‘ has no installation candidate

当你在 Git Bash 中运行 `npm` 命令时遇到“command not found”的错误,这通常意味着系统无法找到 npm 的安装路径。以下是几种常见的解决方案: ### 1. 确认 Node.js 和 NPM 是否已正确安装 首先,你需要确认是否已经成功安装了 Node.js 及其自带的包管理工具 NPM。 - 打开Git Bash终端输入以下命令检查Node版本: ```bash node -v ``` - 输入以下命令查看NPM版本 ```bash npm -v ``` 如果这两个命令都返回相应的版本信息,则说明它们已经被正常安装;反之则需要前往[官方网址](https://nodejs.org/)下载最新版并按照指示完成安装过程。 ### 2. 将 NPM 添加到环境变量 Path 中 如果你确定 Node.js 和 NPM 已经安装好了但是依旧报错,那可能是由于环境变量配置的问题导致找不到NPM的位置。这时你可以尝试手动添加NPM所在的文件夹路径至系统的Path环境变量里。 对于 Windows 用户来说,在安装过程中会自动将必要的目录加入PATH,但如果未生效可以自行添加: - 查找全局模块存放位置,例如通过命令行获取默认值: ```bash npm config get prefix ``` - 根据提示结果将该地址下的 `\bin` 文件夹全路径复制下来; - 按Win+Pause键打开"系统属性">高级设置>环境变量; - 在用户或系统级别的 "Path" 下点击编辑按钮,并新增上述路径作为一项内容保存即可重启电脑让更改生效。 如果是 Linux 或 macOS 则可以直接编辑 shell 配置文件如 `.bashrc`,`.zshrc`等, 加入类似下面一行代码(假设你是用nvm管理node) : ```bash export PATH=$HOME/.nvm/versions/node/v$(node -v)/bin:$PATH ``` 然后执行source加载使之立即有效: ```bash source ~/.bashrc # or source ~/.zshrc if you're using zsh ``` ### 3. 直接在 Git Bash 内启动 NPM 脚本 有时为了临时解决问题可以在当前shell session里面直接导出path而不需要永久修改环境变量: ```bash export PATH="$PATH:/c/Users/<YourUsername>/AppData/Roaming/npm" # 对于Linux/MacOS则是类似于这样的形式 export PATH="/usr/local/bin:$PATH" ``` 请注意替换 `<YourUsername>` 为你实际用户名的部分。这种方式仅对本次会话起作用,关闭窗口之后还需要再次设置。 以上步骤应该能够帮助大多数开发者解决git bash:npm: command not found” 的问题。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值