I'm playing around with a microservice automation pipeline that builds out a bunch of templated code.
Currently, the pipeline code is automatically running via guard and generates a shell script to create databases, create new rails application and other types of assets.
I run ruby & rails within RbEnv and the script that I am executing works fine from the command line, it creates a brand new Rails 6 application.
But, when I run it from guard which has an existing Rails 5.2 environment Application, the new application is built as a Rails 5.2 app.
I have tried overriding certain settings, but it still picks up the environment that my code is running in.
When I run the script from the command line:
echo 'SHOULD CREATE A RAILS APP using VERSION 6.0.0 on RUBY 2.6.3'
gem install rails -v 6.0.0
echo 'rails -v'
rails -v
echo 'rbenv local'
rbenv local
echo 'rbenv version'
rbenv version
echo 'rbenv versions'
rbenv versions
rails _6.0.0_ new . -d postgresql --force --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-spring --skip-test --skip-bundle --skip-webpack-install
I get the following output log
Successfully installed rails-6.0.0
1 gem installed
SHOULD CREATE A RAILS APP using VERSION 6.0.0 on RUBY 2.6.3
rails -v
Rails 6.0.0
rbenv local
2.6.3
rbenv version
2.6.3 (set by /Users/myname/dev/myapp/.ruby-version)
rbenv versions
system
2.4.1
2.4.6
2.5.3
* 2.6.3 (set by /Users/myname/dev/myapp/.ruby-version)
With following GEM file
ruby '2.6.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.0'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
And confirmation via the last line that the application is Rails 6
remove config/initializers/new_framework_defaults_6_0.rb
When I run the same script from ruby using variations on system and fork { exec { } }
Dir.chdir File.dirname(output_file) do
# fork { exec("bash #{output_file}") }
system "bash #{output_file}"
# system "/usr/local/bin/zsh #{output_file}"
end
I get Rails 5.2
rails -v
Rails 5.2.3
rbenv local
rbenv: no local version configured for this directory
rbenv version
2.5.3 (set by RBENV_VERSION environment variable)
rbenv versions
system
2.4.1
2.4.6
* 2.5.3 (set by RBENV_VERSION environment variable)
2.6.3
I don't really understand why you'd want to do this in ruby, it seems like you're trying to reinvent something like what docker or kubernetes already has solved pretty well.
But I suspect the problem with your script is the bash session doesn't have the rbenv stubs you want. I would find where they are on your computer or server's filesystem and then specify exact paths to them when running commands like gem, rails, or bundler.
Related
I'm trying to deploy my Rails6 app to Heroku
I'm able to successfully build after pushing my app to Heroku but the app immediately crashes.
I just added
group :production do
gem 'pg', '~> 1.2', '>= 1.2.3'
end
to my Gemfile after developing in sqlite so I figured I'd need to migrate which brings me to my error
peter.walker#MACSB-RJ2CLKF45C friends % heroku run rake db:migrate
Running rake db:migrate on ⬢ ptw-friends... up, run.5761 (Free)
Your Ruby version is 2.7.0, but your Gemfile specified 2.6.10
I'm not sure why I'm being told my Ruby version is 2.7.0.
My Gemfile has ruby '2.6.10',
my Gemfile.lock has RUBY VERSION ruby 2.6.10p210,
and my $ ruby -v returns ruby 2.6.10p210 (2022-04-12 revision 67958) [arm64-darwin21]
Could it be from adding
"scripts": {
"start": "rails s"
},
to my package.json?
Could I not be specifying the Ruby version in Procfile?
The full error is here along with my repo
https://github.com/petertimwalker/friends/issues/1
Heroku doesn't support all Ruby versions. When a Ruby version reaches end-of-life and is not maintained anymore then Heroku stops supporting it.
Ruby 2.6 reached end-of-life December last year.
Therefore you need to update your application and your local environment to at least 2.7 when you still want to be able to deploy on Heroku.
Please see the list of Ruby versions supported by Heroku.
install ruby version 2.6.10 and use that version in local
if you using rvm check your current version with rvm list and
install ruby 2.6.10 with rvm install 2.6.10
then use command rvm use 2.6.10
I am working on migration our Ruby on Rails application from AWS platform Amazon Linux 1 to Amazon Linux 2 Platform.
I created an Elastci Beanstalk using Amazon Platform: Ruby 2.6 and Amazon Linux 2, and selected Amazon sample application as the first deployed application.
After modifying the Gemfile and deploment scripts of our EB application, I deployed new application to above environment.
In Gemfile, ruby, resque and resque-scheduler included:
ruby '2.6.8'
gem 'rails', '~> 6.1'
gem 'resque', '~> 2.0.0'
gem 'resque-scheduler', '~> 4.4.0'
...
After deployment, I access the EC2 instance through SSH and check the ruby on rails version . I get below response:
[ec2-user#ip-10-0-2-203 ~]$ ruby -v
ruby 2.6.8p205 (2021-07-07 revision 67951) [x86_64-linux]
[ec2-user#ip-10-0-2-203 ~]$ rake --version
rake, version 12.3.3
[ec2-user#ip-10-0-2-203 ~]$ rails --version
-bash: rails: command not found
It seems Rails is not installed properly in the platform.
In Amazon Linux2, why rails cannot be installed through Gemfile during deployment process?
In our project, there are many dependencies are expected to be installed through Gemfile. But like rails, I concern some of these dependencies may not be installed through Gemfile anymore.
Rails, resque and resque-scheduler are automatically installed through Gemfile when we deploy the application to platform Amazon Linux1, but it seems something different in Amazon Linux2.
Can we still use Gemfile for install dependencies in Amazon Linux 2?
I create a gemset with
$ rvm gemset create r3
$ rvm gemset use r3
$ gem install rails -v3.2.13
At this point
$ rails -v
now shows
$ Rails 3.2.13
but every time I do bundle with a project I've forked, I find that
rails -v
shows Rails 4.0.1 - which then gives issue with the project in question when running tests
(4.0.1 conflicts with 3.2.13).
My question is - if my Gemfile only has:
$ cat Gemfile
source 'https://rubygems.org'
gemspec
how is this happening? How can I make my command line ruby version stay at 3.2.13 and not switch to 4.0.1 Once it switches to 4.0.1 I seem to be stuck with that for that gemset and to create a 3.2.13 gemset I have to start over again.
Bundle works by finding the most up-to-date version of the gems that are compatible with the restrictions from the Gemfile. In this case, those restrictions are coming from the gemspec file, which presumably allows versions of Rails greater than 3.2.13. So it's picking the most up-to-date version of Rails allowed - which is 4.0.1. Your RVM configuration is not relevant here.
To lock your particular fork to Rails 3.2.13, just add the following:
gem 'rails', '3.2.13'
to the Gemfile in your fork. This will lock the local version to 3.2.13 when you run bundle.
if you wish to explicitly use different version of rails then the one that would be calculated from Gemfile then you need to use:
NOEXEC_DISABLE=1 rails ...
you can make it permanent for single shell session with:
export NOEXEC_DISABLE=1
rails ...
and to disable loading gems in versions calculated via Gemfile put it in you shell initialization file (like ~/.bash_profile or ~/.zlogin):
export NOEXEC_DISABLE=1
this happens because RVM installs gem rubygems-bundler which automatically analyzes your Gemfile when you run gem binaries and if a version specified via this file is available then it is loaded (even if it is specified only as dependency of your gems).
yulong#ubuntu:~$ rails -v
Rails 3.1.0
yulong#ubuntu:~$ cd four
yulong#ubuntu:~/four$ rails -v
Rails 3.1.0.rc8
yulong#ubuntu:~/four$ cd ..
yulong#ubuntu:~$ cd z
yulong#ubuntu:~/z$ rails -v
Rails 3.1.0.rc6
Use rvm on Linux/OSX and DevKit on Windows
For every project(folder) you can customize ruby and rails versions. Then rvm automatically will set all gems(libraries) for every folder accordingly to your settings.
If you just need to set Rails versions and not Ruby versions per project add the rails version to the Gemfile in each project folder
gem 'rails', '3.1.0'
and run
bundle install
I have two versions of rails (2.1.0 and 2.2.2) installed in my computer.
When I create a new application, is it possible to specify that I want to use the older (2.1.0) version?
I found here an undocumented option to create a new application using an older version of Rails.
rails _2.1.0_ new myapp
Here is the command which I use normally:
rails _version_ new application_name
for example rails _7.0.4_ new my_app
Here is the list of all available rails versions so far:
http://rubygems.org/gems/rails/versions
I was having some trouble using rails _version_ new application_name (the resulting project was still generated for the newest version of Rails installed.)
After a bit of digging I found an article by Michael Trojanek with an alternative approach. This works by creating a folder with a Gemfile specifying the desired version of Rails and then using bundle exec rails... so that Bundler takes care of running the appropriate version of rails. e.g. to make a new Rails 4.2.9 projects the steps are:
mkdir myapp
cd myapp
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '4.2.9'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
bundle update
As rightly pointed out by #mikej for Rails 5.0.0 or above, you should be following these steps:
Create a directory for your application along with a Gemfile to specify your desired Rails version and let bundler install the dependent gems:
$ mkdir myapp
$ cd myapp
$ echo "source 'https://rubygems.org'" > Gemfile
$ echo "gem 'rails', '5.0.0.1'" >> Gemfile
$ bundle install
Check that the correct version of rails has been installed: $ bundle exec rails -v
Now create your application, let Rails create a new Gemfile (or rather overwrite the existing one by using the --force flag) and instead of installing the bundle (--skip-bundle) update it manually:
$ bundle exec rails new . --force --skip-bundle
If you check the entry for rails in Gemfile, it should be like this:
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
You should update it to the exact version needed for the application:
gem 'rails', '5.0.0.1'
Now, the final step:
$ bundle update
There are two ways to achieve this:
one as suggested in accepted answer:
gem install rails -v 2.1.0 #only when the gem has not been installed in the desired ruby version you are using, so that you don't get error on next step
rails _2.1.0_ new my_app
and alternative method is to create gemfile with desired rails version before initializing rails project
mkdir my_app
cd my_app
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '2.1.0'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
I have written about this in details in my article
You can generate the skeleton with either version and require the one you want in config/environment.rb:
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.1.2' unless defined? RAILS_GEM_VERSION
or use the "rails" command form the version you want anyway.
You should also take a look at "freezing" your Rails gems into the app. This helps a lot with deployment, specially in shared hosting environments.
Just change the RAILS_GEM_VERSION variable in config/environment.rb and issue the freeze rake task:
rake rails:freeze:gems
Please watch out which version of ruby you are using with Rails.
The command for making a new project for a specific version of Rail may not work for you. I had some issues about it. And the problem was the ruby version I have default which is 3.0.0. This version did not work with Rails 5. Then I installed ruby 2.7.5 and switched to it as default. Only then I was able to make projects both for Rails 5 and 7.
If you want the same environment with ruby 2.7.5
rvm install ruby-2.7.5
switch to this version as default
rvm --default use 2.7.5
install bundler and webpacker
gem install bundler
gem install webpacker
install lastest rails (which is 7)
gem install rails
test it
rails new test_app_6
cd test_app_6
rails s
check for localhost 3000
http://localhost:3000
then stop the server (control + c) and install Rails 5
gem install rails -v 5.2.6
test it
rails _5.2.6_ new test_app_5
cd test_app_5
rails s
check for localhost 3000
http://localhost:3000
You're set!