在本文中,我们将学习如何设置和配置Ruby on Rails,它是开发人员用来创建站点和Web应用程序的最流行的堆栈应用程序。Ruby是一种编程语言,程序员可以将它与Rails开发框架结合使用,以使应用程序开发变得快速而简单。
“ rbenv”是用于安装Ruby和Rails的命令行工具,它也为使用Ruby on Rails进行应用程序开发提供了良好的环境。“ rbenv”提供特定于应用程序的Ruby版本。
我们需要具有非root用户的Ubuntu 16.04安装机器。
在开始安装之前,我们需要更新和安装所有相关的依赖项。
首先,我们将更新Ubuntu,要更新Ubuntu,我们需要运行以下命令。
$ sudo apt-get update
然后,我们将使用apt-get安装rbenv和Ruby所需的所有依赖项,下面是安装依赖项的命令。
$ 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) ...
我们将使用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”,我们需要添加以下几行,还添加源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 }
要使用rebenv,请安装命令,该命令可使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.
我们将使用rbenv插件ruby-build来安装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]
Gem是将被安装以扩展Ruby功能的软件包。我们必须使用gem命令安装软件包。我们将使用带有gem的install命令来安装依赖项。
$ 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
现在,我们将使用gem install命令安装最新版本的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
由于某些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环境。