如何将 Ruby 添加到 Windows 上的 PATH 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26947427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add Ruby to the PATH variable on Windows?
提问by kburlz
I have Ruby installed, but I still need to add it to the PATH variable. I found something online for how to manually add it using the command line:
我已经安装了 Ruby,但我仍然需要将它添加到 PATH 变量中。我在网上找到了一些关于如何使用命令行手动添加它的信息:
set PATH=C:\Ruby200-x64\bin;%PATH%
But before I try it, I want to be sure it's not going to overwrite what's currently in the PATH variable. (I have no experience with this stuff so I don't know what to expect).
但在我尝试之前,我想确保它不会覆盖当前在 PATH 变量中的内容。(我没有这方面的经验,所以我不知道会发生什么)。
Thanks in advance for your help!
在此先感谢您的帮助!
回答by Chosmos
first, notice that this question is not really about Ruby, rather about how to set a path in windows (it work the same way if you want to add an executable different from Ruby)
首先,请注意这个问题并不是关于 Ruby,而是关于如何在 Windows 中设置路径(如果你想添加一个不同于 Ruby 的可执行文件,它的工作方式是一样的)
second, you are not overwriting the PATH environment variable because you add the existing content of the same to the new one you are setting in:
其次,您不会覆盖 PATH 环境变量,因为您将相同的现有内容添加到您正在设置的新内容中:
set PATH=C:\Ruby200-x64\bin;%PATH%
the %PATH%
is the current content of the PATH variable.
的%PATH%
是PATH变量的当前内容。
Consider using
考虑使用
set PATH=%PATH%;C:\Ruby200-x64\bin
instead, this will make your OS search the original path before searching the ruby bin folder. Maybe it makes few difference on modern computers, but my old DOS days claim the second solution is better.
相反,这将使您的操作系统在搜索 ruby bin 文件夹之前搜索原始路径。也许它在现代计算机上几乎没有什么不同,但是我过去的 DOS 时代声称第二种解决方案更好。
third and last point, in Windows you can set environment variables in control panel / system properties How to get there depends on the version of your OS, but if you search for the ambient variables and system variables you should get there.
第三点也是最后一点,在 Windows 中,您可以在控制面板/系统属性中设置环境变量 如何到达那里取决于您的操作系统版本,但是如果您搜索环境变量和系统变量,您应该到达那里。
回答by Kamil Lelonek
- From the Desktop, right-click the very bottom left corner of the screen to get the Task Menu.
- From the Task Menu, click System.
- Click the Advanced System Settings link in the left column.
- In the System Properties window, click on the Advanced tab, then click the Environment Variables button near the bottom of that tab.
- In the Environment Variables window (pictured below), highlight the Path variable in the "System variables" section and click the Edit button.
Add or modify the path lines with the paths you want the computer to access. For ruby it will be:
;YOUR_RUBY_INSTALLATION_PATH\bin;
- 在桌面上,右键单击屏幕的左下角以获取任务菜单。
- 从任务菜单中,单击系统。
- 单击左栏中的高级系统设置链接。
- 在“系统属性”窗口中,单击“高级”选项卡,然后单击该选项卡底部附近的“环境变量”按钮。
- 在“环境变量”窗口(如下图)中,突出显示“系统变量”部分中的 Path 变量,然后单击“编辑”按钮。
添加或修改带有您希望计算机访问的路径的路径行。对于红宝石,它将是:
;YOUR_RUBY_INSTALLATION_PATH\bin;
The operation with set PATH=C:\Ruby200-x64\bin;%PATH%
is probably only temporary until you restart your computer.
操作set PATH=C:\Ruby200-x64\bin;%PATH%
可能只是暂时的,直到您重新启动计算机。
回答by agm1984
I just wanted to let everyone know that when you install rubyinstaller
on Windows and follow its steps, there is no option to 'add to path variables' because it automatically adds it.
我只是想让大家知道,当您rubyinstaller
在 Windows 上安装并按照其步骤操作时,没有“添加到路径变量”的选项,因为它会自动添加它。
Rubyinstaller
trolled me hard because it said gem
not found when I did gem install sass
immediately after install.
Rubyinstaller
狠狠地折磨我,因为它说gem
在我gem install sass
安装后立即执行时找不到。
Your path variable is probably already set if you used
rubyinstaller
.The trick is to open CMD or I would imagine, PowerShell, ConEMU, etc, git bash, and type
gem
.
- Press WINKEYand type
cmd
- Type
gem install sass
(or whatever else that is in the bin folder for Ruby)
如果您使用
rubyinstaller
.诀窍是打开 CMD 或者我想像的 PowerShell、ConEMU 等,git bash,然后输入
gem
.
- 按下WINKEY并输入
cmd
- 键入
gem install sass
(或 Ruby 的 bin 文件夹中的任何其他内容)
I just went to add the PATH variable, and it was already set, so my problem was the garbage command line tool that opened after installing rubyinstaller
.
我刚去添加PATH变量,已经设置好了,所以我的问题是安装后打开的垃圾命令行工具rubyinstaller
。
You can also do these stepsto add to the PATH variables:
您还可以执行以下步骤来添加到 PATH 变量:
- Press WINKEY
- Type
view advanced system settings
- Open that
- Click
Environment Variables
- Click
Path
in the list - Click
Edit
- Check if
C:\Ruby24-x64\bin
is already there, if so, done - Click
New
and type inC:\Ruby24-x64\bin
- Done
- 按 WINKEY
- 类型
view advanced system settings
- 打开那个
- 点击
Environment Variables
Path
在列表中点击- 点击
Edit
- 检查是否
C:\Ruby24-x64\bin
已经存在,如果存在,则完成 - 点击
New
并输入C:\Ruby24-x64\bin
- 完毕
回答by glucas
Yes, this is correct. In your example %PATH%
will be expanded to the current value of the PATH variable, so this command is effectively adding a new entry to the beginning of the PATH.
是的,这是正确的。在您的示例中,%PATH%
将扩展为 PATH 变量的当前值,因此此命令实际上是在 PATH 的开头添加了一个新条目。
Note that calling set PATH
will only affect the current shell. If you want to make this change permanent for all shells, the simplest option is to set it as a user variable using the Environment Variables dialog box.
请注意,调用set PATH
只会影响当前的 shell。如果要使此更改对所有 shell 永久生效,最简单的选择是使用环境变量对话框将其设置为用户变量。
On Windows 8 you can open this dialog by hitting Win+s and searching for 'environment variables'. On earlier versions of Windows you can right-click 'My Computer', choose Properties, then Advanced system settings, then Environment variables. You can create (or update) a PATH variable in the user variables section and add whatever entries you need. These will be appended to the existing system path. If you take this approach you will need to open a new cmd shell after updating the variables.
在 Windows 8 上,您可以通过按 Win+s 并搜索“环境变量”来打开此对话框。在早期版本的 Windows 上,您可以右键单击“我的电脑”,选择“属性”,然后选择“高级系统设置”,然后选择“环境变量”。您可以在用户变量部分创建(或更新)一个 PATH 变量并添加您需要的任何条目。这些将附加到现有的系统路径。如果您采用这种方法,您将需要在更新变量后打开一个新的 cmd shell。
回答by Nabil Kadimi
Fear nothing, What you are doing is prepend C:\Ruby200-x64\bin
to the existing %PATH%
, this is what the command you posted does.
不要害怕,你正在做的是C:\Ruby200-x64\bin
在现有的前面%PATH%
,这就是你发布的命令所做的。
The path is a list of directories, separated by ;
, in which the system will look for the command you execute.
路径是一个目录列表,以 分隔;
,系统将在其中查找您执行的命令。
In your case it's:
在你的情况下是:
C:\Ruby200-x64\bin
%PATH%
, if you print it on the command line, you'll find it's itself a list of directories separated by;
.
C:\Ruby200-x64\bin
%PATH%
, 如果你在命令行上打印它,你会发现它本身就是一个由;
.
In case you want to make your change permanent, you have to change your PATH sytemwide.
如果您想让您的更改永久化,您必须更改您的 PATH 系统范围。
回答by Ben Personick
For CLI, as noted elsewhere calling SET on the path variable only acts on the current window and closing it or restarting windows voids the change.
对于 CLI,如其他地方所述,在路径变量上调用 SET 仅作用于当前窗口,关闭它或重新启动窗口会使更改无效。
Example the transient Version selected as the answer:
例如选择作为答案的瞬态版本:
set "PATH=%PATH%;C:\Ruby200-x64\bin"
To correctly set the path permanently in CLI use the path command:
要在 CLI 中永久正确设置路径,请使用 path 命令:
PATH %PATH%;C:\Ruby200-x64\bin
This will persist between CMD windows and after reboots.
这将在 CMD 窗口之间和重新启动后持续存在。
回答by CorruptComputer
I know this is questions has the Windows tag, however it is one of the first DuckDuckGo results for "ruby gems add to path" so I just wanted to add this.
我知道这是带有 Windows 标签的问题,但是它是“ruby gems add to path”的第一个 DuckDuckGo 结果之一,所以我只想添加它。
On Linux you can add this line to the end of your .bashrc
in order to add it to your path:
在 Linux 上,您可以将此行.bashrc
添加到您的末尾,以便将其添加到您的路径中:
export PATH=$PATH:~/.gem/ruby/2.6.0/bin
export PATH=$PATH:~/.gem/ruby/2.6.0/bin