How to install rails in Ubuntu 16 OS X? - ruby-on-rails

I am beginner of learning rails. I want to install rails5 in my Linux operating system.
How can I install rails in Ubuntu 16.04 LTS system ?

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 python-software-properties libffi-dev nodejs
Install rbenv and ruby:
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.4.0
rbenv global 2.4.0
ruby -v
The last step is to install Bundler
gem install bundler
Install nodejs:
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
Install rails now:
gem install rails -v 5.0.1
you'll need to run the following command to make the rails executable available:
rbenv rehash
Now that you've installed Rails, you can run the rails -v command to make sure you have everything installed correctly:
rails -v
# Rails 5.0.1

You should use RVM to install Rails. It's a virtualization system that will allow you to install multiple versions of Ruby, and maintain different sets of libraries (including Rails) for each project you work on. By default, it installs Rails and its libraries (gems) in your user directory. This is both safer (more secure) than system-wide installation, and much more flexible.
You could install Rails via Ubuntu's package manager (sudo apt-get install rails), and install its libraries via the system-wide RubyGems installer (gem), but that's not a best-practice. While Ruby and Rails don't change as often as they did a few years ago, the dev ecosystem still evolves faster than Linux distributions like Ubuntu can keep up with.
These days, everyone developing Rails apps uses either RVM or rbenv to manage user-space installations. There's no compelling reason to prefer RVM or rbenv as you start out, but RVM is more popular.

Related

How to install ruby 3.1.2 in an Ubuntu Docker container?

I tried:
if ! command -v ruby &> /dev/null
then
# - proverbot's version
# First, install Ruby, as that is for some reason required to build
# the "system" project
# git clone https://github.com/rbenv/ruby-build.git ~/ruby-build
# mkdir -p ~/.local
# PREFIX=~/.local ./ruby-build/install.sh
# ~/.local/ruby-build 3.1.2 ~/.local/
# - u-pycoq's version
# sudo apt-get install ruby-full
sudo apt-get install rbenv
rbenv init
eval "$(rbenv init - bash)"
echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc
sudo apt-get install ruby-build
# ruby-build 3.1.2
rbenv install 3.1.2
rbenv global 3.1.2
ruby -v
else
echo "Error: failed to install ruby"
fi
But I get my Ruby build is not up-to-date perhaps, but when I install it, it is up-to-date. Therefore, why is the proverbot's attempt 3.1.2 if it doesn't exist?
It was inspired from: https://superuser.com/questions/340490/how-to-install-and-use-different-versions-of-ruby
It seems it works with 2.7.1, but not with 3.1.2.
sudo apt-get install rbenv
rbenv init
eval "$(rbenv init - bash)"
echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc
sudo apt-get install ruby-build
# mkdir -p ~/.local
# PREFIX=~/.local
# sh ~/ruby-build/install.sh
mkdir -p ~/.local
ruby-build 2.7.1 ~/.local
rbenv global 2.7.1
# ruby-build 2.3.1p112 ~/.local/
# ruby-build 3.1.2 ~/.local/
# rbenv global 3.1.2
ruby -v
bot#513314bdda2a:~/ruby-build$ ruby -v
ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [aarch64-linux-gnu]
Main command that fails
I think this is the main command that fails after rbenv and ruby-build are installed:
rbenv install 3.1.2
with
ruby-build: definition not found: 3.1.2
See all available versions with `rbenv install --list.
If the version you need is missing, try upgrading ruby-build.
Might my version of Ubuntu be the issue?
I tried it in a different server outside my Docker container and Ruby works. In the Docker container it says it's:
bot#513314bdda2a:~/ruby-build$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 11 (bullseye)
Release: 11
Codename: bullseye
Is that an issue?
I tried updating ruby-build
I ran sudo apt-get update then sudo apt-get upgrade ruby-build then mkdir -p ~/.local then ruby-build 3.1.2 ~/.local/ and the error is ruby-build: definition not found: 3.1.2
I tried a bunch of Ubuntu's, but it failed
#!/usr/bin/env bash
# - Get the Ubuntu version for the Ubuntu image
#docker run -it --rm ubuntu:20.04 /bin/bash
docker run -it --rm ubuntu:18.04 /bin/bash
apt-get update && apt-get install -y lsb-release && apt-get clean all
lsb_release -a
#apt-get install git
apt-get update
apt-get upgrade ruby-build
apt-get install rbenv
apt-get install ruby-build
rbenv install 3.1.2
# - Get Ubuntu version for the 'miniconda3' Docker image
docker run -it --rm continuumio/miniconda3:latest /bin/bash
#sudo apt-get install lsb-release
#apt-get install lsb-release
apt-get update && apt-get install -y lsb-release && apt-get clean all
lsb_release -a
Output of ruby-build https://gist.github.com/brando90/8a40e83df107f5a915ba105d9fb0121c
$ ruby-build --definitions
1.8.5-p52
1.8.5-p113
...
The gist is that all 3.x.x are missing.
I tried updating ruby-build manually, but it failed
I did do:
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
then
git -C "$(rbenv root)"/plugins/ruby-build pull
Then I installed 3.1.2 with rbenv. It seemed to succeed, but then ruby --version doesn't seem right(?).
bot#e3a50e4f740f:~$ rbenv install 3.1.2
To follow progress, use 'tail -f /tmp/ruby-build.20221207002433.1304.log' or pass --verbose
Downloading ruby-3.1.2.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.1/ruby-3.1.2.tar.gz
Installing ruby-3.1.2...
Installed ruby-3.1.2 to /home/bot/.rbenv/versions/3.1.2
bot#e3a50e4f740f:~$ ruby --version
ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [aarch64-linux-gnu]
bot#e3a50e4f740f:~$ rbenv global 3.1.2
bot#e3a50e4f740f:~$ ruby --version
ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [aarch64-linux-gnu]
Option: Official rbenv instructions
Alas, that didn't work :frowning:
$ rbenv install 3.1.2
rbenv: no such command `install'
I did:
mkdir ~/.rbenv
cd ~/.rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bashrc
bash
# exec $SHELL
rbenv install 3.1.2
Using the official instructions: Seamlessly manage your app’s Ruby environment with rbenv, Basic Git Checkout
Is it probably best to use a Ruby Docker container? But how do I get one with the specific version I want 3.1.2 (or any specific version)?
All options I tried to install ruby on a Docker container with specific version 3.1.2, likely better to just use a Ruby image instead, any recommendations for 3.1.2?
An idea for a bounty: force insert the FROM ruby:3.1.2 image into running container
I still can't install Ruby on a already-running container :(, is it possible to layer on top of the running image the ruby 3.1.2 image into the running container?
References:
Crossposted: Why can't I install Ruby 3.1.2 in a Linux Docker container?
All attempts are documented at ruby_install_ubuntu.sh - the Discord channel for Ruby was really helpful, even though we didn't solve it: https://discord.com/channels/518658712081268738/650031651845308419
This doesn't work on an Ubuntu Docker container which is the title of the question, but it at least worked on the HPC with Ubuntu I was using, so it can still be useful for future readers, a real answer on an Ubuntu Docker container is still needed and wanted.
# - install rbenv for installing the Ruby interpreter
mkdir ~/.rbenv
cd ~/.rbenv
git clone https://github.com/rbenv/rbenv.git .
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc.user
echo 'eval "$(rbenv init -)"' >> ~/.bashrc.user
exec $SHELL
bash
source ~/.bashrc.user
rbenv -v
# - install ruby-build
mkdir ~/.ruby-build
cd ~/.ruby-build
git clone https://github.com/rbenv/ruby-build.git .
export PATH="$HOME/.ruby-build/bin:$PATH"
echo 'export PATH="$HOME/.ruby-build/bin:$PATH"' >> ~/.bashrc.user
exec $SHELL
bash
source ~/.bashrc.user
ruby-build --version
# - install ruby without sudo -- now that ruby build was install
mkdir -p ~/.local
# ruby-build 3.1.2 ~/.local/
rbenv install 3.1.2
rbenv global 3.1.2
ruby -v
which ruby
Given the giant effort I've put so far, I think the best is just to rebuild the Docker image from scratch, starting from all the Docker images you need. For me I need to start from these:
FROM continuumio/miniconda3
# FROM --platform=linux/amd64 continuumio/miniconda3
#FROM ubuntu:20.04
#FROM ubuntu:18.04
FROM ruby:3.1.2
But rebuilding the image, I tested to see if the right Ruby image was available once I started the Ruby 3.1.2 container (if you want a different one, I assume going to the Docker website for Ruby works or try different version tags or do latest). Output:
(meta_learning) brandomiranda~ ❯ docker run -it --rm ruby:3.1.2 bash
Unable to find image 'ruby:3.1.2' locally
3.1.2: Pulling from library/ruby
077c13527d40: Pull complete
a3e29af4daf3: Pull complete
3d7b1480fa4d: Pull complete
426e8acfed2a: Pull complete
7301bf329e1e: Pull complete
1e6ce022773b: Pull complete
08fd3b7f7d99: Pull complete
2a797222ff77: Pull complete
Digest: sha256:7681a3d37560dbe8ff7d0a38f3ce35971595426f0fe2f5709352d7f7a5679255
Status: Downloaded newer image for ruby:3.1.2
root#70d03f6408cd:/# ruby --version
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [aarch64-linux]
You can use rvm instead of rbenv.
website: https://rvm.io. it works on docker.
here are the commands for ubuntu docker image
curl -sSL https://get.rvm.io | bash -s stable
rvm install 3.1.2
ruby -v // 3.1.2

How to install Ruby on docker?

I am trying to install ruby on docker. I could install the 1.9 versions but it is not possible to install the latest version such as 2.2.0 and above. I am actually trying to set up calabash on docker. Have tried this. Whenever I try to install calabash-android in it getting the error
ERROR: Error installing calabash-android:
luffa requires Ruby version >= 2.0.
If you're starting FROM a different base Docker instance, you can simply RUN commands that install Ruby from your base instance's package management system. For example, this GitHub Gist shows how to use apt-get to install Ruby on a Ubuntu instance:
# Pull base image.
FROM dockerfile/ubuntu
# Install Ruby.
RUN \
apt-get update && \
apt-get install -y ruby
And this Gist shows a Dockerfile that's configured to install RVM and Ruby on a Ubuntu instance:
FROM ubuntu
RUN apt-get update
# basics
RUN apt-get install -y openssl
# install RVM, Ruby, and Bundler
RUN \curl -L https://get.rvm.io | bash -s stable
RUN /bin/bash -l -c "rvm requirements"
RUN /bin/bash -l -c "rvm install 2.0"
RUN /bin/bash -l -c "gem install bundler --no-ri --no-rdoc"
This makes ruby available for any future RUN command and not just bash:
FROM debian:stretch-slim
RUN \
apt-get update && apt-get install -y --no-install-recommends --no-install-suggests curl bzip2 build-essential libssl-dev libreadline-dev zlib1g-dev && \
rm -rf /var/lib/apt/lists/* && \
curl -L https://github.com/sstephenson/ruby-build/archive/v20180329.tar.gz | tar -zxvf - -C /tmp/ && \
cd /tmp/ruby-build-* && ./install.sh && cd / && \
ruby-build -v 2.5.1 /usr/local && rm -rfv /tmp/ruby-build-* && \
gem install bundler --no-rdoc --no-ri
You could start view a dockerfile starting with:
# 2016
FROM ruby:2.3.0
# 2020
# Import your ruby version
FROM ruby:2.7.1
# Install bundler gem
RUN gem install bundler
# Assign a work directory
WORKDIR /work
That would use the docker image ruby, with ruby already installed.
The 2020 version comes from "Ruby version management with docker" from Arjun Das, mentioned by ArMD in the comments.
Low reputation so I can't comment inline (all those years of lurking, sigh), but in case anyone else happens across this while searching for ways to install old ruby versions to docker, I found #grosser's answer very helpful - it worked where trying to install via RVM simply wouldn't, at least for me.
I would, however, recommend using the recommended approach for installing ruby-build - the following worked for me:
<prior steps>
RUN git clone https://github.com/rbenv/ruby-build.git && \
PREFIX=/usr/local ./ruby-build/install.sh && \
ruby-build -v 2.4.1 /usr/local && \
gem install bundler -v <VERSION HERE> --no-ri --no-rdoc && bundle install
<following steps>
Key point here is that this keeps you up to date with ruby-build instead of being hard-coded to the 2018-03-29 version as in a previous #grosser's comment.
If you want to use things like bundle install and don't use a base image with pre-installed devtools like Ubuntu, you need to install these packages:
RUN apt-get update && apt-get install -y ruby ruby-dev ruby-bundler build-essential
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
Thanks to #Jacob and #grosser, I've managed to set up mine in a similar, if a bit more unpacked way:
# Install Local ruby
RUN git clone https://github.com/rbenv/rbenv.git ~/.rbenv \
&& echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc \
&& echo 'eval "$(rbenv init -)"' >> ~/.bashrc
ENV HOME /home/jenkins # Change this dir as needed.
ENV PATH "$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"
ENV RUBY_VERSION 2.6.3
RUN mkdir -p "$(rbenv root)"/plugins \
&& git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
RUN rbenv install $RUBY_VERSION
RUN rbenv global $RUBY_VERSION && rbenv versions && ruby -v
# RUN curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash # Uncomment this to get rbenv to validate your setup.

ImageMagick on Google Cloud

I use ImageMagick on the Google Cloud Platform. I use rails and Google's App Engine Flexible Environment. So the problem is I want to upload an Image to process in more sizes. I use paperclip.
The error is:
Could not run the identify command. Please install ImageMagick.
So my question is how I can solve this issue? Uploading a Image without processing works 100%. But the issue is the processing I think. So paperclip needs ImageMagick to process the images.
The problem is I use App Engine Flexible Environment so I am not sure how to install it. I already tried it with apt-get install imageMagick
Okay really simple you need just a Dockerfile.
# This Dockerfile for a Ruby application was generated by gcloud.
# The base Dockerfile installs:
# * A number of packages needed by the Ruby runtime and by gems
# commonly used in Ruby web apps (such as libsqlite3)
# * A recent version of NodeJS
# * A recent version of the standard Ruby runtime to use by default
# * The bundler and foreman gems
FROM gcr.io/google_appengine/ruby
# Install ruby 2.3.0 if not already preinstalled by the base image
RUN cd /rbenv/plugins/ruby-build && \
git pull && \
rbenv install -s 2.3.0 && \
rbenv global 2.3.0 && \
gem install -q --no-rdoc --no-ri bundler --version 1.11.2 && \
gem install -q --no-rdoc --no-ri foreman --version 0.78.0
ENV RBENV_VERSION 2.3.0
# To install additional packages needed by your gems, uncomment
# the "RUN apt-get update" and "RUN apt-get install" lines below
# and specify your packages.
# RUN apt-get update
# RUN apt-get install imagemagick -y
RUN apt-get update && apt-get install imagemagick -y
# Install required gems.
COPY Gemfile Gemfile.lock /app/
RUN bundle install && rbenv rehash
# Start application on port 8080.
COPY . /app/
ENTRYPOINT bundle exec rackup -p 8080 -E production config.ru
After that just run gcloud preview app deploy and it will work for you.
Don't forget to change in your app.yaml runtime: ruby to runtime: custom
That's it happy coding 🎉

Can't connect to rails server on local machine

TL;DR I randomly can't connect to a local Rails server and the problem follows me across computers.
I'm working on a website that uses Rails.
I started my work on an Arch VM using VirtualBox. Inside that VM I could start up rails s and connect to the website at 0.0.0.0:3000 no problem. I was using Chrome from inside the VM to connect to the website.
I added one line of code. I saved the file but did NOT commit or stage. Couldn't connect anymore.
Erased the line I and saved again. Still couldn't connect.
Deleted the folder the site was in. Recloned the repo. Reran the setup script (installs things like rvm, node.js, and rails; see end of post). No luck.
Exited the VM, booted up an Ubuntu install, cloned the repo again (this time into an NTFS drive because that's where I have the most space), ran setup again. Nope.
What was weird is that even though the rails server started, I appeared to not have rails on my system.
For some reason, node installed, rvm and rails did not.
Moved to ~, cloned repo yet again, ran setup again. Same deal. Don't have rails. Although again, rails s in the setup script ran but I couldn't connect in the browser.
Installed rvm, installed bundler, ran bundle install --without production staging just like in the script, and ran the rakes. rails s. Still can't connect.
Nmap says I'm accepting connections on port 3000.
So overflowers, any ideas on how a local connectivity problem could follow me across a computer with no changes to the code base? Or how things like rails can install and not install?
Setup script:
#vagrant
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
#Dependencies
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 python-software-properties libffi-dev -y
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev -y
sudo add-apt-repository ppa:chris-lea/node.js -y
sudo apt-get update
sudo apt-get install nodejs -y
#install rvm and ruby
curl -sSL https://get.rvm.io | bash -s stable --ruby
source ~/.rvm/scripts/rvm
echo "" >> ~/.bashrc
echo "# Auto-Generated source for RVM" >> ~/.bashrc
echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
rvm install 2.0
rvm use 2.0 --default
ruby -v
#Set up repo
ssh-keyscan github.com >> ~/.ssh/known_hosts
gem install bundler
bundle install --without production staging
rake db:create
rake db:migrate
rake db:seed
rails s
You are incorrectly connecting to your rails server.
=> Rails x.x.x application starting in development on http://0.0.0.0:3000
The line above states the ip address the server is listening on.
To connect to the server located in the same machine use
localhost:3000
If your VM is bridged to your LAN and you want to access it from the host computer, first get the adapter ip using ifconfig and then use it like
ADAPTER_IP:3000
well, you should type 127.0.0.1:3000 or localhost:3000 in the browser or terminal.
Listen 0.0.0.0:3000 means listen all IP addresses in the local machine.

Error running 'requirements_debian_update_system ruby-1.9.3-p551',

Problem facing when installing rvm install 1.9.3
in ubuntu 12.04
Please help me
The problem here is that you didn't reboot (or log out) as per the ubuntu RVM instructions so your user is not (yet) part of the rvm group, and thus has no write permissions on the logs directory.
You can fix this by logging out, rebooting or opening a new shell using
sudo su - $USER
That will create a new shell with your user with the correct rights.
You can run following command from terminal and i think it will solve your problem.
N.B: If you don't need rails simply skip command after ruby installation
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 python-software-properties
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
rvm install 2.1.3
rvm use 2.1.3 --default
ruby -v
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
gem install rails
rails -v
For those who may have issues after running the commands shared on every thread out there, try opening ubuntu or the Linux environment you are working with as an administrator.
search for your app (in my case Ubuntu) using the search bar on the bottom-left of your computer's screen.
if you don't see the option "open as administrator" on the right side, then right-click over the app's icon and you should see it then.
Allow the app to make changes on your computer when prompted and then try the installation commands again.
If that didn't work then try the commands shared in this or other posts, but always as administrator.

Resources