Can't connect to localhost:3000 after creating Rails project - ruby-on-rails

I recently installed Ruby and then installed Rails. I used the some tutorial as a guide for installing with the commands:
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
Then using rbenv:
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.1
rbenv global 2.4.1
ruby -v
It's indicating that ruby is installed properly.
I installed bundler:
gem install bundler
Configured Git:
git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR#EMAIL.com"
ssh-keygen -t rsa -b 4096 -C "YOUR#EMAIL.com"`
I took the newly generated ssh key and pasted it here:
cat ~/.ssh/id_rsa.pub
I installed Rails:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
gem install rails -v 5.1.3
rbenv rehash
then to verify the install:
rails -v
This indicates successful Rails installation.
Then I setup MySQL:
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
Then created a project:
rails new myapp -d mysql
When creating a database:
rake db:create
I get
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
(See full trace by running task with --trace)
When I run
rails server
and visit localhost:3000 my browser indicates that it is unable to connect.
I installed MySQL with no password.
What is the issue? I previously installed PHP7, MySQL, and phpMyAdmin on the same system. Could this be the problem?

When running rake db:create, make sure you in your app's folder:
cd myapp
rake db:create

Related

How to install rails in Ubuntu 16 OS X?

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.

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.

Vagrant/VirtualBox VM provisioning: rbenv installs successfully but subsequent uses in script fail

I am using Vagrant + VirtualBox to set up a virtual machine for my Rails app. I am working on cleaning up a .sh provisioning script that is referenced in Vagrantfile like so:
config.vm.provision "shell", path: "script/provision-script.sh"
The provision script does a number of things, but towards the end it is supposed to install rbenv Ruby versioning and then use rbenv to install Ruby 2.2.1. That part of the provision script looks like this:
echo "setting up rbenv"
# execute the remaining commands as vagrant user, instead of root
sudo -H -u vagrant bash -c "git clone https://github.com/sstephenson/rbenv.git ~vagrant/.rbenv"
sudo -H -u vagrant bash -c "git clone https://github.com/sstephenson/ruby-build.git ~vagrant/.rbenv/plugins/ruby-build"
sudo -H -u vagrant bash -c "git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~vagrant/.rbenv/plugins/rbenv-gem-rehash"
echo "setting up rbenv environment in bash"
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~vagrant/.bashrc
echo 'eval "$(rbenv init -)"' >> ~vagrant/.bashrc
# start new vagrant shell so rbenv will work
echo "building ruby"
su vagrant
rbenv install 2.2.1 && rbenv global 2.2.1 && rbenv rehash && cd /path/to/my/app && gem install bundler rake && rbenv rehash && bundle && rbenv rehash
Everything up to the rbenv install... part works correctly. Installing ruby fails with the following error:
==> default: setting up rbenv
==> default: Cloning into '/home/vagrant/.rbenv'...
==> default: Cloning into '/home/vagrant/.rbenv/plugins/ruby-build'...
==> default: Cloning into '/home/vagrant/.rbenv/plugins/rbenv-gem-rehash'...
==> default: setting up rbenv environment in bash
==> default: building ruby
==> default: /tmp/vagrant-shell: line 73: rbenv: command not found
The script then finishes. I can open the vm with vagrant ssh and then successfully run rbenv install 2.2.1, so I'm guessing that during provisioning a new vagrant shell is not actually being started. I was under the impression that this should happen with su vagrant right before rbenv install 2.2.1.
What can I do to make sure that a new shell is initialized during this provisioning and that the rbenv command will work?
I had a similar problem because I was trying to install rbenv and the vagrant provisioning was giving me the error:
==> default: /tmp/vagrant-shell: line 10: rbenv: command not found
First of all, it is very important to understand that vagrant provisioning script is running in sudo mode.
So, when in the script we refer to ~/ path, we are referring to /root/ path and not to /home/vagrant/ path.
The problem is that I was installing rbenv for the root user and after trying to call rbenv command from a vagrant user and, of course, it didn't work!
So, what I did is specify the vagrant to run the provisioner NOT in sudo user, adding privileged: false:
config.vm.provision :shell, privileged: false, inline: $script
Then in my script I considered everything as being called from the vagrant user.
Here #Casper answer helped me a lot, because it works only specifying:
sudo -H -u vagrant bash -i -c '......'
Since you just updated .bashrc with a new path and other settings, you
will want to run "sudo bash" with the -i option. This will force bash
to simulate an interactive login shell, and therefore read .bashrc and
load the correct path for rbenv.
Below is my final Vagrantfile.
# -*- mode: ruby -*-
# vi: set ft=ruby :
$script = <<SCRIPT
sudo apt-get -y update
sudo apt-get -y install curl git-core python-software-properties ruby-dev libpq-dev build-essential nginx libsqlite3-0 libsqlite3-dev libxml2 libxml2-dev libxslt1-dev nodejs postgresql postgresql-contrib imagemagick
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
sudo -H -u vagrant bash -i -c 'rbenv install 2.1.3'
sudo -H -u vagrant bash -i -c 'rbenv rehash'
sudo -H -u vagrant bash -i -c 'rbenv global 2.1.3'
sudo -H -u vagrant bash -i -c 'gem install bundler --no-ri --no-rdoc'
sudo -H -u vagrant bash -i -c 'rbenv rehash'
sudo -u postgres createdb --locale en_US.utf8 --encoding UTF8 --template template0 development
echo "ALTER USER postgres WITH PASSWORD \'develop\';" | sudo -u postgres psql
SCRIPT
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3000, host: 3000
# config.vm.provider :virtualbox do |vb|
# vb.customize ["modifyvm", :id, "--memory", "1024"]
# end
config.vm.provision :shell, privileged: false, inline: $script
end
Hope it will be helpful to someone else.
I'm surprised the provisioning script exits, as running su vagrant should in theory hang the script at that point (you're running the command su which does not normally exit by itself).
The problem is you cannot change the user that is running a shell script "on the fly" by running su.
Your only option is to use sudo.
Since you just updated .bashrc with a new path and other settings, you will want to run "sudo bash" with the -i option. This will force bash to simulate an interactive login shell, and therefore read .bashrc and load the correct path for rbenv.
So, something like this should hopefully work:
echo "building ruby"
sudo -H -u vagrant bash -i -c 'rbenv install 2.2.1 ...'
There another Solution to install rbenv within Vagrant Provisioning process different than both #Casper and #Diego D solutions.
Using Next Commands before using rbenv commands
export PATH="$HOME/.rbenv/bin:$PATH"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
eval "$(rbenv init -)"
Then run rbenv install commands without sudo -H -u vagrant bash -i -c
Bootstrap.sh File
#!/usr/bin/env bash
sudo apt-get update
echo "========================= install dependencies for install rbenv ==========================="
sudo apt-get install -y autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev
echo "========================= install rbenv =========================================="
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
echo "========================= install ruby build plugin for rbenv ======================="
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
echo "========================= install ruby v2.5.0 =========================================="
export PATH="$HOME/.rbenv/bin:$PATH"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
eval "$(rbenv init -)"
rbenv install 2.5.0
rbenv global 2.5.0
ruby -v
gem -v
echo "========================= install bundler dependencies manager for ruby ====================="
gem install bundler
rbenv rehash
Then VagrantFile file will include vagrant provisioning line
deploy_config.vm.provision :shell, privileged: false, path: "bootstrap.sh"
Source for my Answer from Gits by #creisor

How to check ruby version inside docker container

I have build the docker container by creating below docker file
# Select ubuntu as the base image
FROM ubuntu
# Install nginx, nodejs and curl
RUN apt-get update -q
RUN apt-get install -qy nginx
RUN apt-get install -qy curl
RUN apt-get install -qy nodejs
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# Install rvm, ruby, bundler
RUN curl -sSL https://get.rvm.io | bash -s stable
RUN /bin/bash -l -c "rvm requirements"
RUN /bin/bash -l -c "rvm install 2.1.0"
RUN /bin/bash -l -c "gem install bundler --no-ri --no-rdoc"
# Add configuration files in repository to filesystem
ADD config/container/nginx-sites.conf /etc/nginx/sites-enabled/default
ADD config/container/start-server.sh /usr/bin/start-server
RUN chmod +x /usr/bin/start-server
# Add rails project to project directory
ADD ./ /rails
# set WORKDIR
WORKDIR /rails
# bundle install
RUN /bin/bash -l -c "bundle install"
# Publish port 80
EXPOSE 80
# Startup commands
ENTRYPOINT /usr/bin/start-server
When i go inside the container and give the command ruby -v it throws bash: ruby: command not found
Could any one help me in doing this
I've spent a bit of time messing with RVM, Ruby and Docker recently. This answer might not be what you're looking for, but it needs to be said anyway: if you don't absolutely need RVM, then don't use it in your docker file. You've already noticed one downside: having to pre-empt your commands with /bin/bash -lc. You'll run into another downside if you ever want to have a non-root user run a ruby program in your Docker container. Also, your problem is most likely related to Docker not loading .bashrc or .bash_profile (I forgot which one RVM modifies) when you run a bash shell.
Instead use this to compile Ruby from source:
RUN apt-get update
RUN apt-get install -yq build-essential openssl libreadline6 libreadline6-dev curl git-core \
zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev \
autoconf libc6-dev ncurses-dev automake libtool bison subversion libmysqlclient-dev
ADD http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz /tmp/
RUN cd /tmp && tar -xzf /tmp/ruby-2.1.2.tar.gz
RUN cd /tmp/ruby-2.1.2/ && ./configure --disable-install-doc && make && make install
RUN rm -rf /tmp/*
ADD http://production.cf.rubygems.org/rubygems/rubygems-2.4.1.tgz /tmp/
RUN cd /tmp && tar -xzf /tmp/rubygems-2.4.1.tgz
RUN cd /tmp/rubygems-2.4.1 && ruby setup.rb
RUN rm -rf /tmp/*
RUN echo "gem: --no-ri --no-rdoc" > ~/.gemrc
RUN gem install bundler --no-rdoc --no-ri
You are not setting the default ruby after installing RVM. Trying setting the default ruby after installing it.
RUN /bin/bash -l -c "rvm install 2.1.0"
RUN /bin/bash -l -c "rvm use 2.1.0 --default"

Error when creating a Docker image for Ruby On Rails environment (from a Dockerfile)

I guess it's a environment problem.
When I do it manually (without a Dockerfile) it works.
Here's my Dockerfile:
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get upgrade --assume-yes
RUN apt-get install wget vim git --assume-yes
# install RVM
RUN apt-get install build-essential curl --assume-yes
RUN curl -L https://get.rvm.io | bash -s stable
RUN echo 'source /etc/profile.d/rvm.sh' >> ~/.bashrc
RUN /usr/local/rvm/bin/rvm-shell -c "rvm requirements"
# install Ruby
RUN /usr/local/rvm/bin/rvm-shell -c "rvm autolibs enable"
RUN /usr/local/rvm/bin/rvm-shell -c "rvm install 2.1.2"
# install Rails
RUN echo "gem: --no-rdoc --no-ri" >> ~/.gemrc
RUN gem install rails -v 4.1.5
# install nodeJS
RUN sudo apt-get install nodejs --assume-yes
EXPOSE 3000
Than I build with:
sudo docker build -t="james/rails" .
I get that error:
Step 11 : RUN gem install rails -v 4.1.5
---> Running in 44efc6b7c254
/bin/sh: 1: gem: not found
2014/09/04 18:33:52 The command [/bin/sh -c gem install rails -v 4.1.5] returned a non-zero code: 127
Try RUN /bin/bash -l -c "gem install rails -v 4.1.5" instead of the line you've got in there. Does that change anything?
With the help from Alex Lynham, here's a working Dockerfile:
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install wget vim git --assume-yes
# install RVM
RUN apt-get install build-essential curl --assume-yes
RUN curl -L https://get.rvm.io | bash -s stable
RUN echo 'source /etc/profile.d/rvm.sh' >> ~/.bashrc
RUN /usr/local/rvm/bin/rvm-shell -c "rvm requirements"
# install Ruby
RUN /bin/bash -l -c "rvm autolibs enable"
RUN /bin/bash -l -c "rvm install 2.1.2"
# install Rails
RUN echo "gem: --no-rdoc --no-ri" >> ~/.gemrc
RUN /bin/bash -l -c "gem install rails -v 4.1.5"
# install nodeJS
RUN sudo apt-get install nodejs --assume-yes
EXPOSE 3000
You need to install rubygems before being able to use it.
RUN apt-get install rubygems
see also : Can I install gems with apt-get on Ubuntu?

Resources