Installing Ruby on Ubuntu Linux is an essential step for developers looking to build, run, and manage Ruby-based applications efficiently. Whether you’re a beginner or experienced programmer, setting up Ruby correctly ensures smooth development workflows, access to the latest features, and compatibility with popular Ruby gems and frameworks like Rails. Before starting, ensure your Ubuntu system is up to date and has internet access, as you’ll need to download packages and dependencies during installation.
What You’ll Need
- An Ubuntu Linux system (Ubuntu 20.04 LTS, 22.04 LTS, or later recommended)
- Administrative privileges (sudo access)
- Internet connection for downloading packages
Step-by-Step
- Update Your System Packages
Open a terminal and run:sudo apt update && sudo apt upgrade -yThis ensures all existing packages are current, reducing conflicts during Ruby installation.
- Install Required Dependencies
Install essential build tools and libraries:sudo apt install -y build-essential libssl-dev libreadline-dev zlib1g-devThese are necessary for compiling Ruby from source or installing via version managers.
- Choose a Ruby Installation Method
There are two main ways: using the system’s package manager or a version manager like RVM or rbenv. For flexibility, we’ll cover rbenv, which allows managing multiple Ruby versions. - Install rbenv and ruby-build
Clone the rbenv repository:git clone https://github.com/rbenv/rbenv.git ~/.rbenvAdd rbenv to your shell environment:
echo 'export PATH="$HOME/.rbenv/bin"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc source ~/.bashrcThen, install ruby-build as a plugin:
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-buildReload your shell or run
source ~/.bashrcto apply changes. - Install the Desired Ruby Version
List available Ruby versions:rbenv install -lChoose a version (e.g., 3.2.2) and install:
rbenv install 3.2.2Set it as the global default:
rbenv global 3.2.2Verify installation:
ruby -v - Install Bundler and Test Ruby
Install Bundler, a dependency manager:gem install bundlerConfirm Ruby is working:
ruby -vand check Bundler:
bundler -v
Common Mistakes
- Using the system’s default Ruby package instead of a version manager, which may be outdated or incompatible.
- Not updating your package list before installing dependencies, leading to missing packages.
- Failing to add rbenv to your shell configuration, resulting in command not found errors.
- Installing Ruby without specifying the correct version, which can cause version conflicts.
- Skipping the restart or source of shell configuration files after editing .bashrc or .zshrc.
Tips
- Always verify the installed Ruby version with
ruby -v. - Use rbenv or RVM to manage multiple Ruby versions for different projects.
- Regularly update rbenv and ruby-build to access the latest Ruby releases.
- Install essential gems like Bundler immediately after Ruby installation for easier dependency management.
- Consider using a version control system like Git for managing your Ruby projects.
Troubleshooting
- Ruby Command Not Found: Ensure rbenv is properly initialized by checking your shell configuration. Run
which rubyto verify the path. - Installation Fails or Ruby Version Not Recognized: Make sure dependencies are installed and that you used
rbenv installwith the correct version number. Reinstall if necessary. - Environment Variables Not Set: Reload your shell or run
source ~/.bashrc(or your shell config file) to refresh environment variables. - Permission Errors During Gem Installation: Avoid running gem commands with sudo, as it can cause permission issues. Use rbenv’s managed Ruby environment instead.
- Slow Compilation or Build Failures: Check available disk space and dependencies. Consider updating your system or using precompiled binaries if available.
Frequently Asked Questions
- Can I install Ruby without using a version manager?
- Yes, but it’s not recommended because it can overwrite system Ruby and cause conflicts. Using rbenv or RVM provides safer, flexible management.
- Which Ruby version should I install?
- Choose the latest stable release for new projects, or a specific version if required by your application’s dependencies.
- How do I switch between multiple Ruby versions?
- Use rbenv commands like
rbenv globalorrbenv localto set default or project-specific Ruby versions. - Is it necessary to restart my computer after installation?
- No, just reload your shell or run
source ~/.bashrcto apply environment changes. - How do I uninstall a specific Ruby version?
- Use
rbenv uninstall <version>to remove a Ruby version managed by rbenv. - Can I install Ruby using apt-get instead?
- Yes, but the versions in Ubuntu repositories are often outdated. For the latest features, using rbenv or RVM is preferable.
Following this guide, you’ll have a fully functional Ruby environment on your Ubuntu Linux system, ready for development and deployment. Managing Ruby versions effectively ensures compatibility and flexibility for your projects, making your development workflow smoother and more efficient.
Related
- How to Install OpenLiteSpeed on Ubuntu Linux Easily
- How to Install NumPy on Ubuntu 24.04 Easily
- How to Install npm on Windows 11 Easily and Quickly