centos install ruby

How To Install Ruby on Rails with rbenv on CentOS 7

Mar 16, 2015 Ruby on Rails, Ruby CentOS

Introduction

Ruby on Rails is an extremely popular open-source web framework that provides a great way to write web applications with Ruby.

This tutorial will show you how to install Ruby on Rails on CentOS 7, using rbenv. This will provide you with a solid environment for developing your Ruby on Rails applications. rbenv provides an easy way to install and manage various versions of Ruby, and it is simpler and less intrusive than RVM. This will help you ensure that the Ruby version you are developing against matches your production environment.

Prerequisites

Before installing rbenv, you must have access to a superuser account on a CentOS 7 server. Follow steps 1-3 of this tutorial, if you need help setting this up: Initial Server Setup with CentOS 7.

When you have the prerequisites out of the way, let's move on to installing rbenv.

Install rbenv

Let's install rbenv, which we will use to install and manage our Ruby installation.

Install the rbenv and Ruby dependencies with yum:

sudo yum install -y git-core zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl sqlite-devel

Now we are ready to install rbenv. The easiest way to do that is to run these commands, as the user that will be using Ruby:

cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
exec $SHELL

git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
exec $SHELL

This installs rbenv into your home directory, and sets the appropriate environment variables that will allow rbenv to the active version of Ruby.

Now we're ready to install Ruby.

Install Ruby

Before using rbenv, determine which version of Ruby that you want to install. We will install the latest version, Ruby 2.2.1.

As the user that will be using Ruby, install it with these commands:

rbenv install -v 2.2.1
rbenv global 2.2.1

The global sub-command sets the default version of Ruby that all of your shells will use. If you want to install and use a different version, simply run the rbenv commands with a different version number.

Verify that Ruby was installed properly with this command:

ruby -v

It is likely that you will not want Rubygems to generate local documentation for each gem that you install, as this process can be lengthy. To disable this, run this command:

echo "gem: --no-document" > ~/.gemrc

You will also want to install the bundler gem, to manage your application dependencies:

gem install bundler

Now that Ruby is installed, let's install Rails.

Install Rails

As the same user, install Rails 4.2.0 with this command:

gem install rails -v 4.2.0

Whenever you install a new version of Ruby or a gem that provides commands, you should run the rehash sub-command. This will install shims for all Ruby executables known to rbenv, which will allow you to use the executables:

rbenv rehash

Verify that Rails has been installed properly by printing its version, with this command:

rails -v

If it installed properly, you will see this output: Rails 4.2.0.

Install Javascript Runtime

A few Rails features, such as the Asset Pipeline, depend on a Javascript runtime. We will install Node.js to provide this functionality.

Add the EPEL yum repository:

sudo yum -y install epel-release

Then install the Node.js package:

sudo yum install nodejs

Note: This will probably not install the latest release of Node.js, as Enterprise Linux does not consider it to be "stable". If you want to install the latest version, feel free to build it on your own.

Congratulations! Ruby on Rails is now installed on your system.

Optional Steps

If you're looking to improve your setup, here are a few suggestions:

Configure Git

A good version control system is essential when coding applications. Follow the How To Set Up Git section of the How To Install Git tutorial.

Install a Database

Rails uses sqlite3 as its default database, which may not meet the requirements of your application. You may want to install an RDBMS, such as MySQL or PostgreSQL, for this purpose.

For example, if you want to use MariaDB as your database, install it with yum:

sudo yum install mariadb-server mariadb-devel

Then install the mysql2 gem, like this:

gem install mysql2

Now you can use MariaDB with your Rails application. Be sure to configure MariaDB and your Rails application properly.

Create a Test Application (Optional)

If you want to make sure that your Ruby on Rails installation went smoothly, you can quickly create a test application to test it out. For simplicity, our test application will use sqlite3 for its database.

Create a new Rails application in your home directory:

cd ~
rails new testapp

Then move into the application's directory:

cd testapp

Create the sqlite3 database:

rake db:create

If you don't already know the public IP address of your server, look it up with this command:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Copy the IPv4 address to your clipboard, then use it with this command to start your Rails application (substitute the highlighted part with the IP address):

rails server --binding=server_public_IP

If it is working properly, your Rails application should be running on port 3000 of the public IP address of your server. Visit your Rails application by going there in a web browser:

http://server_public_IP:3000

If you see the Rails "Welcome aboard" page, your Ruby on Rails installation is working properly!

Conclusion

You're now ready to start developing your new Ruby on Rails application. Good luck!

### 如何在 CentOS 系统中更新 Ruby 至最新版本 为了确保在 CentOS 中成功将 Ruby 更新至最新版本,可以按照以下方法操作: #### 1. 检查当前已安装的 Ruby 版本 运行 `ruby -v` 命令来确认当前系统的 Ruby 版本。这一步非常重要,因为它可以帮助我们了解目前使用的版本以及后续升级的目标版本[^1]。 ```bash ruby -v ``` 如果显示的是较低版本(如默认的 2.0.0),则需要继续执行下面的操作。 --- #### 2. 使用 RVM 或 rbenv 进行管理 推荐使用 **RVM**(Ruby Version Manager)或 **rbenv** 来管理和切换不同版本的 Ruby。这两种工具都可以帮助更方便地安装和维护多个 Ruby 版本。 ##### 方法一:通过 RVM 安装并升级 Ruby - 如果尚未安装 RVM,则可以通过以下命令安装它: ```bash gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB curl -sSL https://get.rvm.io | bash -s stable source /etc/profile.d/rvm.sh ``` - 接下来,使用 RVM 查找可用的 Ruby 版本列表,并选择最新的稳定版进行安装: ```bash rvm list known rvm install ruby-latest-stable ``` - 设置新安装的 Ruby 作为默认版本: ```bash rvm use ruby-latest-stable --default ``` 验证是否成功设置新的 Ruby 版本: ```bash ruby -v ``` 此方式能够有效解决因系统重启而导致的旧版本回滚问题[^5]。 --- ##### 方法二:通过 rbenv 和 ruby-build 安装并升级 Ruby 另一种常用的方法是利用 **rbenv** 工具配合 **ruby-build 插件** 实现 Ruby 的多版本管理。 - 首先克隆 rbenv 及其插件到本地环境: ```bash git clone https://github.com/rbenv/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc exec $SHELL git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build ``` - 列出可选的 Ruby 版本号,并指定目标版本进行安装: ```bash rbenv install -l rbenv install X.X.X # 替换为具体版本号 rbenv global X.X.X # 设定全局默认版本 ``` 最后再次校验 Ruby 是否被正确配置: ```bash ruby -v ``` 对于某些情况下遇到虚拟机重启后恢复原状的现象,可通过编辑 `/etc/profile.d/rh-rubyXX.sh` 文件的方式锁定所需版本[^3]。 --- #### 3. 修改路径变量以支持高优先级加载自定义 Ruby 当存在多种 Ruby 解析器共存时,需调整 `$PATH` 环境变量顺序使用户手动编译的新版本获得更高调用权。例如,在 `.bash_profile` 或者 `.zshrc` 添加如下内容: ```bash export PATH=~/.rbenv/shims:/opt/rubies/latest/bin:$PATH ``` 随后重新加载 shell 并测试效果。 --- ### 总结 无论是采用 RVM 抑或是 rbenv 方案,均能达成快速便捷地更换 CentOSRuby 主干分支的目的。同时注意处理好潜在冲突文件的影响范围,比如修改启动脚本来持久化更改后的状态][^[^35]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值