RVM:指定要使用的ruby版本

时间:2022-04-24 20:46:40

I know how to use RVM, but now I have a weird problem, which I do not understand why.

我知道如何使用RVM,但是现在我有一个奇怪的问题,我不明白为什么。

Here is the simple story (I am using Ubuntu):

这里有一个简单的故事(我正在使用Ubuntu):

I have created a Rails project, the direcotry of this project is "bookstore/".

我创建了一个Rails项目,这个项目的direcotry是“bookstore/”。

I go to project directory by cd bookstore , and type command rvm list like following:

我按cd书店进入项目目录,输入命令rvm列表如下:

bookstore/$ rvm list

rvm rubies

   ruby-1.9.2-p136 [ i386 ]
   ruby-1.8.7-p352 [ i386 ]
   ruby-1.8.7-p330 [ i386 ]
   ruby-1.8.6-p420 [ i386 ]
   ruby-1.9.2-p290 [ i386 ]

Since I did not see the => arrow sign which is supposed to indicate the current ruby version in use, so I specify the ruby version with the following RVM command:

由于我没有看到=>箭头符号,该符号表示当前使用的ruby版本,所以我使用以下RVM命令指定ruby版本:

bookstore/$ rvm use ruby-1.9.2-p290
Using /home/usr/.rvm/gems/ruby-1.9.2-p290

Now, if I rvm list I see my project is using ruby v1.9.2 :

现在,如果我使用rvm列表,我看到我的项目正在使用ruby v1.9.2:

bookstore/$ rvm list
rvm rubies

   ruby-1.9.2-p136 [ i386 ]
   ruby-1.8.7-p352 [ i386 ]
   ruby-1.8.7-p330 [ i386 ]
   ruby-1.8.6-p420 [ i386 ]
=> ruby-1.9.2-p290 [ i386 ]

Every thing works fine at this point!

在这一点上一切都很正常!

But, if now I open a new terminal window on Ubuntu, and cd to the project directory, and run the command rvm list again, I got:

但是,如果现在我在Ubuntu上打开一个新的终端窗口,cd到项目目录,再运行命令rvm列表,我得到:

bookstore/$ rvm list

rvm rubies

    ruby-1.9.2-p136 [ i386 ]
    ruby-1.8.7-p352 [ i386 ]
    ruby-1.8.7-p330 [ i386 ]
    ruby-1.8.6-p420 [ i386 ]
    ruby-1.9.2-p290 [ i386 ]

Where is the => to indicate the ruby version I specified previously? Why it again needs me to specify the ruby version?

Where is =>表示我之前指定的ruby版本?为什么需要我来指定ruby版本?

It happens always when I open a new terminal window. How to have my project "remember" the ruby version I have specified?

当我打开一个新的终端窗口时,它总是会发生。如何让我的项目“记住”我指定的ruby版本?

2 个解决方案

#1


28  

Dave is right, you should set a default. But also, look into defining an .rvmrc file on a per-project or per-machine basis. I use project-specific rvmrc files, so I can use different rubies and gemsets for each project, and changing into the directory automatically switches to that project's ruby/gemset.

Dave是对的,你应该设置一个默认值。但是,还要考虑在每个项目或每个机器的基础上定义.rvmrc文件。我使用项目特定的rvmrc文件,因此我可以为每个项目使用不同的ruby和gemsets,并自动切换到该项目的ruby/gemset目录。

For example, my rvmrc for company site project:

例如,我的公司网站项目rvmrc:

brett@bender:~/Sites/bearded/bearded(master)$ cat .rvmrc 
rvm 1.9.3@bearded

Edit: For explicitness' sake, to solve your problem using an rvmrc file, do the following (assuming you already installed the ruby version you want and created a gemset for this project's gems):

编辑:为了明确起见,要使用rvmrc文件解决问题,请执行以下操作(假设您已经安装了所需的ruby版本,并为该项目的gems创建了gemset):

  1. Create a file in bookstore/ directory named .rvmrc (in your favorite editor)
  2. 在书店/目录中创建一个名为.rvmrc的文件(在您最喜欢的编辑器中)
  3. Add rvm ruby-1.9.2-p290 to the file and save it (you can use rvm ruby-1.9.2-p290@gemset_name if you have a gemset you want to default to)
  4. 向文件中添加rvm ruby-1.9.2-p290并保存它(如果您有一个想默认为的gemset,您可以使用rvm ruby-1.9.2-p290@gemset_name)
  5. Change directory out of your bookstore directory, then change back into it.
  6. 将目录从您的书店目录中更改,然后重新回到它。
  7. RVM should ask you if you want to trust this .rvmrc file (yes)
  8. RVM应该问您是否要信任这个.rvmrc文件(是的)
  9. RVM should have automatically switched your active ruby and gemset to the ones specified in your rvmrc file for that project.
  10. RVM应该自动地将活动的ruby和gemset切换到rvmrc文件中指定的项目。

Also note that if your RVM is older than version 1.8.0 you will need to turn on rvmrc file support (versions 1.8.0+ have it turned on by default). The link at the top of my question contains instructions if you're so inclined.

还要注意,如果您的RVM比1.8.0版本更早,您将需要打开rvmrc文件支持(版本1.8.0+默认打开)。如果你这么想的话,我的问题的顶部的链接包含了指令。

#2


8  

You need to set the default.

您需要设置默认值。

rvm --default 1.9.2-p290  # Or whichever.

A new shell is a new environment; it will not (normally) inherit from already-opened terminals

新的外壳是新的环境;它(通常)不会从已经打开的终端继承

For per-project settings, use a .rvmrc file in the root of your project, for example:

对于每个项目设置,请在项目的根目录中使用.rvmrc文件,例如:

rvm --create gemset use 1.9.2-p0@my_project

The --create will create the gemset if it does not already exist, handy if you or others work on the same project across machines.

如果gemset还不存在,create将创建它,如果您或其他人跨机器处理相同的项目,它将非常方便。

#1


28  

Dave is right, you should set a default. But also, look into defining an .rvmrc file on a per-project or per-machine basis. I use project-specific rvmrc files, so I can use different rubies and gemsets for each project, and changing into the directory automatically switches to that project's ruby/gemset.

Dave是对的,你应该设置一个默认值。但是,还要考虑在每个项目或每个机器的基础上定义.rvmrc文件。我使用项目特定的rvmrc文件,因此我可以为每个项目使用不同的ruby和gemsets,并自动切换到该项目的ruby/gemset目录。

For example, my rvmrc for company site project:

例如,我的公司网站项目rvmrc:

brett@bender:~/Sites/bearded/bearded(master)$ cat .rvmrc 
rvm 1.9.3@bearded

Edit: For explicitness' sake, to solve your problem using an rvmrc file, do the following (assuming you already installed the ruby version you want and created a gemset for this project's gems):

编辑:为了明确起见,要使用rvmrc文件解决问题,请执行以下操作(假设您已经安装了所需的ruby版本,并为该项目的gems创建了gemset):

  1. Create a file in bookstore/ directory named .rvmrc (in your favorite editor)
  2. 在书店/目录中创建一个名为.rvmrc的文件(在您最喜欢的编辑器中)
  3. Add rvm ruby-1.9.2-p290 to the file and save it (you can use rvm ruby-1.9.2-p290@gemset_name if you have a gemset you want to default to)
  4. 向文件中添加rvm ruby-1.9.2-p290并保存它(如果您有一个想默认为的gemset,您可以使用rvm ruby-1.9.2-p290@gemset_name)
  5. Change directory out of your bookstore directory, then change back into it.
  6. 将目录从您的书店目录中更改,然后重新回到它。
  7. RVM should ask you if you want to trust this .rvmrc file (yes)
  8. RVM应该问您是否要信任这个.rvmrc文件(是的)
  9. RVM should have automatically switched your active ruby and gemset to the ones specified in your rvmrc file for that project.
  10. RVM应该自动地将活动的ruby和gemset切换到rvmrc文件中指定的项目。

Also note that if your RVM is older than version 1.8.0 you will need to turn on rvmrc file support (versions 1.8.0+ have it turned on by default). The link at the top of my question contains instructions if you're so inclined.

还要注意,如果您的RVM比1.8.0版本更早,您将需要打开rvmrc文件支持(版本1.8.0+默认打开)。如果你这么想的话,我的问题的顶部的链接包含了指令。

#2


8  

You need to set the default.

您需要设置默认值。

rvm --default 1.9.2-p290  # Or whichever.

A new shell is a new environment; it will not (normally) inherit from already-opened terminals

新的外壳是新的环境;它(通常)不会从已经打开的终端继承

For per-project settings, use a .rvmrc file in the root of your project, for example:

对于每个项目设置,请在项目的根目录中使用.rvmrc文件,例如:

rvm --create gemset use 1.9.2-p0@my_project

The --create will create the gemset if it does not already exist, handy if you or others work on the same project across machines.

如果gemset还不存在,create将创建它,如果您或其他人跨机器处理相同的项目,它将非常方便。