Solucionado (ver solução)
Solucionado
(ver solução)
8
respostas

(Errno::EPERM) instalação da gem 'pg'

Faz dias que estou com esse problema e pesquisei de tudo mas não consigo resolver. Achei que o problema no início fosse a Gemfile ou outros arquivos estarem privados, então fiz sudo chmod 777 nela, além das outras tentativas abaixo:

No início estava faltando a 'libpq-fe.h e fiz:

brew install libpqxx

Depois rodei isso e o bundle install parecia ter funcionado, mas ainda dá erro no push master no Heroku.

Brunas-MacBook-Pro:website ramona$ sudo gem install -n /usr/local/bin gem pg
Password:
ERROR:  Could not find a valid gem 'gem' (>= 0) in any repository
ERROR:  Possible alternatives: gem
Building native extensions. This could take a while...
Successfully installed pg-1.1.4
Parsing documentation for pg-1.1.4
Installing ri documentation for pg-1.1.4
Done installing documentation for pg after 1 seconds
1 gem installed

E ainda ao tentar instalar a gem:

Brunas-MacBook-Pro:website ramona$ gem install pg
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied @ rb_sysopen - /Users/ramona/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.1.4/.gemtest
Brunas-MacBook-Pro:website ramona$ sudo chmod 777 /Users/ramona/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.1.4/.gemtest
Password:
Brunas-MacBook-Pro:website ramona$ gem install pg
ERROR:  While executing gem ... (Errno::EPERM)
    Operation not permitted @ apply2files - /Users/ramona/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.1.4/.gemtest

Desinstalei e reinstalei o PostgreSQL mas o problema continua depois.

Brunas-MacBook-Pro:website ramona$ brew uninstall postgresql


Error: Refusing to uninstall /usr/local/Cellar/postgresql/11.2_1
because it is required by libpqxx, which is currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies postgresql
Brunas-MacBook-Pro:website ramona$ brew uninstall --ignore-dependencies postgresql
Uninstalling /usr/local/Cellar/postgresql/11.2_1... (3,186 files, 35.3MB)
Brunas-MacBook-Pro:website ramona$ brew update


Already up-to-date.
Brunas-MacBook-Pro:website ramona$ 
Brunas-MacBook-Pro:website ramona$ 
Brunas-MacBook-Pro:website ramona$ brew install postgresql
...
==> Summary
  /usr/local/Cellar/postgresql/11.2_1: 3,186 files, 35.3MB

O que aparece quando dou push master:

Brunas-MacBook-Pro:website ramona$ git push heroku master
Enumerating objects: 135, done.
Counting objects: 100% (135/135), done.
Delta compression using up to 4 threads
Compressing objects: 100% (122/122), done.
Writing objects: 100% (135/135), 31.85 KiB | 931.00 KiB/s, done.
Total 135 (delta 9), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote:  !     Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
remote:             Detected buildpacks: Ruby,Node.js
remote:             See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.5.1
remote: -----> Installing dependencies using bundler 2.0.1
remote:        Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote:        You are trying to install in deployment mode after changing
remote:        your Gemfile. Run `bundle install` elsewhere and add the
remote:        updated Gemfile.lock to version control.
remote:        
remote:        The dependencies in your gemfile changed
remote:        
remote:        You have added to the Gemfile:
remote:        * pg (>= 1.1.4, ~> 1.1)
remote: 
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !    Push rejected to metalisthelaw.
remote: 
To https://git.heroku.com/metalisthelaw.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/metalisthelaw.git'
Brunas-MacBook-Pro:website ramona$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   Gemfile.lock

no changes added to commit (use "git add" and/or "git commit -a")
Brunas-MacBook-Pro:website ramona$ gem install pg
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied @ rb_sysopen - /Users/ramona/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.1.4/.gemtest
8 respostas

Código da Gemfile:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.1'

gem 'rails-i18n', '~> 5.1'
gem 'twitter-bootstrap-rails'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Use sqlite3 as the database for Active Record
  gem "sqlite3", "~> 1.3.6"
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15', '< 4.0'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

group :production do
  gem 'pg', '~> 1.1', '>= 1.1.4'
end

Olá Bruna!

Não sou especialista em Rails mas estou disponível para te ajudar!

Vou listar algumas coisas que podem ser a causa do problema:

1 - Atualize suas RubyGems e tente novamente:

gem update --system

2 - Tenta rodar esse comando com o sudo:

sudo gem install pg

3 - Para instalar o libpqxx

sudo apt-get update
sudo apt-get install libpqxx-dev

3 - Vi que essa dependência:

gem 'pg', '~> 1.1', '>= 1.1.4'

Está no ambiente de produção né? Será que se você colocar num contexto geral ele funcione?

4 - Tem um trecho do seu código que está assim:

sudo gem install -n /usr/local/bin gem pg

Pra mim é estranho ver dois "gems" no mesmo comando, isto está correto? Ele está tentando instalar uma gem com o nome "gem".

Veja se alguma das coisas que escrevi fazem sentido. Espero que dê certo! ^^

Aguardo seu retorno!

Fiz 'gem update --system' e o 'sudo gem install pg' funcionou tranquilo, mas ainda recebo o mesmo erro ao tentar dar push no Heroku.

Já o 'sudo apt-get update' não funciona:

 Brunas-MacBook-Pro:website ramona$ sudo apt-get update
sudo: apt-get: command not found

Coloquei a gem pg em contexto geral também e nada pareceu mudar. E sobre o 'sudo gem install -n /usr/local/bin gem pg' você tem razão. Encontrei o comando no Stackoverflow e coloquei assim no terminal, e apesar de estar errado ele contorna e instala a gem. Então aparentemente eu tenho a gem instalada, o bundle install funciona, mas por alguma razão o Heroku ainda mostra esse erro:

remote:        The dependencies in your gemfile changed
remote:        
remote:        You have added to the Gemfile:
remote:        * pg (>= 1.1.4, ~> 1.1)
remote:        Bundler Output: You are trying to install in deployment mode after changing
remote:        your Gemfile. Run `bundle install` elsewhere and add the
remote:        updated Gemfile.lock to version control.
remote:        
remote:        The dependencies in your gemfile changed
remote:        
remote:        You have added to the Gemfile:
remote:        * pg (>= 1.1.4, ~> 1.1)
remote: 
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !    Push rejected to metalisthelaw.
remote: 
To https://git.heroku.com/metalisthelaw.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/metalisthelaw.git'

E se dou 'gem install pg':

Brunas-MacBook-Pro:website ramona$ gem install pg
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied @ rb_sysopen - /Users/ramona/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.1.4/.gemtest

Enfim, vou continuar tentando com as suas dicas também. Obrigada pela ajuda!

Olá Bruna, tudo bem?

Bora descobrir o que está acontecendo. Na sua máquina local, rodando rails server seu projeto está funcionando certinho?

: )

Sim, funciona

solução!

Certo, então o problema esta com a versão do pg no heroku.

Altera por gentileza, a versão do pg no ambiente de produção, deixando assim:

group :production do
  gem 'pg'
end

Em seguida, siga o passos deste link, para fazer o deploy.

Nos fale se deu certo.

:)

Funcionoooou! Não entendo por que já que eu tinha alterado para apenas 'gem pg' na production e tentado fazer o deploy, talvez ainda tivesse alguma outra coisa dando bug, mas estou feliz que finalmente foi resolvido. Muito obrigada! :)

Excelente noticia Bruna!

Conte sempre conosco e bons estudos!

:)

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software