Automatically Run Command Upon Opening Terminal (Linux Mint 16) - ruby-on-rails

I am using Rails and for some reason unbeknownst to me I have to execute the following line every time I start the terminal in order to use ruby and rails.
source ~/.bash_profile
If I type the following before running the above command, it will recommend that I install the packages (ie "type 'sudo apt-get install package' to install")
rails -v && ruby -v && irb -v
Is there a way to make my terminal execute a command upon opening? Or, better yet, can anybody help explain the reason I have to run this line? I found an article that said I had to do that given my issue but it didn't explain why.

Assuming you're using the bash shell, then ~/.bash_profile will be sourced once (and only once) when you log in to the system but not each time you open a terminal window.
The ~/.bashrc, however, will be sourced every time a new shell is opened (i.e. when you open a terminal window).
So they are probably some environment variables settings in ~/.bash_profile which are required for ruby to run. Those settings should be moved to the ~/.bashrc file instead so they are defined in all shell instances.
Alternatively, source ~/.bash_profile from your ~/.bashrc (this is at your own risk, it may have side-effects).

Related

Twilio CLI setup; In autocomplete: What is "add the autocomplete env var to your bash profile and source it"?

I'm following the CLI setup for twilio and i get the following instruction from the command line on my mac (mojave) when i reach the autocomplete section of this document (https://www.twilio.com/docs/twilio-cli/quickstart). I do not know what i am being instructed to do here. Excuse my naivety.
1) Add the autocomplete env var to your bash profile and source it
$ printf "$(twilio autocomplete:script bash)" >> ~/.bashrc; source ~/.bashrc
NOTE: If your terminal starts as a login shell you may need to print the init script into ~/.bash_profile or ~/.profile.
2) Test it out, e.g.:
$ twilio <TAB><TAB> # Command completion
$ twilio command --<TAB><TAB> # Flag completion
Enjoy!
You can run the command:
printf "$(twilio autocomplete:script bash)" >> ~/.bashrc; source ~/.bashrc
from your OS-X terminal prompt, it will then append >> the output of that command to your ~/.bashrcfile. It then sources the contents of that file to populate your terminal environment (do you don't need to close your terminal and re-open it in this first instance).
From that point on, when you start a new Bash shell, you will be good to go (and don't need to run that command again).

Cant perform query in rails : may have been in progress in another thread when fork() was called

I am running under macOS Catalina version 10.15.1
When I run my rails console on my project and try to perform a query like User.first I get :
objc[57093]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called.
objc[57093]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.
I followed this answer and added OBJC_DISABLE_INITIALIZE_FORK_SAFETY to my .zshrcfile which looks like that :
ZSH=$HOME/.oh-my-zsh
# You can change the theme with another one:
# https://github.com/robbyrussell/oh-my-zsh/wiki/themes
ZSH_THEME="robbyrussell"
# Useful oh-my-zsh plugins for Le Wagon bootcamps
plugins=(git gitfast zsh-autosuggestions last-working-dir zsh-syntax-highlighting common-aliases history-substring-search)
# Prevent Homebrew from reporting - https://github.com/Homebrew/brew/blob/master/share/doc/homebrew/Analytics.md
export HOMEBREW_NO_ANALYTICS=1
# Actually load Oh-My-Zsh
source "${ZSH}/oh-my-zsh.sh"
unalias rm # No interactive rm by default (brought by plugins/common-aliases)
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
# Load rbenv if installed (To manage your Ruby versions)
export PATH="${HOME}/.rbenv/bin:${PATH}"
type -a rbenv > /dev/null && eval "$(rbenv init -)"
# Load nvm if installed (To manage your Node versions)
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"
# Anaconda binaries (python, pip, conda, jupyter, pytest, pylint etc.)
export PATH="/anaconda3/bin:${HOME}/anaconda3/bin:${PATH}"
# Rails and Ruby uses the local `bin` folder to store binstubs.
# So instead of running `bin/rails` like the doc says, just run `rails`
# Same for `./node_modules/.bin` and nodejs
export PATH="./bin:./node_modules/.bin:${PATH}:/usr/local/sbin"
# Store your own aliases in the ~/.aliases file and load the here.
[[ -f "$HOME/.aliases" ]] && source "$HOME/.aliases"
# Encoding stuff for the terminal
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export EDITOR=atom
When I run in my terminal echo $OBJC_DISABLE_INITIALIZE_FORK_SAFETY I get YES, so I think the environment variable is correcly set... but that doesnt fix the issue.
How can I fix this problem ?
What worked for me, was disbaling Spring: export DISABLE_SPRING=true before starting your Rails console or server
I ran into this problem after I updated my Postgres database. I ended up updating to the latest ruby version and reinstalling all gems. After that everything worked again.
I tried the OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES suggestion as well, to no avail.

Could not login with bash shell by default

I want to run a Ruby on Rails application. When I tried to run it, it shows me this,
The program 'rails' is currently not installed. You can install it by typing:
sudo apt install ruby-railties
So, I figured out the problem and I found that the problem is due to not login into bash shell. My terminal could not execute 'ruby' or 'ruby on rails' scripts. I checked .bashrc and .bash_profile files if PATH variable is set to point to rvm file.
When I did,
/bash/bin -l
it shows me ruby or rails are installed on system and I could start Rails server successfully. But if I opened another Terminal window, same problem occurs. Basically, I want to log into bash shell by default. Please correct and help me to sort out this. Thanks!
If you are sure the location of your bash shell is /bin/bash you could use this command (replacing "username" with your username):
chsh -s /bin/bash username
That will change your default shell in most unix like operating systems.
Afterwards you can verify it checking /etc/passwd where you will see the default shell at the end of the line of your username.
Warning: Try it first with a new user, in order to avoid losing your shell access if the path to bash is different :-)

Rubymine 6.3.2 doesn't find environment variables on Ubuntu 13.10

I'm trying to execute a bundle install via RubyMine but I'm receiving the following error message:
Error Message:
Set the environment variable ORACLE_HOME if Oracle Full Client.
Append the path of Oracle client libraries to LD_LIBRARY_PATH if Oracle Instant Client.
I'm using Oracle Instant Client and my ~/.bash_profile contains this:
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
export LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client64/lib
I have no issues when I try bundle install via terminal. I suspect that Rubymine is not loading the same environment variables that the terminal app loads when it starts.
run RubyMine, go to Tools -> Create Command-Line Launcher, it should create a /usr/local/bin/mine file.
Edit your RubyMine launcher in Ubuntu, and make it execute bash -ic '/usr/local/bin/mine' (the -i is for an interactive shell)
That will launch RubyMine after loading your profile files (.profile, .bashrc ...)
Sounds like you're running RubyMine from your desktop manager and none of the parents of the desktop manager run your ~/.bash_profile. Fixing that would be an Ubuntu question.
But you could
find the RubyMine executable on disk and run RubyMine by typing the full path of the executable in a terminal (and you could make an alias), or
run RubyMine, go to Tools -> Create Command-Line Launcher, and use the launcher that it creates.

Unable to run ruby script over ssh into ec2 instance

I'm working with ec2 instances and was trying to execute a ruby script on another instance after ssh to that instance.
I have a ruby script which updates configuration files, so i need to run that script as super user. when i run the script manually on that instance, sudo ruby recreate-532d01c.rb, the error that comes is
sudo: ruby: command not found
Running simple scripts with no root permissions works, eg.ruby file_1.rb.
Using rvmsudo in place of sudo executes the script with warning,
ubuntu#ip-10-0-0-111:~$ rvmsudo ruby recreate-82bb000012.rb
Warning: can not check `/etc/sudoers` for `secure_path`, falling back to call via `/usr/bin/env`, this breaks rules from `/etc/sudoers`. Run:
export rvmsudo_secure_path=1
to avoid the warning, put it in shell initialization file to make it persistent.
In case there is no `secure_path` in `/etc/sudoers`. Run:
export rvmsudo_secure_path=0
to avoid the warning, put it in shell initialization file to make it persistent.
I tried to execute the below command from rails console of one of the instance to test and it fails to recognize ruby as command
1.9.3-p545 :002 > system("ssh -i /home/ubuntu/.ssh/own_key.pem ubuntu#**.***.***.** ruby execute-52d.rb")
bash: ruby: command not found
I tried with possible solutions over web, but could not resolve the issue. I have the same configuration running for one of my old aws acount, this is a newly created account. Not sure if this could be issue in any way as currently ec2 instances fall under vpc by default and have some changes after dec 2013
Nothing to do with your VPC. So when you run your ruby script with sudo your environment that your user is using doesn't get set for Ruby.
Sounds like you may be using rvm and you probably set it up with a 'single user' config.
Try running as your user:
which ruby
and see where your ruby executable is located at. That's what you have to make sure that when your run your script as sudo it's available in the PATH.
Worst case you would have to reinstall rvm with multiuser config which should work when you run with sudo:
user$ \curl -sSL https://get.rvm.io | sudo bash -s stable

Resources