Cài đặt ruby on rails trên windows

The reason we"re going khổng lồ be using Bash on Ubuntu on Windows because it allows you lớn run Linux on your Windows machine. Most Ruby on Rails tutorials and dependencies work best on Linux, so this allows you to lớn get the best of both worlds. A Windows machine for your day khổng lồ day work, và a Linux subsystem for Ruby on Rails development.

Bạn đang xem: Cài đặt ruby on rails trên windows

This only works on 64-bit installations of Windows. This is also in beta, so this won"t be nearly as robust as running a Linux virtual machine, but it can definitely vì the basics well enough.

Installing the Windows Subsystem for Linux

Windows 10 allows you lớn run various Linux operating systems inside of Windows similar to lớn a virtual machine, but natively implemented. We"ll use this to install Ruby and run our Rails apps.

mở cửa Powershell as Administrator & run:

dism.exe cộ /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestartdism.exe /online /enable-feature /featurename:VirtualMachinePlatkhung /all /norestartNext install Ubuntu from the Microsoft Store.


Now open Ubuntu in the Start thực đơn or by running wsl in PowerShell or the comm& prompt. You"ll be asked to cài đặt a new user for Ubuntu. Rethành viên this password as it"s what you"ll use later on when installing packages with suvày.


Congrats! You"ve now got a Ubuntu terminal on Windows. You"ll use this to lớn run your Rails server & other processes for development.

The first step is lớn install some dependencies for Ruby.

suvị apt-get updatesubởi apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-devNext we"re going khổng lồ be installing Ruby with a version manager called Rbenv.

Installing with rbenv is a simple two step process. First you install rbenv, và then ruby-build:cdgit clone https://github.com/rbenv/rbenv.git ~/.rbenvemang đến "export PATH="$HOME/.rbenv/bin:$PATH"" >> ~/.bashrceđến "eval "$(rbenv init -)"" >> ~/.bashrcexec $SHELLgit clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-buildemang lại "export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"" >> ~/.bashrcexec $SHELLTo install Ruby and mix the mặc định version, we"ll run the following commands:rbenv install 3.0.3rbenv global 3.0.3Confirm the default Ruby version matches the version you just installed.ruby -vThe last step is to install Bundler

gem install bundlerrbenv users need to run rbenv rehash after installing bundler.

Configuring Git

We"ll be using Git for our version control system so we"re going lớn mix it up to match our Github trương mục. If you don"t already have sầu a Github account, make sure to register. It will come in handy for the future.

Replace my name & gmail address in the following steps with the ones you used for your Github tài khoản.

git config --global color.ui truegit config --global user.name "YOUR NAME"git config --global user.email "YOUR
EMAIL.com"The next step is khổng lồ take the newly generated SSH key and add it khổng lồ your Github trương mục. You want to lớn copy và paste the output of the following comm& and paste it here.cát ~/.ssh/id_rsa.pubOnce you"ve sầu done this, you can check & see if it worked:

ssh -T git
github.comYou should get a message like this:

Hi excid3! You"ve successfully authenticated, but GitHub does not provide shell access.

Since Rails ships with so many dependencies these days, we"re going to lớn need to install a Javascript runtime lượt thích NodeJS và a package manager called Yarn.

To install NodeJS & Yarn, we"re going to lớn add it using the official repository:

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | suvì apt-key add -eđến "deb https://dl.yarnpkg.com/debian/ stable main" | subởi vì tee /etc/apt/sources.list.d/yarn.listsudo apt updatesubởi vì apt-get install -y nodejs yarnAnd now, without further adieu:

gem install rails -v 7.0.0If you"re using rbenv, you"ll need lớn run the following command to lớn make the rails executable available:

rbenv rehashNow that you"ve installed Rails, you can run the rails -v commvà to lớn make sure you have sầu everything installed correctly:

rails -v# Rails 7.0.0If you get a different result for some reason, it means your environment may not be thiết lập properly.

Setting Up MySQL

Rails ships with sqlite3 as the default database. Chances are you won"t want to use it because it"s stored as a simple tệp tin on disk. You"ll probably want something more robust lượt thích MySquốc lộ or PostgreSquốc lộ.

There is a lot of documentation on both, so you can just pick one that seems like you"ll be more comfortable with.

Xem thêm: Cách Xỏ Chỉ Máy Vắt Sổ Siruba 3 Chỉ, Hướng Dẫn Sử Dụng Máy Vắt Sổ Siruba 747

If you"re new to lớn Ruby on Rails or databases in general, I strongly recommend setting up PostgreSquốc lộ.

If you"re coming from PHP, you may already be familiar with MySQL.

You can install MySquốc lộ server and client from the packages in the Ubuntu repository. As part of the installation process, you"ll mix the password for the root user. This information will go into lớn your Rails app"s database.yml tệp tin in the future.

suvì apt-get install mysql-server mysql-client libmysqlclient-devInstalling the libmysqlclient-dev gives you the necessary files lớn compile the mysql2 gem which is what Rails will use to lớn connect to lớn MySQL when you thiết đặt your Rails ứng dụng.

When you"re finished, you can skip khổng lồ the Final Steps.

Setting Up PostgreSQL

The easiest way to lớn thiết đặt PostgreSQL is khổng lồ install it on Windows using one of the Windows installers. You"ll be able khổng lồ run it in Windows & connect khổng lồ it through Linux.

Download PostgreSQL for Windows and install it.

Pay attention to lớn the username và password you setup during installation of Postgres as you will use this to configure your Rails applications later lớn login to Postgres when your Rails phầm mềm runs.

You"ll also need to lớn install the development headers on Ubuntu so you can install the pg gem.

suvì apt install libpq-dev

Final Steps

The best place to lớn develop Rails apps on the Windows Subsystem for Linux is khổng lồ navigate lớn `/mnt/c`. This is actually the C: drive sầu on Windows and it lets you use Sublime, Atom, VS Code, etc on Windows khổng lồ edit your Rails application.

And now for the moment of truth. Let"s create your first Rails application:

# Navigate to lớn the C: drive sầu on Windows. Do this every time you open the Linux console.cd /mnt/c# Create a code directory at C:code for your Rails apps lớn live (You only need to lớn do this once)mkdir -p code#### If you want to use Postgres# lưu ý that this will expect a postgres user with the same username# as your ứng dụng, you may need to lớn edit config/database.yml lớn match the# user you created earlierrails new mytiện ích -d postgresql#### or if you want to use SQLite (not recommended)# rails new myapp#### Or if you want to lớn use MySQL# rails new mytiện ích -d mysql# Then, move into the application directorycd myapp# If you cài đặt MySquốc lộ or Postgres with a username/password, modify the# config/database.yml file lớn contain the username/password that you specified# Create the databaserake db:createrails serverYou can now visit http://localhost:3000 khổng lồ view your new website!

Now that you"ve got your machine setup, it"s time khổng lồ start building some Rails applications!

If you received an error that said Access denied for user "root"
"localhost" (using password: NO) then you need to lớn update your config/database.yml file khổng lồ match the database username and password.

To edit your code in Windows: open up Sublime, Atom, VS Code, or whatever editor your prefer and point them khổng lồ your folders in C:code and you"ll be able to lớn safely edit your Rails apps in Windows & run them in Linux!

When you create a new Rails ứng dụng, you might run into lớn the following error: parent directory is world writable but not sticky.

If you run inlớn this issue, you can run chthủ thuật +t -R ~/.bundle & that should fix the permissions errors & let you finish the bundle install for your Rails tiện ích.

That"s it! Let us know in the comments below if you run inlớn any issues or have sầu any other protips khổng lồ share!.

Want lớn stay up-to-date with Ruby on Rails?

Join 56,919+ developers who get early access to lớn new tutorials, screencasts, articles, và more.

logoCreated with Sketch.

Ruby on Rails tutorials, guides, và screencasts for website developers learning Ruby, Rails, Javascript, Turbolink, Stimulus.js, Vue.js, và more.Icons by Icons8