如何在Ubuntu 16.04上使用‘rbenv’安裝和配置Ruby on Rails
本文將學習如何安裝和配置Ruby on Rails,它是開發者最常用的應用框架之一,用於建立網站和網路應用。Ruby 是一種程式語言,開發者將它與Rails開發框架結合使用,可以使應用開發更快更簡單。
‘rbenv’是一個命令列工具,用於安裝Ruby和Rails,它也為使用Ruby on Rails進行應用開發提供了良好的環境。‘rbenv’提供特定應用版本的Ruby。
前提條件
我們需要一臺已安裝Ubuntu 16.04的機器,並使用非root使用者。
安裝更新和依賴項。
在開始安裝之前,我們需要更新並安裝所有相關的依賴項。
首先,我們將更新Ubuntu。要更新Ubuntu,我們需要執行以下命令。
$ sudo apt-get update
然後,我們將使用apt-get安裝rbenv和Ruby所需的所有依賴項,以下是用apt-get安裝依賴項的命令。
$ sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev Output: Reading package lists... Done Building dependency tree Reading state information... Done build-essential is already the newest version (12.1ubuntu2). build-essential set to manually installed. libgdbm3 is already the newest version (1.8.3-13.1). libgdbm3 set to manually installed. The following additional packages will be installed: automake libbison-dev libssl-doc libssl1.0.0 libtinfo-dev libyaml-0-2 m4 Suggested packages: autoconf-archive gnu-standards autoconf-doc libtool bison-doc ncurses-doc readline-doc libyaml-doc The following NEW packages will be installed: autoconf automake bison libbison-dev libffi-dev libgdbm-dev libncurses5-dev libreadline6-dev libssl-dev libssl-doc libtinfo-dev libyaml-0-2 libyaml-dev m4 zlib1g-dev The following packages will be upgraded: libssl1.0.0 1 upgraded, 15 newly installed, 0 to remove and 107 not upgraded. Need to get 4,888 kB/5,970 kB of archives. After this operation, 19.1 MB of additional disk space will be used. Do you want to continue? [Y/n] y …. …. Setting up libyaml-0-2:amd64 (0.1.6-3) ... Setting up libssl1.0.0:amd64 (1.0.2g-1ubuntu4.5) ... Setting up m4 (1.4.17-5) ... Setting up autoconf (2.69-9) ... Setting up automake (1:1.15-4ubuntu1) ... update-alternatives: using /usr/bin/automake-1.15 to provide /usr/bin/automake (automake) in auto mode Setting up libbison-dev:amd64 (2:3.0.4.dfsg-1) ... Setting up bison (2:3.0.4.dfsg-1) ... update-alternatives: using /usr/bin/bison.yacc to provide /usr/bin/yacc (yacc) in auto mode Setting up libgdbm-dev (1.8.3-13.1) ... Setting up libtinfo-dev:amd64 (6.0+20160213-1ubuntu1) ... Setting up libncurses5-dev:amd64 (6.0+20160213-1ubuntu1) ... Setting up libreadline6-dev:amd64 (6.3-8ubuntu2) ... Setting up zlib1g-dev:amd64 (1:1.2.8.dfsg-2ubuntu4) ... Setting up libssl-dev:amd64 (1.0.2g-1ubuntu4.5) ... Setting up libssl-doc (1.0.2g-1ubuntu4.5) ... Setting up libffi-dev:amd64 (3.2.1-4) ... Setting up libyaml-dev:amd64 (0.1.6-3) ... Processing triggers for libc-bin (2.23-0ubuntu3) ...
安裝‘rbenv’
我們將使用git倉庫安裝‘rbenv’,要安裝或獲取rbenv,我們需要執行以下命令。
$ git clone https://github.com/rbenv/rbenv.git /opt/.rbenv Cloning into '/root/.rbenv'... remote: Counting objects: 2542, done. remote: Total 2542 (delta 0), reused 0 (delta 0), pack-reused 2542 Receiving objects: 100% (2542/2542), 461.53 KiB | 120.00 KiB/s, done. Resolving deltas: 100% (1597/1597), done. Checking connectivity... done.
我們需要將/opt/.rbenv新增到我們的$PATH中,以便可以使用rbenv。為了將路徑新增到環境中,我們需要將其新增到~/.bash_profile檔案中。
為了新增環境變數並啟動‘rbenv’,我們需要新增以下幾行,還要新增source rbenv
$ echo 'export PATH="/opt/.rbenv/bin:$PATH"' >> ~/.bashrc $ echo 'eval "$(rbenv init -)"' >> ~/.bashrc $ source ~/.bashr
現在我們將檢查rbenv,檢查rbenv是否正常工作,我們可以執行以下命令:
$ type rbenv
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
}要使用rbenv,請安裝該命令,這使得Ruby的安裝過程更加簡單。我們應該安裝ruby-build,它作為rbenv的外掛安裝,我們將使用以下命令從git倉庫獲取該外掛。
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build Cloning into '/root/.rbenv/plugins/ruby-build'... remote: Counting objects: 6981, done. remote: Total 6981 (delta 0), reused 0 (delta 0), pack-reused 6981 Receiving objects: 100% (6981/6981), 1.35 MiB | 182.00 KiB/s, done. Resolving deltas: 100% (4151/4151), done. Checking connectivity... done.
在Ubuntu上安裝Ruby
我們將使用ruby-build(一個rbenv外掛)來安裝Ruby,我們需要執行以下命令來列出所有可用的Ruby版本。這將顯示一個很長的可用版本列表。
$ rbenv install –l Output: Available versions: 1.8.5-p113 1.8.5-p114 1.8.5-p115 1.8.5-p231 1.8.5-p52 1.8.6-p110 1.8.6-p111 1.8.6-p114 1.8.6-p230 …. … rbx-3.56 rbx-3.57 rbx-3.58 rbx-3.59 rbx-3.60 ree-1.8.7-2011.03 ree-1.8.7-2011.12 ree-1.8.7-2012.01 ree-1.8.7-2012.02 topaz-dev
我們將只安裝特定版本。例如,如果我們安裝Ruby 2.3.1版本,那麼我們可以使用global命令將其設定為預設版本。
$ sudo rbenv install 2.3.1 Output: Downloading ruby-2.3.1.tar.bz2... -> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.bz2 Installing ruby-2.3.1... Installed ruby-2.3.1 to /root/.rbenv/versions/2.3.1 $ sudo rbenv global 2.3.1
我們也可以透過執行簡單的命令rbenv install 2.3.0來擁有不同的版本。軟體安裝完成後,我們將使用以下命令驗證ruby安裝:
$ ruby –v Output: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
安裝和使用Ruby的Gem
Gem是將安裝的包,用於擴充套件Ruby的功能。我們必須使用gem命令來安裝包。我們將使用install命令和gem來安裝依賴項。
$ echo "gem: --no-document" > ~/.gemrc $ gem install bundler Output: Fetching: bundler-1.13.5.gem (100%) Successfully installed bundler-1.13.5 1 gem installed
現在我們將使用gem env命令來了解更多關於環境和gem配置的資訊,我們可以使用home引數來檢查gem安裝位置,這將顯示伺服器上安裝的gem的路徑。
$ gem env home Output: /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0
在Ubuntu上安裝Rails
我們現在將使用gem install命令安裝最新版本的Rails。以下是安裝rails的命令:
$ gem install rails Output: Successfully installed bundler-1.13.5 1 gem installed root@ubuntu-16:~# gem env home /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0 $ gem install rails Fetching: i18n-0.7.0.gem (100%) Successfully installed i18n-0.7.0 Fetching: thread_safe-0.3.5.gem (100%) Successfully installed thread_safe-0.3.5 Fetching: tzinfo-1.2.2.gem (100%) Successfully installed tzinfo-1.2.2 Fetching: concurrent-ruby-1.0.2.gem (100%) Successfully installed concurrent-ruby-1.0.2 Fetching: activesupport-5.0.0.1.gem (100%) Successfully installed activesupport-5.0.0.1 Fetching: rack-2.0.1.gem (100%) Successfully installed rack-2.0.1 Fetching: rack-test-0.6.3.gem (100%) Successfully installed rack-test-0.6.3 Fetching: mini_portile2-2.1.0.gem (100%) Successfully installed mini_portile2-2.1.0 Fetching: nokogiri-1.6.8.1.gem (100%) Building native extensions. This could take a while... … …. … Successfully installed websocket-driver-0.6.4 Fetching: actioncable-5.0.0.1.gem (100%) Successfully installed actioncable-5.0.0.1 Fetching: thor-0.19.1.gem (100%) Successfully installed thor-0.19.1 Fetching: method_source-0.8.2.gem (100%) Successfully installed method_source-0.8.2 Fetching: railties-5.0.0.1.gem (100%) Successfully installed railties-5.0.0.1 Fetching: sprockets-3.7.0.gem (100%) Successfully installed sprockets-3.7.0 Fetching: sprockets-rails-3.2.0.gem (100%) Successfully installed sprockets-rails-3.2.0 Fetching: rails-5.0.0.1.gem (100%) Successfully installed rails-5.0.0.1
安裝包後,我們將驗證Rails是否正確安裝,我們需要執行以下命令。
$ rails –v Output: Rails 5.0.0.1
安裝Java執行時
由於一些Rails功能依賴於JavaScript執行時環境,我們需要安裝Node.js。以下是安裝Node.js的命令
$ cd /tmp $ \curl -sSL https://deb.nodesource.com/setup_6.x -o nodejs.sh $ less nodejs.sh $ cat /tmp/nodejs.sh | sudo -E bash – cat /tmp/nodejs.sh | sudo -E bash - ## Installing the NodeSource Node.js v6.x repo... ## Populating apt-get cache... + apt-get update Hit:1 http://in.archive.ubuntu.com/ubuntu xenial InRelease Hit:2 http://deb.kamailio.org/kamailio jessie InRelease Hit:3 http://security.ubuntu.com/ubuntu xenial-security InRelease Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease Hit:5 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease Reading package lists... Done W: http://deb.kamailio.org/kamailio/dists/jessie/InRelease: Signature by key E79ACECB87D8DCD23A20AD2FFB40D3E6508EA4C8 uses weak digest algorithm (SHA1) … .. Fetched 6,595 B in 9s (680 B/s) Reading package lists... Done W: http://deb.kamailio.org/kamailio/dists/jessie/InRelease: Signature by key E79ACECB87D8DCD23A20AD2FFB40D3E6508EA4C8 uses weak digest algorithm (SHA1) ## Run `apt-get install nodejs` (as root) to install Node.js v6.x and npm
現在我們可以使用apt-get命令獲取Node.js
$ sudo apt-get install nodejs Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: nodejs 0 upgraded, 1 newly installed, 0 to remove and 107 not upgraded. Need to get 10.1 MB of archives. After this operation, 50.7 MB of additional disk space will be used. Get:1 https://deb.nodesource.com/node_6.x xenial/main amd64 nodejs amd64 6.8.1-1nodesource1~xenial1 [10.1 MB] Fetched 10.1 MB in 37s (272 kB/s) Selecting previously unselected package nodejs. (Reading database ... 70921 files and directories currently installed.) Preparing to unpack .../nodejs_6.8.1-1nodesource1~xenial1_amd64.deb ... Unpacking nodejs (6.8.1-1nodesource1~xenial1) ... Processing triggers for man-db (2.7.5-1) ... Setting up nodejs (6.8.1-1nodesource1~xenial1) ...
在上面的文章中,我們學習了安裝‘rbenv’、Ruby on Rails的基礎知識,我們可以設定多個Ruby環境。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP