rake install does not work for octopress - ruby

I am trying to setup rake via this doc.
http://octopress.org/docs/setup/
But I get some errors.
ikhthiandor#ikhthiandor-Satellite-L450:/opt/octopress$ rake install
## Copying classic theme into ./source and ./sass
mkdir -p source
rake aborted!
Permission denied - source
Tasks: TOP => install
(See full trace by running task with --trace)
With sudo I get this output.
ikhthiandor#ikhthiandor-Satellite-L450:/opt/octopress$ sudo rake install
rake aborted!
no such file to load -- bundler/setup
(See full trace by running task with --trace)
Here is a list of files in the directory.
ikhthiandor#ikhthiandor-Satellite-L450:/opt/octopress$ ls -a
. config.ru .git Rakefile .slugignore
.. _config.yml .gitignore .rbenv-version .themes
CHANGELOG.markdown Gemfile plugins README.markdown
config.rb Gemfile.lock .powrc .rvmrc
How can I solve the problem?

Ikhthiandor: Looks like you are a beginner in the ruby/rails world.
By running the rake install command, you are installing the default octopress theme using the rake tool. Not trying to setup rake as you mention in the question.
The first error ( Permission denied - source when trying mkdir -p source - as you correctly guessed - is because the user doesn't have permissions to create that directory.
The second error ( no such file to load -- bundler/setup ) is because the previous install dependencies steps are not performed correctly (for the user running this command).
The install dependencies steps to be completed successfully are:
1. gem install bundler
2. rbenv rehash # If you use rbenv, rehash to be able to run the bundle command
3. bundle install
I am guessing you ran these steps successfully as 'ikhthiandor' user, so the bundler gem is not available for the 'sudo' user.
You can fix this by either of the following options:
changing permissions on /opt/octopress folder so that 'ikhthiandor' user has rights to create sub-folders/files within.
Run all the commands in the Octopress Setup doc as 'sudo'
Best practice would be to use either rvm or rbenv to manage custom installations of ruby environments per user (and not do everything as super user).
If you are indeed a fresher in the ruby-rails world, and want to step up your knowledge of the tools and the best practices in the ruby/rails world, I suggest browsing through the first few chapters of the Ruby on Rails Tutorial book that is available for free on-line.
HTH

Related

Can't install gem using Bundler's Rakefile install task when developing a custom gem

I'm developing a couple of private gems and I think I don't understand correctly the PATH/GEM_PATH and/or Bundler/RVM installation flow, would love if someone could chip in.
I have a repository with two gems (A & B for simplicity sake). I've developed the gems using the scaffolding + following the guidelines provided by this bundler tutorial.
Thanks to the Bundler project I have a few Rakefile tasks like rake build, rake install, rake install:local and rake release. Because of the private nature of these gems I can't release them to RubyGems (and we haven't looked into hosting our rubygems).
My machines are using RVM to manage ruby versions and Bundler version 1.15.1
What I want to do: Assuming a new machine/developer trying out the project, ideally we would cd into each of the subfolders (currently 2, gem A and gem B), run rake install and after that we should have the gems available system wide for the current user.
What is happening: The gems are built and work properly, but they are only available inside the subfolder of each gem i.e. gem A is only available inside the subfolder A and gem B is only available inside subfolder B.
What I've tried: So, after rake build/install/install:local a new .gem file is generated under pkg. I've tried to manually install the "compiled" file using gem install pkg/A.gem, gem install --local pkg/A.gem and gem install --local --user-install pkg/A.gem without success. (there are plenty of SO questions/answers about this)
I believe this has something to do with the PATH variables, but like I said before I don't fully understand the way they are managed. I get the following results from these commands:
# Our gem
> gem which A
/home/ubuntu/.rvm/gems/ruby-2.4.0/gems/A-0.1.8/lib/A.rb
# Pry, available globally
> gem which pry
/home/ubuntu/.rvm/gems/ruby-2.4.0/gems/pry-0.11.1/lib/pry.rb
I've been lost and frustrated for far too long now, any help is appreciated. Also open to hear suggestions of better private gem installation flows :)
Yes, it has something to do with your PATH variables. Your installation seems to be good.
I advise you to first affirm your gems installation path with:
echo $GEM_HOME
The double check your PATH to ensure its present and also confirm that the GEM home is also where the gem got installed into from the rake install
echo $PATH
If not, put it in your path and you should be fine with something like this:
echo PATH=$PATH:$GEM_HOME >> ~/.bashrc
source ~/.bashrc
Build your gem as per that guide you linked. You should end up with a gem file. Distribute this as you see fit (I use rsync/crontab to download newer gem versions but anything goes). User can install the gem as follows:
gem install --user-install /path/to/your/file.gem
This will install the gem in the user's ~/.gem/ruby/<version>/gems/<your-gem-name> directory.
Tried it with an empty gem (foodie, as in that example guide) and it works fine. But if you don't specify the --user-install parameter it will try to install in the system ruby dir (/usr/lib/ruby/gems...)

RVM: Could not locate Gemfile or .bundle/ directory when running $bundle install command

I'm trying to setup RVM on my mac. When I run bundle install in an empty folder (a dummy project folder) I get the following error:
Could not locate Gemfile or .bundle/ directory
Questions:
Do I need to copy any gem files to the dummy project folder? If so, what gems, and where from?
Do I need to install anything else?
You Just need to check whether you are currently in the rails app folder (In most cases) or double check whether you have created the rails app properly.
Here I am trying to execute the command for starting the rails server,
bundle exec rails server
As you can see since I'm not in that folder it cant locate the required gem to start up the service.
And an Error pops up like below
Could not locate Gemfile or .bundle/ directory
To remove just cd into the app folder
cd YourAPPfolder
And run the required rails command again.
You are done. Voila !!
Make sure you are in the project directory before running bundle install. For example, after running rails new myapp, you will want to cd myapp before running bundle install

Ruby gem equivalent of "pip install -e"?

In Python I can install a package from source in "editable" mode using pip install -e. Then I can carry on editing the code, and any changes will be automatically picked by other Python scripts that import library
Is there a comparable workflow for developing Ruby gems? What is the "Ruby way" of using libs as they are being developed rather than, for example, compiling and installing a gem every time I make a change to the source?
There are two common approaches one might use with bundler:
one executes bundle install --path vendor/bundle and does not run bundle update unless everything is tested.
one tells a bundler to use a local version of the gem:
in Gemfile (this is not supported in mymaingem.gemspec due to rubygems maintainence issues): gem 'mycutegem', :git => 'git://github.com/myname/mycutegem', :branch => 'master';
in command line: bundle config local.mycutegem /path_to_local_git/mycutegem.
The first approach will download everything into subfolder of your current project (here it’d be vendor/bundle.) Feel free to modify everything there, it’ll be reflected.
The second approach is likely better. You are to clone the gem from github and instruct bundle to use your local clone of the respective git repository. This approach provides you with an ability to publish the changes to your main gem into the repository. As soon as dependent repo is published as well, the up-to-date version will be retrieven by your gem subscribers, assuming they have not instructed their bundlers to use their locals.
Hope this helps.
Let's assume you have your gem code residing in a folder (say my_project/mygem/lib). You have some Ruby code in my_project that you want using the mygem code.
What I'd do is add mygem/lib to the $LOAD_PATH global variable in the beginning of said files. Kinda like this:
$LOAD_PATH << File.expand_path('lib', './mygem') # Resolve global paths
require 'a_file' # Would require "mygem/lib/a_file.rb"
I am not sure if this is exactly what you want to achieve, but from the description I infer that you want to have a local copy of some gem and reference that gem in your project.
If this is the case, you can (usually) achieve it in two steps:
Clone the gem from VCS (in most cases: git), e.g.
git clone url-of-the-gem-repo
Reference the local copy using Bundler :path, e.g.
gem "some-gem", :path => "/path/to/local/copy"
If the gem is stored at github, an even better way is to first fork it at github and then clone your own copy. Then, if you provide any improvements to the code in the local repo, you can easily share it with the world using a pull request.
I realize this is a 5 year old question, but I found all of these answers unsatisfying. Since I use ruby to develop CLI tools, using bundler for testing is not ideal. I need to be able to execute my test commands anywhere, and get the actual equivalent of pip install --editable.
Here's my solution.
Use RVM to setup a new gemset for development
rvm gemset use tools-dev --create
Install your gems
gem install mygem.gem
Locate the installation directory
gem list tool -d
In the output, it will say installed at ${rvm_gemset_path}. Copy that path. It will also be an environment variable GEM_HOME, but I include this step for completeness and clarity.
Delete the installed copy
rm -Rf ${rvm_gemset_path}/gems/mygem-${version}
Create a symlink to the git repository working directory.
ln -s ${PWD} ${rvm_gemset_path}/gems/mygem-${version}
This bash script will do it in a more automated fashion assuming that you are in the git directory and you've set GEM_NAME as your gem's name.
rvm gemset use --create ${GEM_NAME}-dev
gem build ${GEM_NAME}.gemspec
gem install ${GEM_NAME}*.gem
gem_install_path=$(ruby -e "puts Gem::Specification.find_by_name('${GEM_NAME}').full_gem_path")
mv $gem_install_path "${gem_install_path}.bak"
ln -s $PWD $gem_install_path

How to contribute to a Ruby Gem

I am trying to contribute to a Ruby gem and I don't understand how to test a local gem without using a globally installed gem.
The gem I want to contribute to is a command line interface gem. I clone the gem into a directory then cd into that directory. However, when I run commands in the terminal when I'm in the cloned project directory it still uses the global gem. I've even run
gem uninstall gemname
then while inside the newly cloned gem directory I redo
gem install gemname.
No matter what changes I make to the gem, I can't see the results or what my contributions are doing because it's always running the global gem.
When I do try to type a command line command that is supposed to interact with the gem while in the cloned gem directory I get:
-bash: ~/.gem/ruby/2.1.0/bin/githubrepo: No such file or directory
I've done a ton of research but I'm just not getting it. Help?
gem install gemname will look for a .gem file in the current directory. If not found it will look for it on the web.
gem install --local /path/to/your/gemname.gem will allow you to target a particular directory. You may need to gem build gemname.gemspec first, so it has your changes.
Instead of doing this, I would write tests in the gem directory itself. It's likely that when running code in there, you can simply require 'gemname' in Ruby to get the gem functionality.
If it's a well-written gem, it should have tests already. They will most likely be in a directory called test or spec. Have a look at these tests and try to carry on in that style to test your changes. This will make your code changes far far more likely to be accepted as a pull request.

Deploying a Ruby Command Line Application that Depends on Non-Rubygems-Hosted Gems

I am attempting to deploy a small command-line application written in Ruby. Many of the gems that the application depends on are hosted on my github account. I have specified their locations in the Gemfile appropriately. When I clone the repo on the deployment machine and run bundle install from the root, all goes smoothly. I can then run the command line app (named "hippo") with bin/hippo. I would like to install the app to the system so that I do not need to patch $PATH or specify the path to the executable in order to use it.
When I run gem install /path/to/my/.gem/file, installation fails and says that it cannot resolve the dependencies to my personal gems hosted on my Github account. I gather this is because the gem install command does not read the Gemfile, only the gemspec (why on earth did the bundler people name their file Gemfile instead of BundleFile or something?), and the locations of the gems are specified in the Gemfile (there is no way to point to a github gem in a gemspec, right?). I have the same issue when I use the rake install task that comes with a bundle-scaffolded gem. However, when I run bundle exec gem install /path/to/my/gemfile then it installs OK, I guess because it sees my gems on the bundler-altered load path and decides that they are already installed. But then, when I try to run the executable, it fails because it can't find the dependencies when they are required. (Note that the executable has require bundler/setup as its first line after the shebang).
So I ask: what is a good way to deploy, with bundler, a command line application that depends on non-rubygems-hosted gems?
bundle exec sets up your load path correctly so that the gems are visible when you require bundler/setup. One option would be to always run your binary with bundle exec bin/hippo.
Something more suited to your purposes might be to run bundle install --standalone, which generates a bundle/bundler/setup.rb file within your project's directory. That file sets up load paths correctly for you, so you'd just need to do something like require_relative '../bundle/bundler/setup' from your binary instead of require 'bundler/setup'.

Resources