Use pg gem with ruby on os x - ruby

I am trying to create a script that will connect to a remote postgresql server. I am using mavericks os x with postgresql and ruby installed on my local computer, but when I try to load pg into a script like this:
#!/usr/bin/ruby
require 'tk'
require 'tkextlib/tile'
require 'rubygems'
require 'pg'
I get the following error:
/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- pg (LoadError)
from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from submit.rb:5:in `<main>'
I have installed os x command line tools and ran the following commands:
sudo gem install pg
sudo PATH=$PATH:/Library/PostgreSQL/9.0/bin/ gem install pg
sudo ARCHFLAGS="-arch x86_64" gem install pg
It seems like it installs correctly but it still can't find the pg function. I installed postgresql with brew. My version of ruby is 2.0.0 and psql is 9.3.4.

It looks like your interpreter is running Ruby 2.0. It's possible that your gem installer is an pre-2.0 version and installing your gems to non-2.0 location. You can verify by running
gem -v
If this is the case check if your system has a gem2.0 binary that you can use like so:
sudo gem2.0 install pg

Related

`require': cannot load such file -- rack/handler (LoadError)

I'm not a Ruby person, so this may be a 101 question. I'm just trying to use a utility that happens to be written in Ruby.
I'm using tilemaker, a utility in the openstreetmap ecosystem. It creates tiles in the mbtiles format. The repository comes with a simple utility to serve the tiles on a browser to test the files you create. This utility is written in Ruby, and is what I'm having trouble with.
The repo's README has instructions for the server utility. The installation instructions read:
(If you don't already have them, you'll need to install Ruby and the required gems to run the demonstration server. On Ubuntu, for example, sudo apt install sqlite3 libsqlite3-dev ruby ruby-dev and then sudo gem install sqlite3 cgi glug rack.)
I'm on Debian 11 (on Qubes, so I don't mind running sudo gem install as they recommend). I hope this is close enough to Ubuntu but maybe this is related to the problem.
This is what I get:
$ ruby server.rb ~/countries-raster.mbtiles
Starting local server
Traceback (most recent call last):
3: from server.rb:22:in `<main>'
2: from server.rb:118:in `<class:MapServer>'
1: from /usr/lib/ruby/vendor_ruby/rubygems/core_ext/kernel_require.rb:85:in `require'
/usr/lib/ruby/vendor_ruby/rubygems/core_ext/kernel_require.rb:85:in `require': cannot load such file -- rack/handler (LoadError)
What am I missing here? Thanks.
It appears that the issue is that you are using a 3.X release of rack.
According to the CHANGELOG as of version 3.0 Rack::Handler was removed from rack and pulled out into its own gem (rackup).
To resolve the issue you will need to either use an older version of rack
gem 'rack', '~> 2.2'
Or you will need to add the rackup gem as a dependency:
gem 'rack'
gem 'rackup'
Either option will provide access to rack/handler:
rack (2.2.5) - Source
rackup (0.2.3) - Source
for a better setup under your user, this could be done:
ensure to have ruby running under your user by:
ruby -v # 2.7 or higher is better
then create a file besides your script server.rb called Gemfile:
# Gemfile
source "https://rubygems.org"
gem "rack"
gem "sqlite3"
gem "cgi"
gem "glug"
After that, ensure to have bundler installed (this is a gem to manage gem versioning), by:
gem install bundler
If you face a permission error, is because your ruby program is under root user. So you can use via sudo bundle install, but anything else must be run under sudo, or you can install and setup rvm which will install and configure ruby under your user.
After installing you can call:
bundle install
# will install all the gems needed and will lock the latest versions for you inside Gemfile.lock
run your server by:
bundle exec ruby server.rb
by running via bundle exec your telling to ruby to use the gems installed and versioned by the Gemfile.lock. This guarantees to your software to require the specific versions and avoid collisions or anything else.

jruby -S bundle install jruby: No such file or directory -- bundle (LoadError)

I have my ruby on rails application and I have installed jruby-1.7.4 in my Windows XP.
When I try to run jruby -S bundle install it gives me an error
jruby: No such file or directory -- bundle (LoadError)
I am really stack with this error..
Please help..
Bundler is probably not installed. You can check the output of jruby -S gem list to verify, and then install it with:
jruby -S gem install bundler
In general, a LoadError means you’re missing a gem dependency.

Error requiring pg under rvm with postgres.app

I'm using Postgres.app on OS X (10.8.3). I have modified my PATH so that the bin folder for the app is before all others.
Rammy:~ phrogz$ which pg_config
/Applications/Postgres.app/Contents/MacOS/bin/pg_config
I have rvm installed and can install the pg gem without error, but when I go to require it I get an error:
Rammy:~ phrogz$ gem -v
1.8.25
Rammy:~ phrogz$ gem install pg
Fetching: pg-0.15.1.gem (100%)
Building native extensions. This could take a while...
Successfully installed pg-0.15.1
1 gem installed
Rammy:~ phrogz$ ruby -v -e "require 'pg'"
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
/Users/phrogz/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': dlopen(/Users/phrogz/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.15.1/lib/pg_ext.bundle, 9): Library not loaded: #executable_path/../lib/libssl.1.0.0.dylib (LoadError)
Referenced from: /Applications/Postgres.app/Contents/MacOS/lib/libpq.dylib
Reason: image not found - /Users/phrogz/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.15.1/lib/pg_ext.bundle
from /Users/phrogz/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/phrogz/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.15.1/lib/pg.rb:4:in `<top (required)>'
from /Users/phrogz/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from /Users/phrogz/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from /Users/phrogz/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from -e:1:in `<main>'
What do I need to do to get the pg gem properly installed?
Edit: Even though this answer currently has more votes than the accepted answer, the accepted answer is far simpler and cleaner.
Remove the Postgres.app binaries from the path when installing the pg gem, and instead use the postgres install built into OS X to configure the gem. The pg library will still correctly connect to the Postgres.app server later on.
Rammy:~ phrogz$ gem uninstall pg
Successfully uninstalled pg-0.15.1
# Modify PATH to remove /Applications/Postgres.app/Contents/MacOS/bin
Rammy:~ phrogz$ gem install pg
Fetching: pg-0.15.1.gem (100%)
Building native extensions. This could take a while...
Successfully installed pg-0.15.1
1 gem installed
Rammy:~ phrogz$ ruby -v -e "require 'pg'"
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
I found a solution that works for me and requires minimal hacking/configuring. You only need to do this once and it will work for every bundle install. Add the following to your .bash_profile, .bash_rc, or equivalent:
export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Postgres.app/Contents/MacOS/lib:$DYLD_LIBRARY_PATH
(Assuming you installed Postgres.app in the default location). Then restart your terminal session and try again.
Exporting to DYLD_LIBRARY_PATH directly can cause serious problems with other apps that depend on it, but using the fallback path avoids these problems.
See also:
https://github.com/PostgresApp/PostgresApp/issues/109#issuecomment-18387546
Postgres.app upgrade, now Rails app won't start
EDIT: It seems that setting DYLD_FALLBACK_LIBRARY_PATH causes an error when you try to run psql. To fix this, you can add the following two lines to your .bash_profile:
alias psql="(. ~/.bash_profile; unset DYLD_FALLBACK_LIBRARY_PATH; psql)";
This is assuming that you're using bash and that your .bash_profile is located in your home directory. If that's not the case (or if you're using a .bashrc or other environment setup instead of .bash_profile) change the ~/.bash_profile part of the command to the path to your environment setup script.
The aliased commands basically start a subshell which does not effect your current bash environment. So when it unsets the DYLD_FALLBACK_LIBRARY_PATH variable, it's only temporary. After you exit psql the environment variable will be set again so that rails functions properly.
This worked for me:
sudo env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config
Just double check that the /Applications/Postgres93.app.. path exists for you.
The problem is in the linking of the gem to the Postgres.app.
If you link it to the postgres version that ships with osx
Here's a small script:
if using rvm with project gemsets: change into your project
run the following commands:
gem uninstall pg
PATH=${PATH/'Postgres.app'/'WRONGFOLDER.app'}
gem install pg
PATH=${PATH/'WRONGFOLDER.app'/'Postgres.app'}
Now everthing should be fine

Why does Nokogiri require only work in super-user mode?

I keep getting an error when I run a file called wc.rb. I type:
ruby wc.rb
but I keep getting this error:
/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- nokogiri (LoadError)
from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from wc.rb:3:in `<main>'
However, it works when I run the same script with:
sudo ruby wc.rb
Is there anything I can do so that I don't have to run this script as a super user?
When you run with sudo are you running the same version of Ruby? If sudo ruby -v returns something different than ruby -v, you may have only installed the Nokogiri gem in the Ruby 2.0 gem location, and not for the gem location associated with your system's default version of Ruby.
Update: If you originally installed the Nokogiri gem using sudo, you also may need to uninstall it first using sudo (sudo gem uninstall nokogiri) and re-install it without using sudo (gem install nokogiri) for the permissions to be correct.

Error while installing watir-webdriver on Ubuntu 11.10

After going step by step (installed both ruby and rubygems properly) to install watir-webdriver, I am facing following error:
ERROR: Error installing watir-webdriver:
ERROR: Failed to build gem native extension.
/usr/bin/ruby1.9.1 extconf.rb
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- mkmf (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from extconf.rb:4:in `<main>'
Please help me resolve this and do let me know what made this happened?
Not sure how you are installing ruby on your Ubuntu machine but may I recommend the Ruby Version Manager.
https://rvm.io/
this will allow you to install different versions of ruby on your machine and switch between them as well as gems etc.
here is a quick guide I wrote for installing watir webdriver in Ubuntu via RVM:
Ubuntu requires the use of a Ruby Version Manager. To do this we will need to first install both the Curl package and Git-core.
To do this use the following command:
sudo apt-get install curl git-core
Upon success we will need to download the Ruby Version Manager application. This application allows us to install multiple versions of ruby as well as switch between. It also provides a Gem manger. Recommended to Google to read up on the commands and features within RVM. To install RVM use the following:
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
We can also install packages within RVM and we will require the use of openSSL:
rvm pkg install openssl
Then installing Ruby itself:
rvm install 1.9.3-p125 --with-openssl-dir=$rvm_path/usr
Best to test this is all working with:
irb
When all clear we can install the Gem, we need to install it within the RVM with the follow:
rvm do gem install watir-webdriver
If installed correctly we should be able to use watir to drive firefox which is already installed on ubuntu.
I am not Linux/Ubuntu expert but the error message says you do not have build tools installed.

Resources