A guide to setting up a Ruby on Rails development environment
This guide is a work in progress as the new Bash on Ubuntu on Windows Linux Subsystem is in beta. There is a lot of functionality not implemented yet so you'll find that, while the basics might work, some of the more advanced and complex things won't.
This will take about 45 minutes.
We will be setting up a Ruby on Rails development environment on Windows 10.
The reason we're going to be using Bash on Ubuntu on Windows because it allows you to run Linux on your Windows machine. Most Ruby on Rails tutorials and dependencies work best on Linux, so this allows you to get the best of both worlds. A Windows machine for your day to day work, and a Linux subsystem for Ruby on Rails development.
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 do the basics well enough.
Bash on Ubuntu on Windows is the name for the Linux Subsystem that's in beta right now for Windows. It allows you to run Linux on Windows without having to run a VM.
Right now, this update is only available to those on the "Fast" option in the Windows Insider program. You'll want to join the Windows Insider program and set your Insider level to "Fast".
Here's how you can join the Windows Insider program.
The first step is to enable "Developer mode" in Windows. You can do this by opening up Settings and navigating to to Update & Security, then "For Developers". Click the "Developer mode" option to enable it.
Now we need to enable to Windows Subsystem for Linux. You can do this by opening Control Panel, going to Programs, and then clicking "Turn Windows Features On or Off". Looking for the "Windows Subsystem for Linux" option and check the box next to it.
The earliest working version of this for me is Windows 10 release 14352. If Windows Subsystem for Linux doesn't show up in the list right away, you may need to make sure you're part of the Windows Insider program and set to the "Fast" level, update Windows, reboot, or wait a few days for the option to become enabled. I enabled Developer mode and had to wait two days or so before the option became available. I'm not sure how this feature propogates, but try a couple of these options and you should get it.
Once you've got this installed and after rebooting, we can start setting up Bash on Ubuntu on Windows.
Start by searching for "bash" in the start menu.
Next we'll follow the installation steps to get Bash on Ubuntu on Windows installed.
Once that's complete, you can open up the Start menu again and search for "Bash". This time it will have the Ubuntu logo.
Now you've got a functional Ubuntu Linux installation running on your Windows machine!
From here we can follow the normal Ubuntu setup steps to install Ruby, Rails, and our database.
Choose the version of Ruby you want to install:
The first step is to install some dependencies for Ruby.
sudo apt-get update sudo 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-dev
Next we're going to be installing Ruby using one of three methods. Each have their own benefits, most people prefer using rbenv these days, but if you're familiar with rvm you can follow those steps as well. I've included instructions for installing from source as well, but in general, you'll want to choose either rbenv or rvm.
Choose one method. Some of these conflict with each other, so choose the one that sounds the most interesting to you, or go with my suggestion, rbenv.
rbenv is a simple two step process. First you install
rbenv, and then
cd git clone https://github.com/rbenv/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc exec $SHELL git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc exec $SHELL rbenv install 2.7.0 rbenv global 2.7.0 ruby -v
The installation for
rvm is pretty simple:
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB curl -sSL https://get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm rvm install 2.7.0 rvm use 2.7.0 --default ruby -v
Arguably the least useful Ruby setup for development is installing from source, but I thought I'd give you the steps anyways:
cd wget http://ftp.ruby-lang.org/pub/ruby/2.7/ruby-2.7.0.tar.gz tar -xzvf ruby-2.7.0.tar.gz cd ruby-2.7.0/ ./configure make sudo make install ruby -v
The last step is to install Bundler
gem install bundler
rbenv users need to run
rbenv rehash after installing bundler.
We'll be using Git for our version control system so we're going to set it up to match our Github account. If you don't already have a Github account, make sure to register. It will come in handy for the future.
Replace my name and email address in the following steps with the ones you used for your Github account.
git config --global color.ui true git config --global user.name "YOUR NAME" git config --global user.email "[email protected]" ssh-keygen -t rsa -b 4096 -C "[email protected]"
The next step is to take the newly generated SSH key and add it to your Github account. You want to copy and paste the output of the following command and paste it here.
Once you've done this, you can check and see if it worked:
ssh -T [email protected]
You should get a message like this:
Hi excid3! You've successfully authenticated, but GitHub does not provide shell access.
Choose the version of Rails you want to install:
To install NodeJS, we're going to add it using the official repository:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - sudo apt-get install -y nodejs
And now, without further adieu:
gem install rails -v 220.127.116.11
If you're using rbenv, you'll need to run the following command to make the rails executable available:
Now that you've installed Rails, you can run the
rails -v command to make sure you have everything installed correctly:
rails -v # Rails 18.104.22.168
If you get a different result for some reason, it means your environment may not be setup properly.
Rails ships with sqlite3 as the default database. Chances are you won't want to use it because it's stored as a simple file on disk. You'll probably want something more robust like MySQL or PostgreSQL.
There is a lot of documentation on both, so you can just pick one that seems like you'll be more comfortable with. If you're coming from PHP, you may already be familiar with MySQL. If you're new to databases, I'd suggest skipping to setting up PostgreSQL.
You can install MySQL server and client from the packages in the Ubuntu repository. As part of the installation process, you'll set the password for the root user. This information will go into your Rails app's
database.yml file in the future.
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
libmysqlclient-dev gives you the necessary files to compile the
mysql2 gem which is what Rails will use to connect to MySQL when you setup your Rails app.
When you're finished, you can skip to the Final Steps.
The easiest way to setup PostgreSQL is to install it on Windows using one of the Windows installers. You'll be able to run it in Windows and connect to it through Linux.
Download PostgreSQL for Windows and install it.
Pay attention to the username and password you setup during installation of Postgres as you will use this to configure your Rails applications later to login to Postgres when your Rails app runs.
The best place to develop Rails apps on the Windows Subsystem for Linux is to navigate to `/mnt/c`. This is actually the C: drive on Windows and it lets you use Sublime, Atom, VS Code, etc on Windows to edit your Rails application.
When you create a new Rails app, you might run into the following error: parent directory is world writable but not sticky.
If you run into this issue, you can run
chmod +t -R ~/.bundle and that should fix the permissions errors and let you finish the bundle install for your Rails app.
And now for the moment of truth. Let's create your first Rails application:
# Navigate to the C: drive 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 to live (You only need to do this once) mkdir -p code #### If you want to use Postgres # Note that this will expect a postgres user with the same username # as your app, you may need to edit config/database.yml to match the # user you created earlier rails new myapp -d postgresql #### or if you want to use SQLite (not recommended) # rails new myapp #### Or if you want to use MySQL # rails new myapp -d mysql # Then, move into the application directory cd myapp # If you setup MySQL or Postgres with a username/password, modify the # config/database.yml file to contain the username/password that you specified # Create the database rake db:create rails server
You can now visit http://localhost:3000 to view your new website!
Now that you've got your machine setup, it's time to 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 update your config/database.yml file to 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 to your folders in
C:\code and you'll be able to safely edit your Rails apps in Windows and run them in Linux!
That's it! Let us know in the comments below if you run into any issues or have any other protips to share!.
Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.