How to run commands in any directory in macOS Terminal? - macos

I have some Unix Executable Files in a paticular directory, say /Users/myUserName/Developer/sdcc/bin/.
I find I must get into this directory using cd before running that command, or it complains zsh: command not found.
But it can be of inconvenience. I know the commands can be used in any directory if it's installed by Homebrew. How can I do the same thing?

Homebrew usually links the necessary executables to /usr/local/bin directory, which should be in your $PATH. Thus, when you execute a command like sdcc, your shell will seek through the $PATH directories, and when it looks at /usr/local/bin, it will find sdcc, follow the link and execute it.
Some packages do not perform this linking, which means you cannot execute them without knowing where they are. You can ask Homebrew to tell you where a package is installed: brew --prefix <formula>, where <formula> is the package name (e.g. brew --prefix sdcc). The executable files will normally be under a bin subdirectory. For example, brew --prefix sdcc would likely tell you something like /usr/local/opt/sdcc; then you can invoke sdcc using /usr/local/opt/sdcc/bin/sdcc, without having to cd there. You could also put /usr/local/opt/sdcc/bin into your $PATH directly, though I do not recommend it. Another alternative is to create your own bin (mine is in $HOME/.local/bin), put it in $PATH, and link there (ln -s <source> $HOME/.local/bin/) any executables you wish your shell to easily find.
However, with Homebrew packages, I strongly suggest you do not try to imitate Homebrew by yourself, by installing things in Homebrew's domain. You can confuse Homebrew and create problems. Instead, let Homebrew do it.
If you need to install a package on a different OS than the one you are downloading at, you may need to first find out the bottle tag for the target (installation) computer. For example, for Big Sur, it is big_sur. See brew info --json <formula>, under bottle.stable.files you should find the bottle tags. Use --bottle-tag <tag> in step 1 and 2 to select the right OS.
Use brew fetch --deps <formula> to download (but not install) a package, including its dependencies. If you use the --verbose flag, Homebrew will tell you where it downloaded each of the files.
If you haven't used --verbose and still want to know where the downloaded files are: brew deps <formula> will list all the packages it depends on. If a package needs to be compiled, you may need to also use the --include-build option. brew --cache <formula> will show you where a package file is downloaded.
If you need to copy a package file to another computer, you should find out where the Homebrew would expect to find it: use brew --cache --force-bottle <formula> on the target computer, and copy the package file there. Don't forget to do that for each dependency package as well.
After that, brew install <formula> will install from cache.
The only part of this process that needs internet connection is the first step, brew fetch.

For Unix (which MacOS is a family member) and Unix-like (eg. Linux) operating systems the thing that controls this feature is the PATH environment variable.
To know which folders allow you to run programs without cd-ing into them or prefix ./ to your program name you can print out the value of PATH:
echo $PATH
On my machine it prints out:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/slebetman/bin:/opt/homebrew/bin
There are two things to notice:
The string is a list of folder paths separated by :
There is a path listed as /opt/homebrew. Presumably this is where homebrew installs command-line programs.
Copying your program/script into any of the folders listed in PATH will allow you to use it like any installed/built-in command. This is what it means to install command-line programs on Unix. It's that simple.
There is another thing to notice: I have in my PATH the folder /Users/slebetman/bin. This is my own personal standard - probably not invented by me but one I adopted from my college days - for installing my own programs. I have this ~/bin folder on all my Unix and Linux machines. It is where I keep my personal collection of scripts that I've written or collected over a lifetime.
Having a personal ~/bin folder is nice because you don't need sudo permission to write to it. Also, if you buy a new PC/Mac you can just copy the folder to your new machine and all the scripts you've gotten used to now exist on the new machine. Better yet, you can manage the folder with git and sync it with github/gitlab/bitbucket to make it easy to keep all your computers updated.
Since PATH is a standard environment variable you can easily modify it to add your own folder. Just add the following in your .profile or .zshrc or .bashrc file (depending on your shell) to add your own bin folder to PATH:
export PATH=$PATH:$HOME/bin
The command above simply says add $HOME/bin to PATH and export it so that other programs will inherit the new PATH variable.
In your case you can even do something like:
export PATH=$PATH:/path/to/your/sdcc/bin
to have all your SDCC commands work like regular commands.

Related

Use RVM Ruby instead of MAMP Ruby

I've used RVM to install the latest stable version of Ruby, however, I can't set it to be the default version. Running which ruby always returns the path of Ruby installed with MAMP on my system eg /Applications/MAMP/Library/bin/ruby
I've tried various rvm commands with no effect. I'd rather not have to run an rvm use command every time I open my terminal.
I'm using oh-my-zsh and iTerm2 if it makes a difference.
As Dave Newton suggested, this smells like a PATH issue, but in the case of MAMP, it's also an alias issue.
If you open ~/.profile and ~/.zprofile, in one of them you will probably see these lines:
alias erb='/Applications/MAMP/Library/bin/erb'
alias gem='/Applications/MAMP/Library/bin/gem'
alias irb='/Applications/MAMP/Library/bin/irb'
alias rake='/Applications/MAMP/Library/bin/rake'
alias rdoc='/Applications/MAMP/Library/bin/rdoc'
alias ri='/Applications/MAMP/Library/bin/ri'
alias ruby='/Applications/MAMP/Library/bin/ruby'
alias rails='/Applications/MAMP/Library/bin/rails'
You'll want to remove them all, save the file, then quit and restart iTerm2.
If you don't know how to open and edit dotfiles, read my guide that explains various ways to read and edit dotfiles on a Mac.
If removing those aliases doesn't fix it, then it's a PATH issue.
For your Mac to know about a command or other executable program, it has to be told where to look for it. It wouldn't be efficient for the computer to search the entire hard drive for the program.
Instead, it looks in a specific list of locations, which are stored in an environment variable called PATH, separated by a colon. You can view this list by running this command in your terminal:
echo $PATH
When you install new programs, such as Ruby, they might get installed in a location that is not already included in the PATH. If you don't add this new location to the PATH, the computer won't know to look for it there, and so it thinks it doesn't exist.
Similarly, if the location of the new program did get added to the PATH, but you have another location for the same program earlier in the PATH, then it will always use the first one it finds.
Most Ruby version managers use a script to automatically update the PATH, and they instruct you to add a line to your shell file to call that script, or they might add it for you. That line should come after any modifications of PATH.
It's been a while since I've used RVM (I no longer recommend it), but I think it's supposed to automatically add this line to your shell file:
source $HOME/.rvm/scripts/rvm
Assuming you installed RVM properly, here's what I would try:
Open your shell file. It should be ~/.zshrc if you're using oh-my-zsh.
Look for any lines that start with export PATH=, and if they mention /Applications/MAMP/Library/bin/ruby, remove that directory from the PATH. Also make sure any PATH lines come before the lines added by RVM.
Quit and restart iTerm2
If you don't see any RVM-related lines in ~/.zshrc or ~/.zprofile or ~/.profile, then RVM was not properly installed.
If you really want to use RVM, try uninstalling and reinstalling it. If all you care about is having a working Ruby environment, I would recommend chruby and ruby-install. You can install them by following my step-by-step guide to install Ruby on Mac.

need to manually create symlinks every time i install something

For example, I'm trying to install pipenv using the following commands:
$ pip3 install --user pipenv
$ cd ~/Documents/myproject
$ pipenv install requests
zsh: command not found: pipenv
It seems that the issue can be resolved if I run sudo ln -s ~/.local/bin/pipenv /usr/bin. Is this a safe thing to do? I've also been having this issue with several other packages, like tensorboard and xflux. Is there something wrong with my system that makes it such that I need to manually create a symlink every time? How can I change things so that packages can install normally without me having to do this every time?
The problem you are having is that the tools you are installing are being installed in non-standard locations and are not being included in your PATH. One way to resolve this would be to include the paths in your PATH environment variable. In order for these programs to be in your path every time you login, do this (assuming you are using zsh as your shell):
1) Open ~/.zshrc using vim or any other editor:
vim ~/.zshrc
2) Append the following to this file:
export PATH=$PATH:/path/to/dir/containing/pipenv
Remember to replace /path/to/dir/containing/pipenv with the appropriate path.
3) Source your ~/.zshrc so that the path is included in your current session:
source ~/.zshrc
4) Now everytime you login, the paths should be available in your environment and you won't need to create symlinks.

Where does Linux install Pocketsphinx program?

I am new in Linux and don't know much about it's installation manner. It's interesting for me to know where does it install the pockesphinx program?
where is continuous.cfile?
How Linux can find and execute this command: pocketsphinx_continuous -inmic yes ?
Where stuff is installed depends on how it was installed. Usually: If you were using your package manager, refer to your distrobutions documentation. If you extracted a tarball or installed using a script from a projects website, refer to their documentation.
If you are able to invoke the program from your terminal from everywhere, it will probably reside somewhere on your $PATH. You can show which directories are included in your path by issuing cat $PATH in your terminal of choice.
Also, the locate command is useful if you need to locate a file on your disk. I.e. locate pocketsphinx would give you all files that has the pocketsphinx string in its name, and its location. If you just installed the program, you might need to run a sudo updatedb to rebuild the index.

Installing Julia v0.5 on Ubuntu 16.04 while v0.6 is installed

I use v0.6, but certain packages do not function with v0.6 (such as Interact.jl). How can I install v0.5? I am running Ubuntu 16.04.
First we have to download the latest Julia version from https://julialang.org/downloads/
I used the “Generic Linux Binaries for x86” version. The choice between x86 and ARM depends on the processor of your machine. Also choose between 32 bit and 64 bit versions based on the operating system and processor you have on your machine.
After download, you will get a compressed tar.gz archive having name similar to “julia-0.6.2-linux-x86_64.tar.gz”. As the “julia-0.6.2-linux-x86_64.tar.gz” name suggests that I downloaded the Julia version 0.6.2 which is latest at the time of writing this.
The names may differ. Adapt the names accordingly.
Remember these are binaries, these don't need to be installed as they can be directly used from the directory they are extracted.
I am assuming that the downloaded file is in your ~/Downloads directory of Ubuntu.
Open the terminal and navigate to the directory where the downloaded tar.gz file is stored, in present case the Downloads directory.
The terminal when just opened will show:
x#xpc:~$
where x should be replaced by your username and xpc should be replaced by your computer name.
Navigate to the Downloads directory using cd Downloads and then press Enter to get following terminal:
x#xpc:~/Downloads $
Extract the tar.gz file using the command
tar -zxvf julia-0.6.2-linux-x86_64.tar.gz
Now a directory with extracted contents will be generated in the same parent directory as of the compressed archive with a name similar to julia-xxxxxxxxxx where xxxxxxxxxx may be a combination of alphabets and numerals.
This is the final directory you need to run the latest Julia version, no installation is needed.
To run Julia, you can directly run using the julia file in location julia-xxxxxxxxxx/bin/julia as discussed below.
Navigate to the bin directory in the extracted directory using
cd /Downloads/julia-xxxxxxxxxx/bin
The terminal will now be like:
x#xpc:~/Downloads/julia-xxxxxxxxxx/bin $
Now run the command ./julia to run julia on the terminal as shown below.
The terminal will now change to julia as presented below. I know the representation is little different here as this is all I can manage to copy from the terminal to present it to you.
julia>
But the problem is that I have to navigate to the directory every time to run Julia.
Many people have discussed on the internet about defining the path and alias through very complex procedures and as I am not a hardcore computer geek, it was really difficult for me to understand.
I came to know about making a soft link.
So I decided to make a soft link to the Julia to run it directly from anywhere with a short command without navigating to the directory containing it.
I always try to do things neatly, so I decided to keep the extracted directory named julia-xxxxxxxxxx in the /opt directory of my system as most of my important programs reside in that.
You need root permissions to copy a file into the /opt directory, so I used the command sudo su and then provided password to get the super user privileges:
x#xpc:~$ sudo su
[sudo] password for x:
root#xpc:/home/x#
Now navigate to the directory presently containing the extracted directory:
root#xpc:/home/x# cd /Downloads/
root#xpc:/home/x/Downloads#
Copy the directory using:
root#xpc:/home/x/Downloads# cp -r julia-xxxxxxxxxx /opt
After the directory is copied to the destination, now we will make the soft link in a directory which is in the system path so that the soft-link can be called from any location to run Julia.
To find out the directories in the system PATH use echo $PATH, you will get a list of paths of directories separated by colon(:) such as /usr/local/bin. You can use any of them. I used /usr/local/bin for the soft link.
Navigate to the chosen folder.
root#xpc:/home/x# cd /usr/local/bin
The terminal will become
root#xpc:/home/x/usr/local/bin#
Create the soft link using
root#xpc:/home/x/usr/local/bin# sudo ln -s /opt/julia-xxxxxxxxxx/bin/julia julia
Now return to the normal user terminal using the keyboard combination Ctrl+D at the empty terminal root#xpc:/home/x/usr/local/bin#.
The terminal prompt will become:
x#xpc:~$
Type the newly made soft link i.e. julia in the terminal as shown below
x#xpc:~$ julia
This is where the magic happens and you get this:
julia>
The instructions can be used for any version of Julia in Ubuntu.
Source
Firstly, you should really read the post that Reza Afzalan linked. It gives you everything you need to know about how to install. If you prefer a list:
Go to the Julia download page.
Download the Generic Linux Binary for your OS (probably 64-bit for Ubuntu 16).
Install it.
Find where the installed Julia binary executables are stored on your machine.
Symlink Julia v0.5 and Julia v0.6 to different aliases, e.g. julia5 and julia6. You can store the symlinks in a directory like /usr/local/bin.
Open julia5.
Start downloading your packages with Pkg.add.
It's very easy, here's how to install it on Linux in 8 steps:
1- go to Julia downloads page
2- Choose you version (32bit or 64bit) from Generic Linux Binaries, and then download it.
3- extract .tar.gz file in home or any place you would like to install Julia on it.
4- run > gksudo gedit /etc/environment on your terminal to setup envirnment path.
5- edit file :/Path_To_extracted_File/bin in my case it was like this:
":/home/okasha/julia-d386e40c17/bin"
6- Save edits and close the file.
7- Restart your machine.
8- Open your terminal again and run > julia -version
you shoud get "julia version 0.6.2" for Example according to your installed julia version.
Run > julia to open julia session.
Reference
Just type this
sudo apt install julia

"config" scripts exist outside your system or Homebrew directory

Ran "brew doctor" and got some errors. I managed to fix the path issue by following the advice at this link: How to modify PATH for Homebrew?.
However, completely lost with what to do with the following error:
Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and what additional flags to use when
compiling and linking.
Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew provided
script of the same name. We found the following "config" scripts:
/opt/sm/pkg/active/bin/curl-config
/opt/sm/pkg/active/bin/ncurses5-config
/opt/sm/pkg/active/bin/ncursesw5-config
/opt/sm/pkg/active/bin/pkg-config
/opt/sm/pkg/active/bin/xml2-config
/opt/sm/pkg/active/bin/xslt-config
Noob. Running OSX. Appreciate any assistance from wiser Jedis...
If you see this message it means you have packages installed without using homebrew.
If these packages are only installed outside homebrew their config scripts should not interfere with homebrew and you don't have to worry.
There might be a problem if you have, for example curl, installed in homebrew and in another packages manager or manually.
If you need these softwares you should remove them and reinstall them in homebrew otherwise you can leave them in /opt or uninstall them.

Resources