Adding homebrew to $PATH without sudo privileges - path

I've been trying to install homebrew without admin privileges on my school MacBook (Mojave 10.14.6) for the past 2 weeks. I've seen many things on the internet, saw some scripts, tutorials, but nothing seems to help.
I used this script (did some tweaking to it like changing to a new repository and setting absolute path)
https://gist.github.com/skyl/36563a5be809e54dc139
the repository I used is
https://github.com/Homebrew/brew
my path to brew is
/Users/as.gurban-zade/Desktop/homebrew/usr/local/bin/brew
I added this to the $PATH using
echo "export PATH=/Users/as.gurban-zade/Desktop/homebrew/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
also tried
echo "export PATH=/Users/as.gurban-zade/Desktop/homebrew/usr/local/:$PATH" >> ~/.bash_profile && source ~/.bash_profile
After everything I did, I still get
-bash: brew: command not found
Updating OS to Catalina is possible if that would help

Related

'Zsh: command not found: valet' - unable to install Valet

I just tried installing valet for a very long time, - and I couldn't find any help anywhere. No matter what I tried, then it kept saying:
Zsh: command not found: valet
I'm running Zshell (instead of Bash) and OSX. I've had Brew installed for quite a while.
I'll answer this myself, - so hopefully people in the same situation can find this in the future.
I had a very similar problem under Zsh, but my solution was easier. Rather than adding ~/.composer/vendor/bin to my $PATH, I needed to use the full directory name; i.e., /Users/[your-user-name]/.composer/vendor/bin.
Everywhere it says to 'Check that your path is right'. And yup - if you haven't done that, then you should start there. The easiest way is to go to your terminal and write:
echo $PATH
... And then you should see ~/.composer/vendor/bin in between two colons in there. If that isn't there, then you should go to your ~/.bashrc-file (or ~/.zshrc-file) and add this line:
export $PATH=~/.composer/vendor/bin:$PATH
And then it may be fixed.
My problem had deeper roots, though. Even though my path was right, then I still got the error:
Zsh: command not found: valet
And if I wrote: which valet - then it (obviously) just responded with valet not found.
Removal of Composer
What fixed it was to remove all my composer-installations. Composer can be located in several different locations (and installed by brew). So in order to remove it, then do this (inspired by this post, but it lacks a couple of steps):
Remove your composer- or -composer.phar-file. You can find them by running which composer and/or which composer.phar.
Remove your .composer-folder (usually located here: ~/.composer).
Then make sure that there isn't an installation done with Homebrew (this was what I think made the collision for me). You do it by running brew unlink composer followed by brew remove composer.
Then, - if you want to be 100% sure that it's all gone, then go to your root (cd /) and run this command: find ./* -name 'composer.phar' and this find ./* -name 'composer'. That should tell you of all the locations where the Composer-installation can be installed.
When all that's removed, then you should be rid of Composer (entirely).
... Then install it again, - and see if you can get valet to work (it did for me).
The solution was adding composer bin to the path. You can use the following commands
From the terminal
export PATH="$PATH:$HOME/.composer/vendor/bin"
echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bash_profile
source ~/.bash_profile
valet install
Issue Ubuntu: no command valet
Valet on Ubuntu:
sudo apt install libnss3-tools jq xsel
composer global require cpriego/valet-linux
.composer/vendor/cpriego/valet-linux/valet install
Now command valet should work, but if still not:
sudo cp .composer/vendor/cpriego/valet-linux/valet /usr/local/bin/

Modularizing and distributing bash script via Homebrew

Context
I have some functions defined in my ~/.bashrc which I'd like to turn into a Homebrew package. Currently, these functions act as custom commands on my command line:
# .bashrc
function foo() {
# do something interesting
}
# at terminal
$ foo
# => does the interesting thing
Approach
I've created a homebrew formula using brew create. My current approach is as follows:
Move the function definitions into a separate file, script, within a directory, brew-script
Make brew-script downloadable as a tarball, brew-script.tar.gz
Have my brew formula append text to the end of ~/.bash_profile to include script when terminal session starts
Concerns
Is modifying .bash_profile in a brew formula bad practice? (eg. when uninstalling with brew uninstall script, brew should somehow remove the text that was appended to .bash_profile... Parsing .bash_profile doesn't seem very fun.)
Is there a convention already established for including functions in bash scripts so that they are available from the command line?
Is it common to simply ask the user to add some text to their .bash_profile or .bashrc?
Desired result
Should be able to install cleanly with brew and then run foo as a command:
$ brew install script
$ foo
# => does the interesting thing
(Assume the brew formula is already installed locally. I'll worry about auditing and pushing the formula to homebrew later)
Refer https://github.com/Homebrew/homebrew/issues/50232 and https://github.com/Homebrew/homebrew/issues/50231.
I have a script that safely‡ modifies ~/.bash_profile as part of a homebrew install process. https://github.com/paul-hammant/homebrew-tap/blob/master/switchjdk.rb
‡ allegedly
Without using homebrew:
to put your bash scripts in some file such as bashrc or any other name works, then put the following line:
source "path/to/brew-script/script"
somewhere in your bash profile.
Then you just have to make sure you refresh or reload your bash profile by running . ~/.bash_profile or source ~/.bash_profile.
How homebrew installs work:
When you installed homebrew it added a line to your bash_profile that modifies your $PATH variable to include the path to the homebrew install repo, so that whenever brew installs something it becomes findable through your PATH.
If you use brew create you must have your script uploaded somewhere on the internet, because the argument brew install takes is a URL. I.e if I create my script at my_bash_function.tar.gz then I would do
brew create http://web.mit.edu/dianah13/www/my_bash_function.tar.gz
It also templates a pull request to include your package in homebrew's main repo.

Automatically Run Command Upon Opening Terminal (Linux Mint 16)

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).

How do you migrate a Homebrew installation to a new location?

I have a Homebrew installation in $HOME/brew, and historically it has worked well. Unfortunately, over time Homebrew has become less and less tolerant of installations outside of /usr/local. Various formulae make hard assumptions about the installation prefix, and do not work properly (i.e., were not tested) with a non-standard prefix. The brew doctor command even goes so far as to warn about this now:
Warning: Your Homebrew is not installed to /usr/local
You can install Homebrew anywhere you want, but some brews may only build
correctly if you install in /usr/local. Sorry!
As such, I would now like to migrate my Homebrew installation over to /usr/local. However, I am loath to simply mv all the files, as I suspect this will cause problems. I could not find any instructions on the Homebrew site or here on migrating an existing installation to a new prefix. Of course, I could uninstall Homebrew and then reinstall it, but I would prefer not to rebuild all my kegs.
Is there any existing script or documented practice for performing such a migration?
Or is this impossible due to hardcoded absolute paths in linked binaries?
The modern way to do this is with homebrew-bundle.
brew tap Homebrew/bundle
brew bundle dump # Creates 'Brewfile' in the current directory
# later ...
brew bundle # Installs packages listed in 'Brewfile'
I just wrote a script to achieve the goal to migrate homebrew packages to a new system, which also applies for your case (named backup-homebrew.sh):
#!/bin/bash
echo '#!/bin/bash'
echo ''
echo 'failed_items=""'
echo 'function install_package() {'
echo 'echo EXECUTING: brew install $1 $2'
echo 'brew install $1 $2'
echo '[ $? -ne 0 ] && $failed_items="$failed_items $1" # package failed to install.'
echo '}'
brew tap | while read tap; do echo "brew tap $tap"; done
brew list --formula | while read item;
do
echo "install_package $item '$(brew info $item | /usr/bin/grep 'Built from source with:' | /usr/bin/sed 's/^[ \t]*Built from source with:/ /g; s/\,/ /g')'"
done
echo '[ ! -z $failed_items ] && echo The following items were failed to install: && echo $failed_items'
You should first run this script on your original system to generate a restore script:
./backup-homebrew.sh >restore-homebrew.sh && chmod +x restore-homebrew.sh
Then, after installing Homebrew on your new system (in your case the same system), simply run restore-homebrew.sh to install all those packages you have on your original system.
The benefits of this script over the one given by #ctrueden is that this script also tries to back up the installation options you used when you installed the packages.
A more detailed description is in my blog.
As suggested by Peter Eisentraut, I indeed ended up migrating my Homebrew installation by reinstalling it. You can script things a bit to retap all your extra taps, and reinstall all your previously installed kegs, without too much manual work:
#!/bin/sh
# save list of kegs for later reinstallation
brew list > kegs.txt
# back up old Homebrew installation
mv $HOME/brew $HOME/old-brew
# install Homebrew into /usr/local
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
# retap all the taps
# NB: It is not enough to move the tap repos to their new location,
# because Homebrew will not automatically recognize the new formulae.
# There might be a configuration file we could edit, but rather than
# risk an incomplete data structure, let's just retap everything.
for tapDir in $HOME/old-brew/Library/Taps/*
do (
cd $tapDir
tap=$(git remote -v | \
grep '(fetch)' | \
sed 's/.*github.com\///' | \
sed 's/ (fetch)//')
/usr/local/bin/brew tap $tap
) done
# reinstall all the kegs
/usr/local/bin/brew install $(cat kegs.txt)
# much later... ;-)
rm -rf kegs.txt $HOME/old-brew
Of course, customized Homebrew installations will have additional wrinkles. For example, if you have committed changes to any of your Homebrew-related Git repos, you may want to import that work before reinstalling your kegs or blowing away your old installation:
cd /usr/local
for f in $(find . -name '.git')
do (
repoDir=$(dirname $f)
cd $f/..
git remote add old-brew-$f $(dirname $HOME/old-brew/$f/..)
git fetch old-brew-$f
) done
Note that I only tested the second snippet above very lightly, as I personally have not customized my Homebrew in such a way.
Another aspect of Homebrew not addressed by this approach is custom flags using during your original installation. For example, to install wine you need to install various dependencies with the --universal flag, and the script above will not reinstall them with such flags enabled. See #xuhdev's answer for a solution that does so.
Or is this impossible due to hardcoded absolute paths in linked binaries?
Indeed. You'll need to reinstall everything from scratch.

RVM not found, after installing RVM

I've found a couple similar posts regarding this same problem, but none of the solutions seem to apply, here.
On a fresh Ubuntu 10.10 install, I follow the instructions for installing RVM:
$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
Then I create .bash_profile and add the following line:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
I restart the terminal and check RVM:
$ type rvm | head -1
-bash: type: rvm: not found
As the RVM installation guide explains to do so, I replaced the first line (below) in .bashrc with the second one, then indented everything in the rest of the file and added a fi.
[ -z "$PS1" ] && return # original
if [[ -n "$PS1" ]]; then # replaced with this
Restarted terminal and still, no luck.
Then, I removed the line I added to .bash_profile in the beginning and added it to .bashrc, even though that isn't what the guide said to do. Still, no luck. I also entered it directly on the command line, with no change in behavior. When I run .rvm from ~/.rvm/bin/rvm it complains that there is no such file or directory as /.rvm/scripts/rvm and that the command was not fund.
Of course, there isn't any such "scripts" directory inside of ./rvm, either -- so I'm not sure why it's looking for one? The only directories inside of .rvm are
archives
bin
config
gems
gemsets
log
man
rubies
src
tmp
user
The only thing I've found while googling for answers are other people complaining of similar problems and people telling them to add the instructed line to .bash_profile (which I obviously already did). At this point, I have nothing more to go on and am at an impasse.
Regards.
Resolution:
As Andrew Marshall advised in his comments, below, I did an 'rm -rf .rvm' and reinstalled rvm. I had actually attempted this two times before posting here, with the same results every time. No odd messages in the install log, but no /scripts/ directory, either. Just so I could say I had, I did it a third time at Andrew's urging. This time, I checked and the /scripts/ directory existed. Running 'type rvm | head -1' confirmed it as a 'function' and I can now move on.
Make sure that you restart a session after reinstalling, so that rvm is in your path.
You can try to logout/login.
You can also open your shell as a login shell. Under ubuntu 12.04:
Open a terminal
Edit > Profile Preferences
Under tab Title and Command, check run Command as a login shell
Open new terminal (ctrl+alt+t) and type rvm
If there's no scripts directory inside .rvm, it would seem that RVM failed to successfully complete installation. Delete the .rvm directory, try reinstalling, and look at the installation output closely to see if it's complaining about anything.

Resources