I would like to prevent the updating of a gem on my windows (rmagick), so it sticks to 2.12.0 mswin32. Still, my coworker needs to have the gem on his Darwin install...
So, I tried to do something like this in the Gemfile:
if RUBY_PLATFORM =~ /darwin/i
gem 'rmagick', '~> 2.12.0'
else
gem 'rmagick', '=2.12.0.mswin32'
end
but bundle install complaints.
What is the right way of handling this properly?
You can't use conditionals on gemspec because gemspec is serialized
into YAML, which doesn't contain executable code.
I faced a related problem in the Gemfile of a local Rails project (not a
gem).
Currently, the Gemfile contains:
group :test do
...
# on Mac os X
gem 'rb-fsevent' if RUBY_PLATFORM.include?("x86_64-darwin")
gem 'ruby_gntp' if RUBY_PLATFORM.include?("x86_64-darwin")
# on Linux
gem 'rb-inotify' unless RUBY_PLATFORM.include?("x86_64-darwin")
gem 'libnotify' unless RUBY_PLATFORM.include?("x86_64-darwin")
end
This works (although it is ugly) for developing on Mac and Linux
systems.
But, we stopped checking in the Gemfile.lock since it changes every time
a developer with a different platform checks in the code.
So, a solution for multi-platform Gemfiles should also solve the
problem for Gemfile.lock.
The other solutions is building multiple .gemspec files for each target OS and change both platform and dependencies for each platform:
gemspec = Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
end
# here build the normal gem
# Now for linux:
gemspec.platform = "linux"
gemspec.add_dependency ...
# build the newer gemspec
...
You should use the platforms option Bundler provides:
If a gem should only be used in a particular platform or set of
platforms, you can specify them. Platforms are essentially identical
to groups, except that you do not need to use the --without
install-time flag to exclude groups of gems for other platforms.
So in your specific case that would look something like this:
gem 'rmagick', '~> 2.12.0', :platforms => :ruby
gem 'rmagick', '=2.12.0.mswin32', :platforms => :mswin
Related
The name of a gem is not the same as the module. Right now I need to require it and include in various types of files, such as controllers and models. This is amounting to many requires which I don't have to do for other gems. It's a gem I can update. Is there a way the gem needs to be configured so it is "attached" to Rails, and if so, where may I find instructions to do this?
UPDATE: using required: "name-of-module" in Gemfile removes need for require everywhere. Still wondering, if gem could be configured to not require this in Gemfile?
In the Gemfile, you can do these things:
# Require a different file than the gem's name
gem 'foo', require: 'bar'
# Install but not require anything.
# You need to manually require the gem somewhere.
gem 'foo', require: false
You can still add version and platform specification if you want.
Real-world examples are ActiveSupport and rspec:
gem 'activesupport', '~> 5.2', require: 'active_support/all'
gem 'rspec', '~> 3.1', group: :test, require: false
You need to specify which files you want Ruby to load for you, so you either need specify the gem in your Gemfile (and run bundle exec ...) or put require in the right place(s) in your code. There's no way around that.
If it's a gem that you work on at the same time as you use it in another project, then you can specify a path to the gem. Like
# Gemfile
source "https://rubygems.org"
ruby '2.6.1'
gem 'my_gem', path: '/home/user/Development/my_gem'
This way, you can change your gem and use it directly without having to build and install it.
I have created a new application using Ruby on Rails v4.1.0. When attempting to start a server or console on Windows, I am encountering the following error:
$ rails server
Booting WEBrick
Rails 4.1.0 application starting in development on ....
Exiting
c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/tzinfo-1.1.0/lib/tzinfo/data_source.rb:199:
in `rescue in create_default_data_source':
No timezone data source could be found. To resolve this, either install
TZInfo::Data (e.g. by running `gem install tzinfo-data`) or specify a zoneinfo
directory using `TZInfo::DataSource.set(:zoneinfo, zoneinfo_path)`.
(TZInfo::DataSourceNotFound)
from c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/tzinfo-1.1.0/lib/tzinfo/data_source.rb:196:
in `create_default_data_source'
How can I resolve this error?
Resolving the Error
To resolve this error, you'll need to make sure that the tzinfo-data gem is being included in your Gemfile.
First of all, check your Gemfile to see if there is an existing reference to tzinfo-data. If there isn't already a reference, then add the following line:
gem 'tzinfo-data'
You may find that there is already a line like the following:
gem 'tzinfo-data', platforms: [:mingw, :mswin]
If you are using a 64-bit version of Ruby on Windows, then add :x64_mingw to the list of platforms as follows:
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]
Alternatively, you can remove the platforms option altogether.
Note that there is a bug in Bundler that means there's currently no platform option that will match 64-bit Ruby 3.1 on Windows. The solution here to remove the platforms option.
After doing this, run bundle update at the command line to install the tzinfo-data gem and you'll then be able to start your Rails server or console.
Background
The TZInfo::DataSourceNotFound error is being raised by TZInfo, a dependency of the Active Support component of Rails. TZInfo is looking for a source of time zone data on your system, but failing to find one.
On many Unix-based systems (e.g. Linux), TZInfo is able to use the system zoneinfo directory as a source of data. However, Windows doesn't include such a directory, so the tzinfo-data gem needs to be installed instead. The tzinfo-data gem contains the same zoneinfo data, packaged as a set of Ruby modules.
Rails generates a default Gemfile when the application is first created. If the application is created on Windows, then a dependency for tzinfo-data will be included. However Rails (up to version 4.1.x) omitted :x64_mingw from the list of platforms and therefore didn't work correctly on 64-bit Windows versions of Ruby.
I had to add two gems to get the server to start..
gem 'tzinfo-data'
gem 'tzinfo'
Then bundle install.
I had that error when trying to install Redmine in a Docker container:
RAILS_ENV=production bundle exec rake db:migrate
gave me the error because package tzdata was not installed in my Ubuntu image.
apt-get update && apt-get install tzdata -y
did the trick.
Just put this in your app terminal :
gem install tzinfo-data
then change the gemfile line to :
gem 'tzinfo-data', platforms: [:x64_mingw, :mingw, :mswin]
then again in your terminal :
bundle update
That will solve the problem directly.
Add the following line to your Gem File
gem 'tzinfo-data', platforms: [:x64_mingw,:mingw, :mswin]
In Gemfile, I added this following line
#tzinfo-data
gem 'tzinfo-data', '~> 1.2021', '>= 1.2021.5'
and then, I commented out the following line
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
and then I simply ran
rails server
and it worked for me successfully.
Here is my full Gemfile
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby "3.1.1"
#tzinfo-data
gem 'tzinfo-data', '~> 1.2021', '>= 1.2021.5'
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.2", ">= 7.0.2.3"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
# Use sqlite3 as the database for Active Record
gem "sqlite3", "~> 1.4"
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"
# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"
# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"
# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
# gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
# Use Sass to process CSS
# gem "sassc-rails"
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
end
group :development do
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"
# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
end
group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "capybara"
gem "selenium-webdriver"
gem "webdrivers"
end
Maybe tzinfo is not installed on your system, try to install it:
gem install tzinfo
gem install tzinfo-data
I had this problem too and fixed it by adding BOTH the :x64_mingw to the list of platforms for tzinfo-data, AND the gem 'tzinfo' to the gemfile. Then bundle install.
I ran into this issue on macOs Mojave 10.14.5 and I found out that it was something with how my symlink in macOs wasn't reading the correct provided zone info files.
I was able to track this down with where the files should be using the command
TZInfo::ZoneinfoDataSource.search_path and that provided the output of ["/usr/share/zoneinfo", "/usr/share/lib/zoneinfo", "/etc/zoneinfo"].
I began looking into /usr/share/zoneinfo and there were files available to read. However rails still wasn't finding them, reading them, executing them..? So I then created a symlink from the other file in /usr/share/zoneinfo.default/ to /etc/zoneinfo (the last path TZInfo looks up)
So finally the command that worked for me to fix this issue was ln -s /usr/share/zoneinfo.default /etc/zoneinfo
Hopefully this information is helpful to someone in the future.
I am using windows and for several days I was not able to solve this problem.
So the solution for our 64 bit platform is to type the following in the terminal:
gem install tzinfo
gem install tzinfo-data
Then afterwards go navigate back to your gem file. In case if you haven't created one create it with rails new xyznameofyours on the terminal.
Then replace the line of code which states something like:
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw] or
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
to this:
gem 'tzinfo-data'
gem 'tzinfo'
Only then after your code will work!
It is possible that installing the gem directly via cmd might not solve the issue on Windows OS.
Here is how it did work for me step-by-step;
Inside the Gemfile folder "ctrl-F" and type "platforms"
Find the line gem "tzinfo-data", platforms: %i[ mingw mswin
x64_mingw jruby ]
Remove everything after "tzinfo-data" (comma included)
Final view should be just gem "tzinfo-data" add also gem "tzinfo"
On the terminal, type "bundler update" or "bundle update"
Once it is completed, type "rails s"
Notice: Even after this, if your server starts but can not resolve the default page (additionally ctrl-c / ctrl-pause/break doesn't work), then add webrick instead of puma by adding the gem gem 'webrick', '~> 1.3', '>= 1.3.1'. After that, to run the webrick server you need to type
rails s -u webrick
1-go to gemfile
add the following line without any arrays only that line
gem "tzinfo-data"
then save the file
2- then on cmd type
bundle install
then press Enter
3-then also on cmd type
rails server
then press Enter
so, the gems didn't quite seem to install properly, i had to do the following
gem 'tzinfo-data'
gem 'tzinfo'
then
bundle show
to see all gems
bundle gem tzinfo
will get you the gem's directory
then, go to that directory. you will need to splice tzinfo-data into tzinfo.
in the tzinfo-data file, go to..
local_pathname/tzinfo-data-1.2014.5/lib/tzinfo
copy all the contents of this directory into...
local_pathname/tzinfo-1.2.1/lib/tzinfo
(for me this meant copying 'data' the file and 'data' the directory)
then
go to
local_pathname/tzinfo-1.2.1/lib
and open the file, tzinfo, (not the directory)
and add this line
require 'tzinfo/data'
this was such a pain to figure out
I've replaced:
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
with:
gem 'tzinfo-data', '~> 1.2021', '>= 1.2021.5'
and it worked fine.
> Blockquote
We have a rails 3 app, it was working great, we deployed it. I had to set up a new computer for development, cloned my repo, set everything up, and now Font Awesome fonts refuse to work (show gibberish). I cloned into my original computer; in my original folder, they work fine. But any new clones do not.
Instead of showing the font, they show the content (gibberish set in the scss file, like '\f010'). This is usually displayed as a strange shape or something, but definitely not the icon. Tried on Mac OS X Mountain Lion and Ubuntu 12.04, Chrome and Firefox.
What we tried:
Made a new branch in original folder. Updated all gems to newest
version - I thought the clone might have newer gems that made it
break somewhere. Original project still looks fine, new clones (and
checking out the branch) are broken.
Replaced the .scss and font files from fresh download from the
fontawesome site.
Verified path is correct for font files (they seem to be found,
because Chrome inspector doesn't complain about missing font files -
if I change the path, it does complain. They just aren't being
used/used properly)
Gem list in both folders show same gems with same versions.
FontAwesome path is:
myapp/vendor/assets/stylesheets/FontAwesome
with the sass and fonts folders inside of that.
The only thing changed in the sass was the path line:
$fontAwesomePath: "FontAwesome/font/fontawesome-webfont" !default;
Which, as I said, seems to work; if I change the path, chrome complains about the missing fonts.
I am using Apache 2, Ruby ree-1.8.7 p-358, Rails 3.2.7, Passenger 3.0.12, RVM 1.14.10, Bundler 1.1.4. Passenger and bundler sit in my global gemset. Here is my Gemfile:
# myapp/Gemfile
# The specified versions are to make sure everything is as it is in the
# original folder; didn't make a difference. But the original branch has
#them and is working.
source 'https://rubygems.org'
gem 'rails', '3.2.7'
gem 'chronic', '0.6.7'
gem 'enumerated_attribute', '0.2.16'
gem 'exception_notification', '2.6.1'
gem 'google-api-client', '0.4.5'
gem 'her', '~> 0.2.6'
gem 'jquery-rails', '2.0.2'
gem 'mysql2', '0.3.11'
gem 'paperclip', '~> 2.7.0'
gem 'pdfkit', '0.5.2'
gem 'remotipart', '~> 1.0'
gem 'settingslogic'
gem 'slim-rails'#, '1.0.3'
gem 'validates_timeliness', '~> 3.0.2'
gem 'whenever', '0.7.3', :require => false
group :assets do
gem 'compass-rails'#, '1.0.3'
gem 'sass-rails'#, '3.2.5'
end
gem 'coffee-rails'#, '3.2.2'
gem 'uglifier'#, '1.2.7'
group :development do
gem 'quiet_assets', '1.0.1'
gem 'thin' , '1.4.1'
end
gem 'letter_opener', '0.0.2', :group => [:development, :test, :staging]
gem 'rspec-rails', '2.11.0', :group => [:development, :test]
group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails', '1.7.0'
gem 'guard-rspec', '1.2.1'
gem 'shoulda', '3.1.1'
end
If anyone needs more info just let me know. I've run out of ideas and I'm not getting anywhere, and I'm worried to deploy now and have our prod env messed up!
EDIT: Still not getting results. I updated to Rails 3.2.8 and all gems are at their latest, except for paperclip and factory_girl_rails, which use older versions for Ruby 1.8.7.
Destroyed all my folders, cloned a new one, works fine. Cloned a second, no good. Open first clone in Chrome incognito window, doesn't work. Inspector -> Resources -> Frames -> (site) -> Fonts shows the font files.
Here's an image of what it should look like (left) vs the problem (right):
try this, i recently had a similar issue,
if you put this at the top of your CSS files,
/*
*= require font-awesome
*/
and then use this gem 'font-awesome-rails' it should work
I'm developing a Rails app on a Mac, and I'm new to testing, so I just added these gems to my Gemfile:
group :test, :development do
gem 'rspec-rails'
gem 'rb-fsevent'
gem 'growl'
end
But my production server runs Linux, so even if they aren't grouped in :production, bundler (v1.0.21) still attempts to install them. ...and fails, obviously!
extconf.rb:19:in '<main>': Only Darwin (Mac OS X) systems are supported (RuntimeError)
Setting RAILS_ENV to production before running bundle install doesn't work.
It worked by running bundle install --without development test, but how can these gems be taken into consideration by bundler only based on your OS?
Edit: The bundler wiki provides details on how to use platform as a parameter.
The same solution is given in bundler issue #663, so I tried:
group :test, :development do
gem 'rspec-rails'
platforms :darwin do
gem 'rb-fsevent'
gem 'growl'
end
end
bundle install does not work, but even if we go back to square one and do
bundle install --without darwin, the result is 'darwin' is not a valid platform.
The available options are: [:ruby, :ruby_18, :ruby_19, :mri, :mri_18, :mri_19, :rbx, :jruby, :mswin, :mingw, :mingw_18, :mingw_19]
Any other (elegant) approaches?
The Bundler wiki has a method that adds all gems to Gemfile.lock regardless of platform. It sets require => false depending on the system so you don't need to be able to actually run the gems:
gem 'rb-inotify', :require => RUBY_PLATFORM.include?('linux') && 'rb-inotify'
And they provide helper methods to make this clean:
def os_is(re)
RbConfig::CONFIG['host_os'] =~ re
end
gem 'rb-fsevent', "~> 0.9.3", platforms: :ruby, install_if: os_is(/darwin/)
gem 'rb-inotify', "~> 0.8.8", platforms: :ruby, install_if: os_is(/linux/)
gem 'wdm', "~> 0.1.0", platforms: [:mswin, :mingw. :x64_mingw], install_if: os_is(/mingw|mswin/i)
On my Windows 7 x64 system running Ubuntu 12.04 in a Vagrant VM, this worked fine, but the :platforms setting was required - the Linux bundler choked on the 'win32console' gem:
Console.c:1:21: fatal error: windows.h: No such file or directory
Gemfile actually is a regular ruby file, so you can use something like
case RUBY_PLATFORM
when /darwin/
gem 'foo'
when /win32/
gem 'bar'
end
#zed_0xff: found a similar approach in an older rspec-core commit:
if RUBY_PLATFORM =~ /darwin/
gem 'foo'
end
You can use :install_if method which accepts arbitrary lambda.
Following example comes directly from Gemfile's man pages:
install_if -> { RUBY_PLATFORM =~ /darwin/ } do
gem "pasteboard"
end
It is much better than control flow constructs (e.g. if) as it maintains dependencies correctly and keeps Gemfile.lock uniform on all machines.
According to the Bundler docs, you need to use the platforms directive:
#Gemfile
platforms :mswin do
gem "x"
end
gem "weakling", :platforms => :jruby
gem "ruby-debug", :platforms => :mri_18
gem "nokogiri", :platforms => [:mri_18, :jruby]
There are a number of Gemfile platforms:
ruby
C Ruby (MRI) or Rubinius, but NOT Windows
ruby_18
ruby AND version 1.8
ruby_19
ruby AND version 1.9
ruby_20
ruby AND version 2.0
mri
Same as ruby, but not Rubinius
mri_18
mri AND version 1.8
mri_19
mri AND version 1.9
mri_20
mri AND version 2.0 rbx Same as ruby, but
only Rubinius (not MRI
jruby
JRuby
mswin
Windows
mingw
Windows 'mingw32' platform (aka RubyInstaller)
mingw_18
mingw AND version 1.8
mingw_19
mingw AND version 1.9 mingw_20 mingw AND version 2.0
I just made my first setup of RoR, and creating a new application works fine. But when I want to run the rails server I get the following error:
C:/Ruby192/lib/ruby/gems/1.9.1/gems/delayed_job-2.1.3/lib/delayed/yaml_ext.rb:30:in 'remove_method': method 'to_yaml' not defined in Class (NameError)
I ran the "bundle install" in the app directory and installed all the necessary files (with some problems however, but I excluded the gems with problems in the gemfile) and the last bundle install said that I have successfully installed all the needed packs.
I'm lost and I can't find a similar error on the internet. Can you help me?
EDIT: I forgot to mention that I'm not having a problem with a new application. It's running an existing one (that I didn't build, but works fine for others) that the error is related to. Here's the gemfile for that app:
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'mysql'
gem "haml", ">= 3.0.0"
gem "haml-rails"
gem 'simple_form'
gem 'delayed_job'
#gem 'auto_crawlers'
gem 'will_paginate', '~> 3.0.beta'
group :test do
gem 'factory_girl_rails'
gem 'mocha'
end
group :development do
#gem "nifty-generators", "0.4.3", :git => "git://github.com/JonasNielsen/nifty-generators.git"
gem 'fastercsv'
end
gem "mocha", :group => :test
Do you think the error is because I left out the two gems with "#" ? Those were causing problems at first, and I don't think I need them to test some minor changes in the app (some views that I have to modify)
The issue is being described, and supposedly fixed here: https://github.com/collectiveidea/delayed_job/issuesearch?state=open&q=yaml#issue/194
Try this from the command line: irb -rubygems -r delayed_job and then from your bundled directory bundle-exec irb -rubygems -r delayed_job which will show if there is a difference between your system gems and your bundled setup - you might see an error in one or both attempts to run IRB.
If there is an error using bundle-exec but not with your system gems then it's a bundler issue. If not, are you sure the app is designed to function under Ruby 1.9? It looks like to_yaml isn't available at the point DJ is required, which implies it probably needs a require "yaml" somewhere.