Jekyll: command not found problem despite installated with most recent versions

Viewed 1057

I am having trouble with the Jekyll installation. Everything is up to date with Ruby, Homebrew and Gems:

Quocs-MacBook-Air:~ quochoantrinh$ brew -v
Homebrew 2.1.6
Homebrew/homebrew-core (git revision 2796; last commit 2019-07-01)
Quocs-MacBook-Air:~ quochoantrinh$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin16]
Quocs-MacBook-Air:~ quochoantrinh$ gem -v
3.0.3

Then I ran the following from the Jekyll on macOS setup from the official website (https://jekyllrb.com/docs/installation/macos/)

export PATH=/usr/local/opt/ruby/bin:$PATH

Afterwards I did the next commands:

gem install --user-install bundler jekyll
export PATH=$HOME/.gem/ruby/2.6.3/bin:$PATH

However when I run jekyll in my terminal it gives me the "command not found".

This is my gem env

Quocs-MacBook-Air:~ quochoantrinh$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 3.0.3
  - RUBY VERSION: 2.6.3 (2019-04-16 patchlevel 62) [x86_64-darwin16]
  - INSTALLATION DIRECTORY: /Users/quochoantrinh/.rvm/gems/ruby-2.6.3
  - USER INSTALLATION DIRECTORY: /Users/quochoantrinh/.gem/ruby/2.6.0
  - RUBY EXECUTABLE: /usr/local/opt/ruby/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /Users/quochoantrinh/.rvm/gems/ruby-2.6.3/bin
  - SPEC CACHE DIRECTORY: /Users/quochoantrinh/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /usr/local/Cellar/ruby/2.6.3/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-16
  - GEM PATHS:
     - /Users/quochoantrinh/.rvm/gems/ruby-2.6.3
     - /Users/quochoantrinh/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /usr/local/opt/ruby/bin
     - /Users/quochoantrinh/.yarn/bin
     - /Users/quochoantrinh/.config/yarn/global/node_modules/.bin
     - /Users/quochoantrinh/anaconda3/bin
     - /Users/quochoantrinh/anaconda3/bin
     - /Users/quochoantrinh/anaconda/bin
     - /Users/quochoantrinh/.gem/ruby/2.6.3/bin
     - /Users/quochoantrinh/.rvm/gems/ruby-2.6.3/bin
     - /Users/quochoantrinh/.rvm/gems/ruby-2.6.3@global/bin
     - /Users/quochoantrinh/.rvm/rubies/ruby-2.6.3/bin
     - /Users/quochoantrinh/.yarn/bin
     - /Users/quochoantrinh/.config/yarn/global/node_modules/.bin
     - /Users/quochoantrinh/anaconda3/bin
     - /Users/quochoantrinh/anaconda3/bin
     - /Users/quochoantrinh/anaconda/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /Library/Frameworks/Mono.framework/Versions/Current/Commands
     - /Users/quochoantrinh/.rvm/bin

1 Answers

There are a few things going on here, but to fix your immediate problem, I believe this should do it:

export PATH="$HOME/.gem/ruby/2.6.0/bin:$PATH"

When you use the --user-install flag, it installs gems in the USER_INSTALLATION_DIRECTORY that's mentioned when you run gem env, which in your case is /Users/quochoantrinh/.gem/ruby/2.6.0, and then you have to add /bin when you add it to the PATH. It's not so much that you want to change the USER_INSTALLATION_DIRECTORY, but that you need to set the PATH to point to the USER_INSTALLATION_DIRECTORY. I know it's not very intuitive because you would think that you want to use the path that corresponds to your current Ruby version (2.6.3), but the USER_INSTALLATION_DIRECTORY always ends with a 0. It only uses the first 2 digits of the Ruby version. If you ever upgrade to 2.7.2, which is the latest version as of today, your PATH has to point to 2.7.0.

Note that running the export PATH command above will only work during your current session. If you want to be able to use jekyll every time you quit and relaunch Terminal, or open a new tab, you need to add that export PATH command to your shell startup file, typically either ~/.bash_profile or ~/.zshrc. If you're not sure which shell you are using, read this: https://www.moncefbelyamani.com/which-shell-am-i-using-how-can-i-switch/

I also noticed that you seem to have installed Ruby with both Homebrew and RVM. This could potentially cause problems. I always recommend that people install Ruby with a Ruby manager (such as RVM, rbenv, and chruby), because most of them automatically set everything up for you. You don't have to worry about setting the PATH manually and thinking about whether to add --user-install. My preferred manager is chruby because it's the lightest and easiest to use.

Based on your gem env, it looks like you are currently using the Homebrew version of Ruby, so I wanted to let you know of another issue you might run into. (I can tell it's Homebrew by looking at the RUBY_EXECUTABLE, and I know from experience that Homebrew installs things in /usr/local and I recognize that path as the Homebrew Ruby.)

Let's say you want to install rails months from now, and you type gem install rails (without the --user-install flag). Once it's installed, if you try to use the rails command, you will get "command not found", because when you use the Homebrew version of Ruby, and you don't use --user-install, the gem will get installed in a directory managed by Homebrew, not in your USER_INSTALLATION_DIRECTORY. The Jekyll documentation forgot to point out that you need to add the Homebrew gems location to your PATH. The fix is covered in my definitive guide to installing Ruby gems on a Mac, and also in the pull request I opened to fix the Jekyll documentation.

To understand more about how PATH works, and why you can get a "command not found" error, I recommend this article: https://www.moncefbelyamani.com/troubleshooting-command-not-found-in-the-terminal/

Related