How to install the latest version of Ruby and Ruby on Rails in Ubuntu?
How to get a current Ruby version without messing up your system
Do not mess with your system Ruby, but instead install a current version with either rbenv
or RVM. I prefer the first, but both work fine. Note that you can only install one of those at a time.
With such a Ruby version manager, you'll never have to type sudo
again to install (or uninstall) a Gem, and you can keep different versions for different projects. You can safely remove these versions again.
Please make sure to read the README
s of those tools, at least once.
Method 1 – rbenv
rbenv is a version manager for Ruby. It allows you to install a Ruby version alongside your original system Ruby, which means you cannot mess up that one, and you can easily upgrade versions.
To install it, use the rbenv-installer
. Make sure to restart your shell once it's installed, and that the rbenv
function works.
Then, once rbenv is installed, run rbenv install -l
. This gives you a list of available Rubies. Install your chosen one with:
rbenv install 2.5.1
Now choose this one as your default:
rbenv global 2.5.1
As soon as this is done, gem
can be used to run:
gem install rails
If the above does not work, you might be missing required packages for building from source. See here for a list of packages that you might want to install. On Ubuntu, these include:
sudo apt install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev
Method 2 – RVM
You can also install Ruby over RVM. Here as well, you don't need to sudo
anything, and you'll be able to get more recent versions of Ruby alongside the system one.
Read the installation instructions for your system.
After installation, you can install Rubies with a simple command. First, check rvm list known
to get the list of installable versions. Now install your choice:
rvm install 2.5.1
Then, set it as the default Ruby version for your user:
rvm use 2.5.1 --default
Now you can install Rails over gem
:
gem install rails