在 Ubuntu 18.04 下搭建 ruby on rails 环境
系统环境
系统环境:Ubuntu 18.04
安装前准备
sudo apt update
sudo apt upgrade
sudo apt install sqlite3 gnupg curl git libpq-dev
安装RVM ruby版本管理器
# 切换管理员模式
sudo su
安装软件签名公钥
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
# gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
# gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
安装rvm
curl -sSL https://get.rvm.io | bash -s stable --ruby
source /usr/local/rvm/scripts/rvm
# source ~/.rvm/scripts/rvm
验证安装
rvm version
安装 Ruby
# 在第一步中,ruby的最新版本已经被自动安装上; 将rvm更新至最新最稳定的状态
rvm get stable --autolibs=enable
# usermod -a -G rvm root
# 查看所有ruby版本
rvm list known
# 安装ruby
# rvm install ruby-2.5.1
# 设置ruby的默认版本
# rvm --default use ruby-2.5.1
# 查看ruby版本
ruby -v
安装Nodejs
# 我们需要安装nodejs作为ruby的js运行时环境;添加nodejs仓库
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
# 安装nodejs
sudo apt install -y nodejs
# sudo apt install gcc g++ make
配置 ruby gem
# gem是ruby的包管理工具,在我们安装ruby的时候,他已经被自动安装上了
# 更新gem
gem update --system
gem -v
安装 ruby on rails
# 使用gem进行安装
gem install rails -v 5.2.0
### gem 更换源,如果当前源不可用, 可更换源
## 移除现有源
# gem sources -r https://rubygems.org
## 添加 http 源,如果不好用也可换为下面的淘宝源
# gem sources -a http://rubygems.org
## 添加淘宝源
# gem sources -a http://ruby.taobao.org/
# 验证安装
rails -v
测试
# 创建 Blog 应用
rails new blog
# 创建 blog 应用后,进入该文件夹
cd blog
# 启动 Web 服务器
bin/rails server -b 0.0.0.0 -p 80
- -b:允许访问的IP地址,0.0.0.0 表示允许任何 IP 地址访问
- -p:web应用端口号
上述命令会启动 Puma,这是 Rails 默认使用的 Web 服务器。要查看运行中的应用,请打开浏览器窗口,访问 http://服务器IP。这时应该看到默认的 Rails 欢迎页面:
报错类型及解决方案
Could not find pg-0.21.0 in any of the sources
Could not find pg-0.21.0 in any of the sources
Run `bundle install` to install missing gems.
按照提示操作
bundle install
又报如下错误
Installing pg 0.21.0 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
An error occurred while installing pg (0.21.0), and Bundler cannot continue.
Make sure that `gem install pg -v '0.21.0'` succeeds before bundling.
在执行 bundle install
时报错,提示安装 gp,按提示操作
gem install pg -v '0.21.0'
继续报错
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
current directory: /usr/local/rvm/gems/ruby-2.7.0/gems/pg-0.21.0/ext
/usr/local/rvm/rubies/ruby-2.7.0/bin/ruby -I /usr/local/rvm/rubies/ruby-2.7.0/lib/ruby/site_ruby/2.7.0 -r ./siteconf20200811-33351-1v35h8a.rb extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/local/rvm/rubies/ruby-2.7.0/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-windows-cross
--disable-windows-cross
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/usr/local/rvm/gems/ruby-2.7.0/extensions/x86_64-linux/2.7.0/pg-0.21.0/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /usr/local/rvm/gems/ruby-2.7.0/gems/pg-0.21.0 for inspection.
Results logged to /usr/local/rvm/gems/ruby-2.7.0/extensions/x86_64-linux/2.7.0/pg-0.21.0/gem_make.out
此时需要安装缺失的依赖包,再重新安装 pg
apt install libpq-dev
gem install pg -v '0.21.0'
安装成功
Could not find proper version of railties (5.1.6) in any of the sources
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Could not find proper version of railties (5.1.6) in any of the sources
Run `bundle install` to install missing gems.
按照提示操作
bundle install
又报如下错误
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Traceback (most recent call last):
4: from /usr/local/rvm/gems/ruby-2.7.0/bin/ruby_executable_hooks:24:in `<main>'
3: from /usr/local/rvm/gems/ruby-2.7.0/bin/ruby_executable_hooks:24:in `eval'
2: from /usr/local/rvm/rubies/ruby-2.7.0/bin/bundler:23:in `<main>'
1: from /usr/local/rvm/rubies/ruby-2.7.0/lib/ruby/site_ruby/2.7.0/rubygems.rb:296:in `activate_bin_path'
/usr/local/rvm/rubies/ruby-2.7.0/lib/ruby/site_ruby/2.7.0/rubygems.rb:277:in `find_spec_for_exe': Could not find 'bundler' (1.12.1) required by your /data5/xieqk/web/ruby_on_rails/ICME/Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:1.12.1`
继续按照提示操作
gem install bundler:1.12.1
再重新输入
bundle install
安装成功