How to install Ruby

Share:
# Synoposis - Install ruby from source code - Install ruby using rbenv - Install ruby in docker - Check ruby version # Intruduction ROS, Tmux and other progrms depending on ruby. Therefore, you should config ruby before installing ROS and tmux. By defalut you cannot install the latest version via: ```sh sudo apt install ruby-full ``` - Requirements ```sh sudo apt instll git build-essential libssl-dev libreadline-dev zlib1g-dev ``` # Install ruby from Source Code - Get source code ```sh # 2.6 https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.6.tar.gz # 2.7 https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.1.tar.gz ``` - Build from source ```sh $ ./configure $ make $ sudo make install ``` By default, this will install Ruby into `/usr/local`. To change, pass the `--prefix=DIR` option to the `./configure` script. - Check ruby version ```sh root@f21a4be0aba9:~# ruby -v ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux] ``` # Install using rbenv 1. First, update the packages index and install the packages required for the ruby-build tool to build Ruby from source: ```sh sudo apt-get remove ruby sudo apt update sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev ``` 2. Next, run the following curl command to install both rbenv and ruby-build: https://github.com/rbenv/rbenv-installer: ```sh # with curl curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash # alternatively, with wget wget -q https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer -O- | bash ``` 3. Add $HOME/.rbenv/bin to the system PATH config to .bashrc or .zshrc ```sh echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc echo 'eval "$(rbenv init -)"' >> ~/.zshrc source ~/.zshrc ``` 4. Install the latest stable version of Ruby and set it as a default version with: ```sh rbenv install 2.6.0 rbenv global 2.6.0 ``` 5. Verify that Ruby was properly installed by printing out the version number: ```sh ruby -v ``` **Install rubygems:** ```sh sudo apt-get install rubygems ``` Not using root: ```sh chown -R rubyusername:rubyusername /var/lib/gems ``` # Docker version ```dockerfile FROM base MAINTAINER tcnksm "https://github.com/tcnksm" # Install packages for building ruby RUN apt-get update RUN apt-get install -y --force-yes build-essential curl git RUN apt-get install -y --force-yes zlib1g-dev libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt-dev RUN apt-get clean # Install rbenv and ruby-build RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build RUN /root/.rbenv/plugins/ruby-build/install.sh ENV PATH /root/.rbenv/bin:$PATH RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh # or /etc/profile RUN echo 'eval "$(rbenv init -)"' >> .bashrc # Install multiple versions of ruby ENV CONFIGURE_OPTS --disable-install-doc ADD ./versions.txt /root/versions.txt RUN xargs -L 1 rbenv install < /root/versions.txt # Install Bundler for each version of ruby RUN echo 'gem: --no-rdoc --no-ri' >> /.gemrc RUN bash -l -c 'for v in $(cat /root/versions.txt); do rbenv global $v; gem install bundler; done' ``` - https://github.com/tcnksm/dockerfile-rbenv/blob/master/Dockerfile versions.txt: ```sh 1.8.7-p371 1.9.3-p392 2.0.0-p353 ``` Another version: https://qiita.com/takat0-h0rikosh1/items/72fb3382fea2760826a7 : ```dockerfile FROM ubuntu:xenial # Require to install ruby RUN apt-get update && \ apt-get install -y \ git \ build-essential \ libssl-dev \ libreadline-dev \ zlib1g-dev # Install rbenv RUN git clone https://github.com/rbenv/rbenv.git ~/.rbenv && \ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc && \ echo 'eval "$(rbenv init -)"' >> ~/.bashrc ENV PATH /root/.rbenv/shims:/root/.rbenv/bin:$PATH # Install ruby-build & ruby RUN git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build && \ ~/.rbenv/bin/rbenv install 2.5.1 && \ ~/.rbenv/bin/rbenv global 2.5.1 # Initiarize ruby encording ENV RUBYOPT -EUTF-8 # Install bundler RUN ~/.rbenv/bin/rbenv exec gem install bundler -v 1.16.3 ``` # Install using snap This method is not working for me, ignore it. ```sh $ sudo apt install snapd $ which snap /usr/bin/snap $ snap --version ``` Reinstall snap ```sh $ sudo apt autoremove --purge snapd $ sudo apt install snapd ``` # References - https://www.ruby-lang.org/en/downloads/ - https://stackify.com/install-ruby-on-ubuntu-everything-you-need-to-get-going/

No comments