国科大ruby选课原型系统的环境搭建(Ubuntu 16.04)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/diyinqian/article/details/84314100

步骤:

1.查看系统版本

wsy@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.3 LTS
Release:	16.04
Codename:	xenial

2.更新软件(可以避免以后遇到的一些问题)

wsy@ubuntu:~$ sudo apt-get update

3.安装curl(如果没安装curl的话才安装)

wsy@ubuntu:~$ sudo apt-get install curl
	Reading package lists... Done
	Building dependency tree       
	Reading state information... Done

4.安装rvm(curl是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在"标准输出"(stdout)上面。使用-L参数,curl就会跳转到新的网址。)

wsy@ubuntu:~$ curl -L get.rvm.io | bash -s stable
	  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
									 Dload  Upload   Total   Spent    Left  Speed
	100   194  100   194    0     0     77      0  0:00:02  0:00:02 --:--:--    77
	100 24361  100 24361    0     0   5050      0  0:00:04  0:00:04 --:--:-- 15321
	Downloading https://github.com/rvm/rvm/archive/1.29.4.tar.gz

4.1如果公钥报错的话执行以下操作

wsy@ubuntu:~$ gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
wsy@ubuntu:~$ 
wsy@ubuntu:~$ curl -sSL https://get.rvm.io | bash -s stable
wsy@ubuntu:~$ 
wsy@ubuntu:~$ source ~/.bashrc  #这两个加入之后就能检测到ruby和rails
wsy@ubuntu:~$ source ~/.bash_profile #这两个加入之后就能检测到ruby和rails

5.使用RVM安装RUBY

wsy@ubuntu:~$ echo "ruby_url=https://cache.ruby-china.org/pub/ruby" > ~/.rvm/user/db #输出重定向到文件,这个语句似乎并没有用
wsy@ubuntu:~$ rvm list known
wsy@ubuntu:~$ rvm install "ruby-2.3.0"

6.安装rails

wsy@ubuntu:~$ gem install rails -v 4.2.6
wsy@ubuntu:~$ rails -v

7.安装git并下载原型系统

wsy@ubuntu:~/Desktop$ sudo apt install git
wsy@ubuntu:~/Desktop$ git clone https://github.com/PENGZhaoqing/CourseSelect
wsy@ubuntu:~/Desktop$ cd CourseSelect/

8.执行bundle install

wsy@ubuntu:~/Desktop/CourseSelect$ bundle install#可能的报错信息参考本文件上面部分

9.安装postgresql和pgadmin3,并创建新用户dbuser

wsy@ubuntu:~/Desktop/CourseSelect$ sudo apt-get install postgresql
wsy@ubuntu:~/Desktop/CourseSelect$ sudo adduser dbuser #新建一个Linux新用户,可以取你想要的名字,这里为dbuser。
wsy@ubuntu:~/Desktop/CourseSelect$ sudo apt-get install pgadmin3

10.切换用户并启动postgres

wsy@ubuntu:~$ sudo su - postgres
postgres@ubuntu:~$ psql

11.创建用户并设置密码

postgres=# \password postgres
postgres=# CREATE USER dbuser WITH PASSWORD 'dbuser';
CREATE ROLE

12.新建数据库并授权

postgres=# CREATE DATABASE dbuser OWNER dbuser;
CREATE DATABASE
postgres=# CREATE DATABASE courseselect_development OWNER dbuser;
CREATE DATABASE

13.给数据库授权(貌似没用)

postgres=# GRANT ALL PRIVILEGES ON DATABASE dbuser to dbuser;
GRANT
postgres=# \q

14.切换用户,并解决ruby -v失效问题

postgres@ubuntu:~$ su wsy
Password: 
wsy@ubuntu:~$ cd ~/Desktop/CourseSelect/
wsy@ubuntu:~/Desktop$ source ~/.bashrc & source ~/.bash_profile

15.执行rake db:migrate和rake db:seed

wsy@ubuntu:/var/lib/postgresql$ cd ~/Desktop/CourseSelect/
wsy@ubuntu:~/Desktop/CourseSelect$ rake db:migrate
wsy@ubuntu:~/Desktop/CourseSelect$ rake db:seed

16.启动rails server

wsy@ubuntu:~/Desktop/CourseSelect$ rails s 

问题汇总

  1. su: Authentication failure 解决:在ubuntu中,root账户默认不可用。使用这个获得root:sudo -i
  2. rake db:migrate报错:Ruby on Rails , No Rakefile found
    解决: https://stackoverflow.com/questions/18452701/ruby-on-rails-no-rakefile-found-error
  3. postgresql相关知识:
  4. 切换用户并启动postgres(3种方法)

法1:

wsy@ubuntu:~/Desktop/CourseSelect$ sudo su - postgres #然后,切换到postgres用户。
postgres@ubuntu:/home/wsy/Desktop/CourseSelect$ psql #下一步,使用psql命令登录PostgreSQL控制台。

法2:

wsy@ubuntu:~/Desktop/CourseSelect$ sudo -u postgres psql
psql (9.5.14)
Type "help" for help.

法3:

wsy@ubuntu:/etc/postgresql/9.5/main$ psql -h 127.0.0.1 -U postgres
  1. 修改PostgreSQL数据库默认用户postgres的密码(两种方法)

法1:

postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; #第一件事是使用\password命令,为postgres用户设置一个密码。(不要落下分号)

法2:

postgres=# \password postgres
Enter new password: 
Enter it again: 
ALTER ROLE
  1. 执行bundle install报错:

报错1: Could not fetch specs from https://gems.ruby-china.org/
详细信息:

wsy@ubuntu:~/Desktop/CourseSelect$ bundle install
Fetching source index from https://gems.ruby-china.org/
Retrying fetcher due to error (2/4): Bundler::HTTPError Could not fetch specs from https://gems.ruby-china.org/
Retrying fetcher due to error (3/4): Bundler::HTTPError Could not fetch specs from https://gems.ruby-china.org/
Retrying fetcher due to error (4/4): Bundler::HTTPError Could not fetch specs from https://gems.ruby-china.org/
Could not fetch specs from https://gems.ruby-china.org/

解决思路: 将原来的https://gems.ruby-china.org/修改为https://gems.ruby-china.com/

wsy@ubuntu:~/Desktop/CourseSelect$ gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
wsy@ubuntu:~/Desktop/CourseSelect$ gem sources -l #查看修改后的配置
wsy@ubuntu:~/Desktop/CourseSelect$ cd CourseSelect
wsy@ubuntu:~/Desktop/CourseSelect$ vi Gemfile 然后将第一行改为:source 'https://gems.ruby-china.com'	

报错2: An error occurred while installing pg (0.18.4)
详细信息:

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /home/wsy/.rvm/gems/ruby-2.3.0/gems/pg-0.18.4/ext
/home/wsy/.rvm/rubies/ruby-2.3.0/bin/ruby -r ./siteconf20181118-99965-1ksvqt7.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.
...
An error occurred while installing pg (0.18.4), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.4' --source 'https://gems.ruby-china.com/'` succeeds before bundling.

解决:

wsy@ubuntu:~/Desktop/CourseSelect$ sudo apt-get install libpq-dev
wsy@ubuntu:~/Desktop/CourseSelect$ gem install pg
wsy@ubuntu:~/Desktop/CourseSelect$ bundle install

猜你喜欢

转载自blog.csdn.net/diyinqian/article/details/84314100