How To Install Ruby with rbenv on Cent OS 7.2

来源:互联网 发布:安卓富文本编辑器源码 编辑:程序博客网 时间:2024/06/05 08:19
[root@contoso ~]# yum install -y openssl-devel readline-devel zlib-devel


Now we are ready to install rbenv. Let's clone the rbenv repository from git. You should complete these steps from the user account from which you plan to run Ruby.


[root@contoso ~]# git clone https://github.com/rbenv/rbenv.git ~/.rbenv
From here, you should add ~/.rbenv/bin to your $PATH so that you can use rbenv's command line utility. Also adding ~/.rbenv/bin/rbenv init to your ~/.bash_profile will let you load rbenv automatically.


[root@contoso ~]# echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
[root@contoso ~]# echo 'eval "$(rbenv init -)"' >> ~/.bashrc
Next, source rbenv by typing:


[root@contoso ~]# source ~/.bashrc
You can check to see if rbenv was set up properly by using the type command, which will display more information about rbenv:


[root@contoso ~]# type rbenv
Your terminal window should output the following:


Output
rbenv is a function
rbenv () 

    local command;
    command="$1";
    if [ "$#" -gt 0 ]; then
        shift;
    fi;
    case "$command" in 
        rehash | shell)
            eval "$(rbenv "sh-$command" "$@")"
        ;;
        *)
            command rbenv "$command" "$@"
        ;;
    esac
}
In order to use the rbenv install command, which simplifies the installation process for new versions of Ruby, you should install ruby-build, which we will install as a plugin for rbenv through git:


[root@contoso ~]# git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
At this point, you should have both rbenv and ruby-build installed, and we can move on to installing Ruby.


Install Ruby
With the ruby-build rbenv plugin now installed, we can install whatever versions of Ruby that we may need through a simple command. First, let's list all the available versions of Ruby:


[root@contoso ~]# rbenv install -l
The output of that command should be a long list of versions that you can choose to install.


We'll now install a particular version of Ruby. It's important to keep in mind that installing Ruby can be a lengthy process, so be prepared for the installation to take some time to complete.


As an example here, let's install Ruby version 2.3.1, and once it's done installing, we can set it as our default version with the global sub-command:


[root@contoso ~]# rbenv install 2.4.1
[root@contoso ~]# rbenv global 2.4.1
If you would like to install and use a different version, simply run the rbenv commands with a different version number, as in rbenv install 2.3.0 and rbenv global 2.3.0.


Verify that Ruby was properly installed by checking your version number:




[root@contoso ~]# rbenv global 2.4.1


 
If you installed version 2.4.1 of Ruby, your output to the above command should look something like this:


Output
[root@contoso ~]# ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
You now have at least one version of Ruby installed and have set your default Ruby version. Next, we will set up gems and Rails.


Working with Gems
Gems are packages that extend the functionality of Ruby. We will want to install Rails through the gem command.


So that the process of installing Rails is less lengthy, we will turn off local documentation for each gem we install. We will also install the bundler gem to manage application dependencies:


[root@contoso ~]# echo "gem: --no-document" > ~/.gemrc
[root@contoso ~]# gem install bundler
Fetching: bundler-1.15.4.gem (100%)
Successfully installed bundler-1.15.4
1 gem installed
You can use the gem env command (the subcommand env is short for environment) to learn more about the environment and configuration of gems. You can check the location where gems are being installed by using the home argument, which will show the pathway to where gems are installed on your server.


[root@contoso ~]# gem env home
Your output should look something like this, with root being the name of the user:


[root@contoso ~]# gem env home
/root/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0




Uninstalling Ruby versions


[root@contoso ~]# rbenv uninstall 2.4.1
原创粉丝点击