Created
December 27, 2017 08:18
-
-
Save nomadalex/4b6c48239830d3b69da3aa6d5f62bc7b to your computer and use it in GitHub Desktop.
install rbenv on centos 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
prog=${0##*/} | |
function usage () | |
{ | |
cat << EOF | |
NAME | |
$prog - Install rbenv. | |
SYNOPSIS | |
$prog [OPTIONS] | |
DESCRIPTION | |
A bash script for install rbenv. | |
OPTIONS | |
-h - show this help or help for a subcommand | |
-i - install centos depends with sudo | |
-d - development mode | |
-c - use china mirror | |
-v ruby_version - ruby version to install | |
EOF | |
} | |
while getopts "idcv:h" opt | |
do | |
case "$opt" in | |
i) INSTALL_DEPS=1 ;; | |
d) DEVELOPMENT=1 ;; | |
c) CHINA_MIRROR=1 ;; | |
v) RUBY_VERSION="${OPTARG}" ;; | |
h|*) usage; exit 1 ;; | |
esac | |
done | |
RUBY_VERSION=${RUBY_VERSION:-2.5.0} | |
cd ~ | |
if [ "${INSTALL_DEPS}" -eq 1 ]; then | |
# install build dependencies | |
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 | |
fi | |
# clone and install rbenv environment | |
git clone git://github.com/sstephenson/rbenv.git ~/.rbenv | |
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile | |
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile | |
# re-init bash | |
source ~/.bash_profile | |
# 用来编译安装 ruby | |
git clone git://github.com/sstephenson/ruby-build.git "$(rbenv root)"/plugins/ruby-build | |
if [ "${CHINA_MIRROR}" -eq 1 ]; then | |
# 使用 Ruby China 的镜像安装 Ruby, 国内用户推荐 | |
git clone https://github.com/andorchen/rbenv-china-mirror.git "$(rbenv root)"/plugins/rbenv-china-mirror | |
fi | |
if [ "${DEVELOPMENT}" -eq 1 ]; then | |
# 通过 gem 命令安装完 gem 后无需手动输入 rbenv rehash 命令, 推荐 | |
git clone git://github.com/sstephenson/rbenv-gem-rehash.git "$(rbenv root)"/plugins/rbenv-gem-rehash | |
# 通过 rbenv update 命令来更新 rbenv 以及所有插件, 推荐 | |
git clone git://github.com/rkh/rbenv-update.git "$(rbenv root)"/plugins/rbenv-update | |
fi | |
# install latest ruby | |
rbenv install -v $RUBY_VERSION | |
# sets the default ruby version that the shell will use | |
rbenv global $RUBY_VERSION | |
if [ "${CHINA_MIRROR}" -eq 1 ]; then | |
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ | |
fi | |
if [ "${DEVELOPMENT}" -ne 1 ]; then | |
# to disable generating of documents as that would take so much time | |
echo "gem: --no-document" > ~/.gemrc | |
fi | |
# install bundler | |
gem install bundler | |
# must be executed everytime a gem has been installed in order for the ruby executable to run | |
rbenv rehash | |
if [ "${CHINA_MIRROR}" -eq 1 ]; then | |
bundle config mirror.https://rubygems.org https://gems.ruby-china.org | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment