[技術博客] 利用Vagrant+virtualbox在windows下進行linux開發

做者:莊廓然linux

在windows下進行linux開發:利用Vagrant+virtualboxgit

基本教程在此github

加速box安裝的方法

網址中點擊打開要安裝的系統,在vagrant文件夾中執行shell

$ vagrant init ubuntu/xenial64

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

我安裝的是ubuntu/xenial64,接下來執行ubuntu

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'ubuntu/xenial64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'ubuntu/xenial64'
    default: URL: https://vagrantcloud.com/ubuntu/xenial64
==> default: Adding box 'ubuntu/xenial64' (v20190325.0.0) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20190325.0.0/providers/virtualbox.box
    default: Download redirected to host: cloud-images.ubuntu.com
    default: Progress: 0% (Rate: 61440/s, Estimated time remaining: 4:03:53)

Ctrl+C終止下載,獲得下載網址https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20190325.0.0/providers/virtualbox.box 而後複製到迅雷連接裏下載。vim

下載獲得文件virtualbox.box,複製文件夾下,執行指令windows

$ vagrant box add ubuntu/xenial64 virtualbox.box

查看是否安裝成功:瀏覽器

$ vagrant list
ubuntu/xenial64 (virtualbox, 0)

接下來ruby

$ vagrant up

用xshell登陸

在vagrant文件夾下輸入

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/vagrant/ubuntu/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

獲得private_key的地址

root帳戶登陸

http://www.javashuo.com/article/p-nzvgonqs-dv.html

$ sudo -i

換源教程

其餘源:http://www.javashuo.com/article/p-onguiwhv-dt.html

一、備份源文件:

$ cd /etc/apt/
$ sudo cp sources.list sources.list.bak 
$ sudo vim sources.list

二、刪除原來的內容,替換如下

deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse 
##測試版源 
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse 
# 源碼 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse 
##測試版源 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse 
# Canonical 合做夥伴和附加 
deb http://archive.canonical.com/ubuntu/ xenial partner 
deb http://extras.ubuntu.com/ubuntu/ xenial main

三、更新源和軟件

$ sudo apt-get update  #更新源
$ sudo apt-get upgrade #更新軟件
  • 一些命令
sudo apt-get update  更新源
sudo apt-get install package 安裝包
sudo apt-get remove package 刪除包
sudo apt-cache search package 搜索軟件包
sudo apt-cache show package  獲取包的相關信息,如說明、大小、版本等
sudo apt-get install package --reinstall  從新安裝包
sudo apt-get -f install  修復安裝
sudo apt-get remove package --purge 刪除包,包括配置文件等
sudo apt-get build-dep package 安裝相關的編譯環境
sudo apt-get upgrade 更新已安裝的包
sudo apt-get dist-upgrade 升級系統
sudo apt-cache depends package 瞭解使用該包依賴那些包
sudo apt-cache rdepends package 查看該包被哪些包依賴
sudo apt-get source package  下載該包的源代碼
sudo apt-get clean && sudo apt-get autoclean 清理無用的包
sudo apt-get check 檢查是否有損壞的依賴

安裝rvm

網絡不是很穩定。。要是嘗試不少次

$ sudo curl -L get.rvm.io | bash -s stable
  • 換掉源鏡像
$ echo "ruby_url=https://cache.ruby-china.com/pub/ruby" > ~/.rvm/user/db

訪問rails server

rails server默認只對本機有效,這個地方。。。坑了我過久了。。。

因爲我是用的虛擬機,因此實用rails server的時候不能用windows本地瀏覽器訪問

如要其餘機器訪問,請使用

$ rails server -b 0.0.0.0

而後經過虛擬機的ip地址進行訪問rails

RubyMine鏈接虛擬機上的解釋器

https://www.jetbrains.com/help/ruby/configuring-remote-interpreters-using-vagrant.html

一、在settings裏的Languages&Frameworks

Ctrl+Alt+S調出Ruby SDK and GemsAlt+Insert添加new remote

二、選擇Vagrant,找到vagrant虛擬機的路徑

三、而後指定rvm或者ruby在該虛擬機下的路徑 ,點擊ok便可

四、接下來就能夠在本地調試了

相關文章
相關標籤/搜索